[code] 하위코드 등록후 재조회 로직 추가 handleAfterSave 추가

This commit is contained in:
2025-07-10 19:02:26 +09:00
parent ec91aa461a
commit 20db700215
4 changed files with 134 additions and 68 deletions
+8
View File
@@ -0,0 +1,8 @@
# 디폴트 무시된 파일
/shelf/
/workspace.xml
# 에디터 기반 HTTP 클라이언트 요청
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
@@ -5,56 +5,62 @@ import type { LwpoCode } from "@/entities/code";
import { LwpoCodeManagementDialog } from "../ui/lwpocode-dialog";
import { useState } from "react";
export const columns: ColumnDef<LwpoCode>[] = [
{
id: "no",
header: "NO",
cell: ({ row, table }) => {
const totalRows = table.getRowCount();
const { pageIndex, pageSize } = table.getState().pagination;
return totalRows - (pageIndex * pageSize + row.index);
// columns를 함수로 변경하여 onAfterSave를 주입받음
export function getColumns(onAfterSave: () => void) : ColumnDef<LwpoCode>[] {
return [
{
id: "no",
header: "NO",
cell: ({ row, table }) => {
const totalRows = table.getRowCount();
const { pageIndex, pageSize } = table.getState().pagination;
return totalRows - (pageIndex * pageSize + row.index);
},
},
},
{
accessorKey: "lwpoCd",
header: "하위코드",
},
{
accessorKey: "lwpoCdNm",
header: "하위코드명",
},
{
accessorKey: "useYn",
header: "사용 여부",
},
{
accessorKey: "regDt",
header: "등록일",
cell: ({ row }) => {
const date = dayjs(row.original.regDt);
return date.format("YYYY-MM-DD");
{
accessorKey: "lwpoCd",
header: "하위코드",
},
},
{
id: "actions",
header: "관리",
cell: ({ row }) => (
<div onClick={(e) => e.stopPropagation()}>
<LwpoCodeManageButton
lwpoCd={row.original.lwpoCd}
upCd={row.original.upCd}
/>
</div>
),
},
];
{
accessorKey: "lwpoCdNm",
header: "하위코드명",
},
{
accessorKey: "useYn",
header: "사용 여부",
},
{
accessorKey: "regDt",
header: "등록일",
cell: ({ row }) => {
const date = dayjs(row.original.regDt);
return date.format("YYYY-MM-DD");
},
},
{
id: "actions",
header: "관리",
cell: ({ row }) => (
<div onClick={(e) => e.stopPropagation()}>
<LwpoCodeManageButton
lwpoCd={row.original.lwpoCd}
upCd={row.original.upCd}
onAfterSave={onAfterSave}
/>
</div>
),
},
];
}
const LwpoCodeManageButton = ({
lwpoCd,
upCd,
onAfterSave,
}: {
lwpoCd: string;
upCd: string;
onAfterSave: () => void;
}) => {
const [dialogOpen, setDialogOpen] = useState(false);
return (
@@ -63,6 +69,7 @@ const LwpoCodeManageButton = ({
upCd={upCd}
open={dialogOpen}
onOpenChange={setDialogOpen}
onAfterSave={onAfterSave}
>
<Button size="sm">관리</Button>
</LwpoCodeManagementDialog>
+64 -22
View File
@@ -37,7 +37,8 @@ type ManagementLwpoCodeDialogProps = {
upCd?: string;
children?: React.ReactNode;
open?: boolean;
onOpenChange?: (open: boolean) => void;
onOpenChange?: (open: boolean) => void;
onAfterSave?: () => void; // 콜백 추가
};
const LwpoCodeManagementDialog = memo(
@@ -46,19 +47,21 @@ const LwpoCodeManagementDialog = memo(
upCd,
children,
open,
onOpenChange,
onOpenChange,
onAfterSave, // 콜백 사용
...props
}: ManagementLwpoCodeDialogProps) => {
const isRegistration = !lwpoCd;
const { confirm } = useConfirm();
const alert = useAlertStore((state) => state.setMessage);
const alert = useAlertStore((state) => state.setMessage);
// 상세 데이터
const { data: detailData } = useQuery({
queryKey: [ENDPOINT.CMS_SERVICE.LWPO_CODE, upCd, lwpoCd],
queryFn: () => LwpoCodeApi.lwpoCodeDetail(upCd!, lwpoCd!),
enabled: !!upCd && !isRegistration,
enabled: !!upCd && !isRegistration && open,
});
const { data: upCodeData } = useQuery({
queryKey: [ENDPOINT.CMS_SERVICE.UP_CODE, upCd],
@@ -68,16 +71,22 @@ const LwpoCodeManagementDialog = memo(
const { mutateAsync: createLwpoCode } = useMutationWrapper({
mutationFn: (data: LwpoCodeFormData) => LwpoCodeApi.createLwpoCode(data),
onSuccess: () => {
alert("등록되었습니다.", () => onOpenChange?.(false));
onSuccess: async () => {
alert("등록되었습니다.", () => {
onOpenChange?.(false);
if (onAfterSave) onAfterSave();
});
},
});
const { mutateAsync: updateLwpoCode } = useMutationWrapper({
mutationFn: (data: LwpoCodeFormData) =>
LwpoCodeApi.updateLwpoCode(upCd!, lwpoCd!, data),
onSuccess: () => {
alert("수정되었습니다.", () => onOpenChange?.(false));
onSuccess: async () => {
alert("수정되었습니다.", () => {
onOpenChange?.(false);
if (onAfterSave) onAfterSave();
});
},
});
@@ -95,16 +104,40 @@ const LwpoCodeManagementDialog = memo(
delYn: "N",
},
});
const onSubmit =
(callback: (data: LwpoCodeFormData) => void) =>
async (data: LwpoCodeFormData) => {
const confirmMessage = isRegistration
? "등록하시겠습니까?"
: "수정하시겠습니까?";
if (await confirm(confirmMessage)) callback(data);
};
const handleRegister = async () => {
if (!upCd) {
alert("상위코드가 없습니다.");
return;
}
const confirmMessage = "등록하시겠습니까?";
if (await confirm(confirmMessage)) {
const rawValues = form.getValues();
const values = {
...rawValues,
upCd: upCd,
sortSeq: Number(rawValues.sortSeq) || 0,
};
await createLwpoCode(values);
}
};
const handleSave = async () => {
if (!upCd) {
alert("상위코드가 없습니다.");
return;
}
const confirmMessage = "수정하시겠습니까?";
if (await confirm(confirmMessage)) {
const rawValues = form.getValues();
const values = {
...rawValues,
upCd: upCd,
sortSeq: Number(rawValues.sortSeq) || 0,
};
await updateLwpoCode(values);
}
};
return (
<Dialog open={open} onOpenChange={onOpenChange} {...props}>
@@ -117,9 +150,14 @@ const LwpoCodeManagementDialog = memo(
</DialogHeader>
<Form {...form}>
<form
onSubmit={form.handleSubmit(
onSubmit(isRegistration ? createLwpoCode : updateLwpoCode),
)}
onSubmit={(e) => {
e.preventDefault();
if (isRegistration) {
handleRegister();
} else {
handleSave();
}
}}
className="space-y-4"
>
<Table variant="secondary">
@@ -292,7 +330,11 @@ const LwpoCodeManagementDialog = memo(
</TableBody>
</Table>
<DialogFooter className="!justify-center">
<Button size="lg" type="submit">
<Button
size="lg"
type="submit"
onClick={isRegistration ? handleRegister : handleSave}
>
{isRegistration ? "등록" : "저장"}
</Button>
<DialogClose asChild>
+14 -5
View File
@@ -5,7 +5,7 @@ import { Button, Input, Label } from "@common/components";
import { LwpoCodeApi, type LwpoCodeParams } from "@/entities/code";
import { ENDPOINT } from "@/shared/config";
import { useAlertStore, useDataTable } from "@/shared/lib";
import { columns } from "../model/lwpocode-table-columns";
import { getColumns } from "../model/lwpocode-table-columns";
import { LwpoCodeManagementDialog } from "./lwpocode-dialog";
type LwpoCodeSectionProps = {
@@ -24,7 +24,7 @@ function LwpoCodeSection({ upCd }: LwpoCodeSectionProps) {
pageIndex: 0,
pageSize: 10,
});
const query = useQuery({
queryKey: [ENDPOINT.CMS_SERVICE.LWPO_CODE, upCd, searchParams, pagination],
queryFn: () =>
@@ -34,10 +34,17 @@ function LwpoCodeSection({ upCd }: LwpoCodeSectionProps) {
page: pagination.pageIndex,
size: pagination.pageSize,
}),
enabled: !!upCd,
enabled: !!upCd ,
placeholderData: keepPreviousData,
});
});
// 등록/수정 후 목록 재조회 콜백
const handleAfterSave = () => {
query.refetch();
};
const columns = getColumns(handleAfterSave);
const { mutateAsync: codeRefreshAll } = useMutation({
mutationFn: LwpoCodeApi.codeRefreshAll,
onSuccess: () => {
@@ -117,7 +124,9 @@ function LwpoCodeSection({ upCd }: LwpoCodeSectionProps) {
</Button>
<LwpoCodeManagementDialog
open={dialogOpen}
onOpenChange={setDialogOpen}
upCd={upCd}
onOpenChange={setDialogOpen}
onAfterSave={handleAfterSave}
>
<Button>등록</Button>
</LwpoCodeManagementDialog>