142 lines
4.2 KiB
TypeScript
142 lines
4.2 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { useForm } from "react-hook-form";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { getCoreRowModel, type RowSelectionState } from "@tanstack/react-table";
|
|
import { useConfirm } from "@frontend-opensource/use-react-hooks";
|
|
import { AuthorityButton } from "@/entities/menu-authority";
|
|
import { PostingApi, type PostPayload } from "@/entities/posting";
|
|
import {
|
|
useAlertStore,
|
|
useUserStore,
|
|
useMutationWrapper,
|
|
useDataTable,
|
|
} from "@/shared/lib";
|
|
import { ENDPOINT } from "@/shared/config";
|
|
import {
|
|
popupUpdateFormSchema,
|
|
type PopupUpdateFormData,
|
|
} from "../model/popup-interface";
|
|
import { columns } from "../model/posting-table-columns";
|
|
import { PopupApi } from "../api/popup-service";
|
|
import { PopupForm } from "./popup-form";
|
|
|
|
function ModifyPage({ popupSn }: { popupSn: number }) {
|
|
const router = useRouter();
|
|
const queryClient = useQueryClient();
|
|
const { confirm } = useConfirm();
|
|
const userInfo = useUserStore((state) => state.userInfo);
|
|
const alert = useAlertStore((state) => state.setMessage);
|
|
|
|
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
|
|
|
|
const { data: popupInfo } = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.POPUPS, popupSn],
|
|
queryFn: () => PopupApi.getPopupInfo(popupSn),
|
|
enabled: !!popupSn,
|
|
});
|
|
|
|
const query = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.POSTINGS, popupSn],
|
|
queryFn: () => PostingApi.getPostingHistory({ pstgTrgtNo: popupSn }),
|
|
});
|
|
|
|
const { mutateAsync: updatePopup } = useMutationWrapper({
|
|
mutationFn: (data: PopupUpdateFormData) =>
|
|
PopupApi.updatePopup(popupSn, data),
|
|
onSuccess: () => {
|
|
alert("수정되었습니다.", router.back);
|
|
},
|
|
});
|
|
|
|
const { mutateAsync: deletePopup } = useMutationWrapper({
|
|
mutationFn: () => PopupApi.deletePopup(popupSn),
|
|
onSuccess: () => {
|
|
alert("삭제되었습니다.", router.back);
|
|
},
|
|
});
|
|
|
|
const { mutateAsync: savePostingHistory } = useMutationWrapper({
|
|
mutationFn: (data: PostPayload[]) => PostingApi.savePosting(data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.POSTINGS, popupSn],
|
|
});
|
|
alert("삭제되었습니다.");
|
|
},
|
|
});
|
|
|
|
const form = useForm<PopupUpdateFormData>({
|
|
resolver: zodResolver(popupUpdateFormSchema),
|
|
values: {
|
|
...popupInfo,
|
|
mdfr: userInfo?.userUnqId ?? "",
|
|
},
|
|
});
|
|
|
|
const { tableComponent, tableInstance: table } = useDataTable({
|
|
query,
|
|
tableOptions: {
|
|
columns,
|
|
state: {
|
|
rowSelection,
|
|
},
|
|
enableMultiRowSelection: true,
|
|
getCoreRowModel: getCoreRowModel(),
|
|
onRowSelectionChange: setRowSelection,
|
|
},
|
|
});
|
|
|
|
const handleDeletePostingHistory = async () => {
|
|
const data = table.getFilteredSelectedRowModel().rows.map((row) => ({
|
|
...row.original,
|
|
mode: "D",
|
|
}));
|
|
|
|
if (await confirm("삭제하시겠습니까?")) {
|
|
savePostingHistory(data);
|
|
setRowSelection({});
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="mt-6 flex flex-col">
|
|
<div className="card card-border !mb-0 !mt-6">
|
|
<div className="card-header">
|
|
<h3 className="text-2xl font-medium text-label">팝업 수정</h3>
|
|
</div>
|
|
<PopupForm
|
|
form={form}
|
|
popupSn={popupSn}
|
|
handleSave={updatePopup}
|
|
handleDelete={deletePopup}
|
|
/>
|
|
</div>
|
|
<div className="card card-border">
|
|
<div className="card-header">
|
|
<h3 className="text-2xl font-medium text-label">사이트 게시 이력</h3>
|
|
</div>
|
|
<div className="card">
|
|
<div className="mb-4 flex justify-end">
|
|
<AuthorityButton
|
|
authority="mngAuthrtYn"
|
|
size="sm"
|
|
color="red"
|
|
disabled={!table.getFilteredSelectedRowModel().rows.length}
|
|
onClick={handleDeletePostingHistory}
|
|
>
|
|
이력삭제
|
|
</AuthorityButton>
|
|
</div>
|
|
{tableComponent}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export { ModifyPage };
|