From 42d042d92cfc82490b2b9fdaed0ed2935150f184 Mon Sep 17 00:00:00 2001 From: Choi JunHo Date: Wed, 7 Aug 2024 02:09:44 +0900 Subject: [PATCH 1/5] Feat: check question answer store --- .../admin/check-question-answer-store.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/store/admin/check-question-answer-store.ts diff --git a/src/store/admin/check-question-answer-store.ts b/src/store/admin/check-question-answer-store.ts new file mode 100644 index 0000000..69c272b --- /dev/null +++ b/src/store/admin/check-question-answer-store.ts @@ -0,0 +1,40 @@ +import { create } from 'zustand'; + +export interface QuestionAnswerState { + id: number; + content: string; + viewCount: string; + isChecked: boolean; + answer: { + id: number; + content: string; + }; +} + +interface CheckQuestionAnswerState { + questionData: QuestionAnswerState[]; + updateQuestionData: (firstData: QuestionAnswerState[]) => void; + inputQuestionData: (data: QuestionAnswerState) => void; + updateCheck: (id: number, isChecked: boolean) => void; + updateAnswer: (answerId: number, answerContent: string) => void; +} + +const useCheckQuestionAnswerStore = create((set) => ({ + questionData: [], + updateQuestionData: (firstData) => set({ questionData: firstData }), + inputQuestionData: (data) => set((state) => ({ questionData: [...state.questionData, data] })), + updateCheck: (id, isChecked) => + set((state) => ({ + questionData: state.questionData.map((question) => (question.id === id ? { ...question, isChecked } : question)), + })), + updateAnswer: (answerId, answerContent) => + set((state) => ({ + questionData: state.questionData.map((question) => + question.answer.id === answerId + ? { ...question, answer: { ...question.answer, content: answerContent } } + : question, + ), + })), +})); + +export default useCheckQuestionAnswerStore; From afff2382ab43996f5586695613fe0b3822784106 Mon Sep 17 00:00:00 2001 From: Choi JunHo Date: Wed, 7 Aug 2024 02:10:09 +0900 Subject: [PATCH 2/5] Refactor: edit-modal & question-check add store --- src/ui/components/admin/modal/edit-modal.tsx | 40 ++++++++-------- src/ui/pages/admin/question-check.tsx | 48 ++++---------------- 2 files changed, 30 insertions(+), 58 deletions(-) diff --git a/src/ui/components/admin/modal/edit-modal.tsx b/src/ui/components/admin/modal/edit-modal.tsx index c4b27b7..ab11af0 100644 --- a/src/ui/components/admin/modal/edit-modal.tsx +++ b/src/ui/components/admin/modal/edit-modal.tsx @@ -3,29 +3,26 @@ import TextArea from 'antd/es/input/TextArea'; import React, { useState } from 'react'; import { AdminEditAnswer } from '../../../../api/admin-edit-answer'; import { AdminCheckQuestionAnswer } from '../../../../api/admin-check-question-answer'; +import useCheckQuestionAnswerStore from '../../../../store/admin/check-question-answer-store'; interface CustomModalProps { open: boolean; setOpen: React.Dispatch>; - modalTitle: string; - modalContent: string; - modalContentId: number; questionId: number; - isChecked: boolean; } -const EditModal = ({ - open, - setOpen, - modalTitle, - modalContent, - modalContentId, - questionId, - isChecked, -}: CustomModalProps) => { +const EditModal = ({ open, setOpen, questionId }: CustomModalProps) => { + const { questionData, updateCheck, updateAnswer } = useCheckQuestionAnswerStore(); + const question = questionData.find((question) => question.id === questionId); + + if (!question) { + return null; + } + + const { content: modalTitle, answer, isChecked } = question; const [editStatus, setEditStatus] = useState(false); const [loading, setLoading] = useState(false); - const [content, setContent] = useState(modalContent); + const [content, setContent] = useState(answer.content); const executeWithLoading = async (action: () => Promise) => { setLoading(true); @@ -41,8 +38,12 @@ const EditModal = ({ const handleEditSubmit = async () => { try { await executeWithLoading(async () => { - await AdminEditAnswer(modalContentId, content); - await AdminCheckQuestionAnswer({ questionId, check: true }); + await AdminEditAnswer(answer.id, content); + updateAnswer(answer.id, content); + if (!isChecked) { + await AdminCheckQuestionAnswer({ questionId, check: true }); + updateCheck(questionId, true); + } }); } catch (err) { setEditStatus(false); @@ -51,8 +52,10 @@ const EditModal = ({ }; const handleCheckToggle = async () => { + const newCheckStatus = !isChecked; await executeWithLoading(async () => { - await AdminCheckQuestionAnswer({ questionId, check: !isChecked }); + await AdminCheckQuestionAnswer({ questionId, check: newCheckStatus }); + updateCheck(questionId, newCheckStatus); }); }; @@ -86,7 +89,6 @@ const EditModal = ({ 질문-답변 미확인 상태 변경 ), - editStatus ? (