260821_01_FST: First Commit
This commit is contained in:
134
src/app/(pages)/(main)/admin/settings/PopupFormDialog.tsx
Normal file
134
src/app/(pages)/(main)/admin/settings/PopupFormDialog.tsx
Normal file
@@ -0,0 +1,134 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { apiError } from "@kri/common/http";
|
||||
import { KriDialog } from "@/components/common/KriDialog";
|
||||
import { KriDateTimePicker } from "@/components/common/KriDateTimePicker";
|
||||
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 { useCreatePopupMutation, usePopupDetailQuery, useUpdatePopupMutation } from "@/query/popup";
|
||||
import type { Popup } from "@/types/popup";
|
||||
|
||||
export interface PopupFormDialogProps {
|
||||
popupId: number | null;
|
||||
onSaved: () => void;
|
||||
}
|
||||
|
||||
const toDatetimeLocal = (value?: string) => (value ? value.slice(0, 16) : "");
|
||||
|
||||
export function PopupFormDialog({ popupId, onSaved }: PopupFormDialogProps) {
|
||||
const { isDialogOpen, hideDialog } = useDialog();
|
||||
const open = isDialogOpen("popupForm");
|
||||
const isEdit = popupId !== null;
|
||||
|
||||
const centersQuery = useCentersQuery();
|
||||
const detailQuery = usePopupDetailQuery(isEdit ? popupId : null);
|
||||
const createMutation = useCreatePopupMutation();
|
||||
const updateMutation = useUpdatePopupMutation(popupId ?? 0);
|
||||
|
||||
const [title, setTitle] = useState("");
|
||||
const [contentHtml, setContentHtml] = useState("");
|
||||
const [centerCds, setCenterCds] = useState<string[]>([]);
|
||||
const [startDt, setStartDt] = useState("");
|
||||
const [endDt, setEndDt] = useState("");
|
||||
|
||||
const resetFrom = (popup: Popup | undefined) => {
|
||||
setTitle(popup?.title ?? "");
|
||||
setContentHtml(popup?.contentHtml ?? "");
|
||||
setCenterCds((popup?.centerCds ?? []).map(String));
|
||||
setStartDt(toDatetimeLocal(popup?.startDt));
|
||||
setEndDt(toDatetimeLocal(popup?.endDt));
|
||||
};
|
||||
|
||||
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("내용을 입력해 주세요.");
|
||||
if (!startDt || !endDt) return toast.error("팝업 기간을 입력해 주세요.");
|
||||
if (startDt > endDt) return toast.error("시작일은 종료일보다 늦을 수 없습니다.");
|
||||
|
||||
const request = {
|
||||
title: title.trim(),
|
||||
contentHtml,
|
||||
centerCds: centerCds.map(Number),
|
||||
startDt: `${startDt}:00`,
|
||||
endDt: `${endDt}:00`,
|
||||
};
|
||||
|
||||
try {
|
||||
if (isEdit) await updateMutation.mutateAsync(request);
|
||||
else await createMutation.mutateAsync(request);
|
||||
toast.success(isEdit ? "팝업을 수정했습니다." : "팝업을 등록했습니다.");
|
||||
hideDialog("popupForm");
|
||||
onSaved();
|
||||
} catch (cause) {
|
||||
toast.error(apiError(cause, { fallbackMessage: "저장에 실패했습니다." }).message);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<KriDialog
|
||||
open={open}
|
||||
onOpenChange={(next) => !next && hideDialog("popupForm")}
|
||||
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("popupForm")}>취소</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="grid w-150 grid-cols-2 gap-3">
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm font-medium">시작</label>
|
||||
<KriDateTimePicker value={startDt} onChange={setStartDt} modalPopover={false} />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm font-medium">종료</label>
|
||||
<KriDateTimePicker value={endDt} onChange={setEndDt} modalPopover={false} />
|
||||
</div>
|
||||
</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>
|
||||
</div>
|
||||
</KriDialog>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user