164 lines
4.6 KiB
TypeScript
164 lines
4.6 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import {
|
|
getCoreRowModel,
|
|
type PaginationState,
|
|
type RowSelectionState,
|
|
} from "@tanstack/react-table";
|
|
import { useConfirm } from "@frontend-opensource/use-react-hooks";
|
|
import {
|
|
Button,
|
|
Dialog,
|
|
DialogClose,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
Input,
|
|
Label,
|
|
} from "@common/components";
|
|
import { ENDPOINT } from "@/shared/config";
|
|
import { useAlertStore, useDataTable, useMutationWrapper } from "@/shared/lib";
|
|
import { MenuHistoryApi } from "../api/menu-history-service";
|
|
import type { MenuHistorySearchParams } from "../model/menu-history-interface";
|
|
import { columns } from "../model/menu-history-columns";
|
|
|
|
type MenuHistoryDialogProps = {
|
|
menuCd?: number;
|
|
} & React.ComponentProps<typeof Dialog>;
|
|
|
|
function MenuHistoryDialog({ menuCd }: MenuHistoryDialogProps) {
|
|
const { confirm } = useConfirm();
|
|
const queryClient = useQueryClient();
|
|
const alert = useAlertStore((state) => state.setMessage);
|
|
|
|
const [open, setOpen] = useState(false);
|
|
const [pagination, setPagination] = useState<PaginationState>({
|
|
pageIndex: 0,
|
|
pageSize: 10,
|
|
});
|
|
const [searchParams, setSearchParams] = useState<MenuHistorySearchParams>({
|
|
keyword: "",
|
|
keywordType: "menuKornNm",
|
|
});
|
|
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
|
|
const selectedHistoryId = parseInt(Object.keys(rowSelection)[0] ?? "0");
|
|
|
|
const query = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.MENU_HISTORY, pagination],
|
|
queryFn: () =>
|
|
MenuHistoryApi.getMenuHistoryList({
|
|
...searchParams,
|
|
menuCd,
|
|
page: pagination.pageIndex,
|
|
size: pagination.pageSize,
|
|
}),
|
|
enabled: !!menuCd && open,
|
|
});
|
|
|
|
const { mutateAsync: rollbackMenu } = useMutationWrapper({
|
|
mutationFn: (hstrySn: number) => MenuHistoryApi.rollbackMenu(hstrySn),
|
|
onSuccess: () => {
|
|
alert("메뉴가 성공적으로 복원되었습니다.");
|
|
queryClient.invalidateQueries({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.MENUS],
|
|
});
|
|
setOpen(false);
|
|
},
|
|
});
|
|
|
|
const handleRollback = async (hstrySn: number) => {
|
|
if (!(await confirm("메뉴를 '이전 데이터'로 되돌리시겠습니까?"))) return;
|
|
await rollbackMenu(hstrySn);
|
|
};
|
|
|
|
const { tableComponent } = useDataTable({
|
|
query,
|
|
tableOptions: {
|
|
columns,
|
|
state: { pagination, rowSelection },
|
|
manualPagination: true,
|
|
enableRowSelection: true,
|
|
enableMultiRowSelection: false,
|
|
onPaginationChange: setPagination,
|
|
onRowSelectionChange: setRowSelection,
|
|
getCoreRowModel: getCoreRowModel(),
|
|
getRowId: (row) => row.hstrySn.toString(),
|
|
},
|
|
dataTableOptions: {
|
|
viewPagination: true,
|
|
},
|
|
});
|
|
|
|
const handleSearch = (e: React.FormEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
query.refetch();
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (open) {
|
|
setSearchParams({
|
|
...searchParams,
|
|
keyword: "",
|
|
});
|
|
setPagination({
|
|
...pagination,
|
|
pageIndex: 0,
|
|
});
|
|
setRowSelection({});
|
|
}
|
|
}, [open]);
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
<DialogTrigger asChild>
|
|
<Button color="white" disabled={!menuCd}>
|
|
<i className="diveicon di-history" />
|
|
이력보기
|
|
</Button>
|
|
</DialogTrigger>
|
|
<DialogContent className="max-w-[100rem]" aria-describedby={undefined}>
|
|
<DialogHeader>
|
|
<DialogTitle>메뉴 이력 관리</DialogTitle>
|
|
</DialogHeader>
|
|
|
|
<form onSubmit={handleSearch}>
|
|
<div className="mb-4 flex items-center gap-2">
|
|
<Label>메뉴명</Label>
|
|
<Input
|
|
placeholder="검색어를 입력하세요"
|
|
value={searchParams.keyword ?? ""}
|
|
onChange={(e) =>
|
|
setSearchParams({ ...searchParams, keyword: e.target.value })
|
|
}
|
|
className="w-[36rem]"
|
|
/>
|
|
<Button type="submit">조회</Button>
|
|
</div>
|
|
</form>
|
|
|
|
<div className="max-h-[56rem] overflow-auto">{tableComponent}</div>
|
|
|
|
<DialogFooter className="!justify-center">
|
|
<DialogClose asChild>
|
|
<Button size="lg" color="white">
|
|
닫기
|
|
</Button>
|
|
</DialogClose>
|
|
<Button
|
|
size="lg"
|
|
onClick={() => handleRollback(selectedHistoryId)}
|
|
disabled={!selectedHistoryId}
|
|
>
|
|
되돌리기
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|
|
|
|
export { MenuHistoryDialog };
|