260821_01_FST: First Commit
This commit is contained in:
124
src/app/(pages)/(main)/notices/NoticeFormDialog.tsx
Normal file
124
src/app/(pages)/(main)/notices/NoticeFormDialog.tsx
Normal file
@@ -0,0 +1,124 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { apiError } from "@kri/common/http";
|
||||
import { FileAttachmentField } from "@/components/common/FileAttachmentField";
|
||||
import { KriDialog } from "@/components/common/KriDialog";
|
||||
import { MultiSelect } from "@/components/common/multi-select";
|
||||
import { KriEditor } from "@/components/kri-editor";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { useDialog } from "@/hooks/useDialog";
|
||||
import { useCentersQuery } from "@/query/center";
|
||||
import { useCreateNoticeMutation, useNoticeDetailQuery, useUpdateNoticeMutation } from "@/query/notice";
|
||||
import type { Notice } from "@/types/notice";
|
||||
|
||||
export interface NoticeFormDialogProps {
|
||||
noticeId: number | null;
|
||||
onSaved: () => void;
|
||||
}
|
||||
|
||||
export function NoticeFormDialog({ noticeId, onSaved }: NoticeFormDialogProps) {
|
||||
const { isDialogOpen, hideDialog } = useDialog();
|
||||
const open = isDialogOpen("noticeForm");
|
||||
const isEdit = noticeId !== null;
|
||||
|
||||
const centersQuery = useCentersQuery();
|
||||
const detailQuery = useNoticeDetailQuery(isEdit ? noticeId : null);
|
||||
const createMutation = useCreateNoticeMutation();
|
||||
const updateMutation = useUpdateNoticeMutation(noticeId ?? 0);
|
||||
|
||||
const [title, setTitle] = useState("");
|
||||
const [contentHtml, setContentHtml] = useState("");
|
||||
const [centerCds, setCenterCds] = useState<string[]>([]);
|
||||
const [keptFileIds, setKeptFileIds] = useState<number[]>([]);
|
||||
const [newFiles, setNewFiles] = useState<File[]>([]);
|
||||
|
||||
const resetFrom = (notice: Notice | undefined) => {
|
||||
setTitle(notice?.title ?? "");
|
||||
setContentHtml(notice?.contentHtml ?? "");
|
||||
setCenterCds((notice?.centerCds ?? []).map(String));
|
||||
setKeptFileIds((notice?.files ?? []).map((file) => file.fileId));
|
||||
setNewFiles([]);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
resetFrom(isEdit ? detailQuery.data : undefined);
|
||||
}, [open, isEdit, detailQuery.data]);
|
||||
|
||||
const centerOptions = (centersQuery.data ?? [])
|
||||
.filter((center) => center.centerCd !== 0)
|
||||
.map((center) => ({ label: center.centerName, value: String(center.centerCd) }));
|
||||
|
||||
const saving = createMutation.isPending || updateMutation.isPending;
|
||||
|
||||
const handleSave = async () => {
|
||||
if (!title.trim()) return toast.error("제목을 입력해 주세요.");
|
||||
if (!contentHtml.trim()) return toast.error("내용을 입력해 주세요.");
|
||||
|
||||
const request = {
|
||||
title: title.trim(),
|
||||
contentHtml,
|
||||
centerCds: centerCds.map(Number),
|
||||
...(isEdit ? { keepFileIds: keptFileIds } : {}),
|
||||
};
|
||||
|
||||
try {
|
||||
if (isEdit) await updateMutation.mutateAsync({ request, files: newFiles });
|
||||
else await createMutation.mutateAsync({ request, files: newFiles });
|
||||
toast.success(isEdit ? "공지사항을 수정했습니다." : "공지사항을 등록했습니다.");
|
||||
hideDialog("noticeForm");
|
||||
onSaved();
|
||||
} catch (cause) {
|
||||
toast.error(apiError(cause, { fallbackMessage: "저장에 실패했습니다." }).message);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<KriDialog
|
||||
open={open}
|
||||
onOpenChange={(next) => !next && hideDialog("noticeForm")}
|
||||
title={isEdit ? "공지사항 수정" : "공지사항 등록"}
|
||||
contentClassName="w-[calc(100vw-2rem)] max-w-[84rem] sm:max-w-[84rem] max-h-[calc(100vh-2rem)]"
|
||||
bodyClassName="min-w-0 overflow-x-hidden"
|
||||
footer={
|
||||
<>
|
||||
<Button type="button" variant="outline" onClick={() => hideDialog("noticeForm")}>취소</Button>
|
||||
<Button type="button" onClick={() => void handleSave()} disabled={saving}>{saving ? "저장 중..." : "저장"}</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<div className="min-w-0 space-y-4">
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm font-medium">공지 대상</label>
|
||||
<MultiSelect
|
||||
className="w-200 ml-2"
|
||||
modalPopover={false}
|
||||
options={centerOptions}
|
||||
defaultValue={centerCds}
|
||||
onValueChange={setCenterCds}
|
||||
placeholder="전체 (선택 안 함)"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm font-medium">제목</label>
|
||||
<Input value={title} onChange={(event) => setTitle(event.currentTarget.value)} maxLength={128} />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm font-medium">내용</label>
|
||||
<KriEditor value={contentHtml} onChange={setContentHtml} />
|
||||
</div>
|
||||
<FileAttachmentField
|
||||
existingFiles={detailQuery.data?.files ?? []}
|
||||
keptFileIds={keptFileIds}
|
||||
onKeptFileIdsChange={setKeptFileIds}
|
||||
newFiles={newFiles}
|
||||
onNewFilesChange={setNewFiles}
|
||||
downloadPath={(fileId) => `/api/notices/${noticeId}/files/${fileId}`}
|
||||
/>
|
||||
</div>
|
||||
</KriDialog>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user