260821_01_FST: First Commit
This commit is contained in:
121
src/app/(pages)/(main)/qna/QnaAnswerSection.tsx
Normal file
121
src/app/(pages)/(main)/qna/QnaAnswerSection.tsx
Normal file
@@ -0,0 +1,121 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { apiError } from "@kri/common/http";
|
||||
import { FileAttachmentField } from "@/components/common/FileAttachmentField";
|
||||
import { KriEditor } from "@/components/kri-editor";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { useCreateQnaAnswerMutation, useDeleteQnaAnswerMutation, useUpdateQnaAnswerMutation } from "@/query/qna";
|
||||
import type { QnaAnswer } from "@/types/qna";
|
||||
|
||||
export interface QnaAnswerSectionProps {
|
||||
questionId: number;
|
||||
answer: QnaAnswer | null;
|
||||
admin: boolean;
|
||||
onChanged: () => void;
|
||||
}
|
||||
|
||||
export function QnaAnswerSection({ questionId, answer, admin, onChanged }: QnaAnswerSectionProps) {
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [contentHtml, setContentHtml] = useState("");
|
||||
const [keptFileIds, setKeptFileIds] = useState<number[]>([]);
|
||||
const [newFiles, setNewFiles] = useState<File[]>([]);
|
||||
|
||||
const createMutation = useCreateQnaAnswerMutation(questionId);
|
||||
const updateMutation = useUpdateQnaAnswerMutation(questionId);
|
||||
const deleteMutation = useDeleteQnaAnswerMutation(questionId);
|
||||
|
||||
useEffect(() => {
|
||||
setContentHtml(answer?.contentHtml ?? "");
|
||||
setKeptFileIds((answer?.files ?? []).map((file) => file.fileId));
|
||||
setNewFiles([]);
|
||||
setEditing(false);
|
||||
}, [answer]);
|
||||
|
||||
const canModify = admin && !answer?.viewed;
|
||||
const saving = createMutation.isPending || updateMutation.isPending;
|
||||
|
||||
const handleSave = async () => {
|
||||
if (!contentHtml.trim()) return toast.error("답변 내용을 입력해 주세요.");
|
||||
const request = { contentHtml, ...(answer ? { keepFileIds: keptFileIds } : {}) };
|
||||
try {
|
||||
if (answer) await updateMutation.mutateAsync({ request, files: newFiles });
|
||||
else await createMutation.mutateAsync({ request, files: newFiles });
|
||||
toast.success("답변을 저장했습니다.");
|
||||
setEditing(false);
|
||||
onChanged();
|
||||
} catch (cause) {
|
||||
const errorInfo = apiError(cause, { fallbackMessage: "답변 저장에 실패했습니다." });
|
||||
if (errorInfo.status === 409) toast.error("이미 확인된 답변은 수정할 수 없습니다.");
|
||||
else toast.error(errorInfo.message);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async () => {
|
||||
if (!window.confirm("답변을 삭제하시겠습니까?")) return;
|
||||
try {
|
||||
await deleteMutation.mutateAsync();
|
||||
toast.success("답변을 삭제했습니다.");
|
||||
onChanged();
|
||||
} catch (cause) {
|
||||
const errorInfo = apiError(cause, { fallbackMessage: "답변 삭제에 실패했습니다." });
|
||||
if (errorInfo.status === 409) toast.error("이미 확인된 답변은 삭제할 수 없습니다.");
|
||||
else toast.error(errorInfo.message);
|
||||
}
|
||||
};
|
||||
|
||||
if (!answer && !admin) {
|
||||
return <p className="text-sm text-muted-foreground">아직 등록된 답변이 없습니다.</p>;
|
||||
}
|
||||
|
||||
if (!answer && admin && !editing) {
|
||||
return (
|
||||
<Button type="button" variant="outline" onClick={() => setEditing(true)}>답변 작성</Button>
|
||||
);
|
||||
}
|
||||
|
||||
if (editing) {
|
||||
return (
|
||||
<div className="space-y-3 rounded-md border p-3">
|
||||
<KriEditor value={contentHtml} onChange={setContentHtml} />
|
||||
<FileAttachmentField
|
||||
existingFiles={answer?.files ?? []}
|
||||
keptFileIds={keptFileIds}
|
||||
onKeptFileIdsChange={setKeptFileIds}
|
||||
newFiles={newFiles}
|
||||
onNewFilesChange={setNewFiles}
|
||||
downloadPath={(fileId) => `/api/qna-questions/${questionId}/answer/files/${fileId}`}
|
||||
/>
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button type="button" variant="outline" onClick={() => setEditing(false)}>취소</Button>
|
||||
<Button type="button" onClick={() => void handleSave()} disabled={saving}>{saving ? "저장 중..." : "저장"}</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-3 rounded-md border p-3">
|
||||
<div className="text-sm text-muted-foreground">
|
||||
답변자: {answer?.regUser} · {answer?.regDt?.slice(0, 10)}
|
||||
</div>
|
||||
<div dangerouslySetInnerHTML={{ __html: answer?.contentHtml ?? "" }} />
|
||||
<FileAttachmentField
|
||||
existingFiles={answer?.files ?? []}
|
||||
keptFileIds={(answer?.files ?? []).map((file) => file.fileId)}
|
||||
onKeptFileIdsChange={() => {}}
|
||||
newFiles={[]}
|
||||
onNewFilesChange={() => {}}
|
||||
downloadPath={(fileId) => `/api/qna-questions/${questionId}/answer/files/${fileId}`}
|
||||
disabled
|
||||
/>
|
||||
{canModify ? (
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button type="button" variant="destructive" size="sm" onClick={() => void handleDelete()}>삭제</Button>
|
||||
<Button type="button" variant="outline" size="sm" onClick={() => setEditing(true)}>수정</Button>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user