152 lines
4.0 KiB
TypeScript
152 lines
4.0 KiB
TypeScript
import dayjs from "dayjs";
|
|
import { memo, useState } from "react";
|
|
import { useFieldArray, useForm } from "react-hook-form";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { type ColumnDef } from "@tanstack/react-table";
|
|
import { useConfirm } from "@frontend-opensource/use-react-hooks";
|
|
import {
|
|
Button,
|
|
Checkbox,
|
|
Dialog,
|
|
DialogClose,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
} from "@common/components";
|
|
import type { Site } from "@/entities/site";
|
|
import { TreeMenuApi } from "@/entities/tree-menu";
|
|
import {
|
|
MenuApi,
|
|
siteMapFormSchema,
|
|
type SiteMapFormData,
|
|
} from "@/entities/menu";
|
|
import { ENDPOINT } from "@/shared/config";
|
|
import { useAlertStore, useMutationWrapper } from "@/shared/lib";
|
|
import { MenuTree } from "../ui/menu-tree";
|
|
import { generateMenuTreeData } from "../lib/utils";
|
|
|
|
export const columns: ColumnDef<Site>[] = [
|
|
{
|
|
id: "no",
|
|
header: "NO",
|
|
cell: ({ row, table }) => {
|
|
const totalRows = table.getRowCount();
|
|
const currentPage = table.getState().pagination.pageIndex;
|
|
const pageSize = table.getState().pagination.pageSize;
|
|
return totalRows - (currentPage * pageSize + row.index);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "siteId",
|
|
header: "사이트코드",
|
|
},
|
|
{
|
|
accessorKey: "siteNm",
|
|
header: "사이트명",
|
|
},
|
|
{
|
|
accessorKey: "dmnAddr",
|
|
header: "대표 URL",
|
|
},
|
|
{
|
|
accessorKey: "bscSiteYn",
|
|
header: "기본 사이트 여부",
|
|
},
|
|
{
|
|
accessorKey: "useYn",
|
|
header: "사용 여부",
|
|
},
|
|
{
|
|
accessorKey: "rgTrNm",
|
|
header: "등록자",
|
|
},
|
|
{
|
|
accessorKey: "regDt",
|
|
header: "등록일",
|
|
cell: ({ row }) => {
|
|
const date = dayjs(row.original.regDt);
|
|
return date.format("YYYY-MM-DD");
|
|
},
|
|
},
|
|
{
|
|
id: "action",
|
|
header: "관리",
|
|
cell: ({ row }) => {
|
|
return (
|
|
<div onClick={(e) => e.stopPropagation()}>
|
|
<ActionCell data={row.original} />
|
|
</div>
|
|
);
|
|
},
|
|
},
|
|
];
|
|
|
|
const ActionCell = memo(({ data }: { data: Site }) => {
|
|
const { confirm } = useConfirm();
|
|
const alert = useAlertStore((state) => state.setMessage);
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const { data: menuTreeData, refetch } = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.MENUS, data.siteId],
|
|
queryFn: () => TreeMenuApi.getTreeMenuList(data.siteId),
|
|
select: (data) => generateMenuTreeData(data),
|
|
enabled: !!data.siteId && open,
|
|
});
|
|
|
|
const { mutate: publishSiteMap } = useMutationWrapper({
|
|
mutationFn: (data: SiteMapFormData["siteMap"]) =>
|
|
MenuApi.publishSiteMap(data),
|
|
onSuccess: () => {
|
|
alert("사이트맵이 저장되었습니다.", () => {
|
|
setOpen(false);
|
|
refetch();
|
|
});
|
|
},
|
|
});
|
|
|
|
const form = useForm<SiteMapFormData>({
|
|
resolver: zodResolver(siteMapFormSchema),
|
|
});
|
|
const fieldArray = useFieldArray({
|
|
control: form.control,
|
|
name: "siteMap",
|
|
});
|
|
|
|
const handlePublishSiteMap = async () => {
|
|
if (await confirm("저장하시겠습니까?")) {
|
|
publishSiteMap(form.getValues("siteMap"));
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
<DialogTrigger asChild>
|
|
<Button size="sm">사이트맵</Button>
|
|
</DialogTrigger>
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle>{data.siteNm} - 사이트맵 관리</DialogTitle>
|
|
</DialogHeader>
|
|
<div>
|
|
<div className="flex items-center justify-end gap-2 p-2">
|
|
<span className="text-xl">표시여부</span>
|
|
<Checkbox checked />
|
|
</div>
|
|
<MenuTree tree={menuTreeData ?? []} fieldArray={fieldArray} />
|
|
</div>
|
|
<DialogFooter>
|
|
<Button onClick={handlePublishSiteMap}>저장</Button>
|
|
<DialogClose asChild>
|
|
<Button color="white">취소</Button>
|
|
</DialogClose>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
});
|
|
|
|
ActionCell.displayName = "ActionCell";
|