From 9a28a51eb051d1c01ba7ffd3f457f5f419be27c9 Mon Sep 17 00:00:00 2001 From: designsoo Date: Sun, 28 Apr 2024 02:35:34 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=EC=A7=88=EB=AC=B8=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=ED=9B=84=20input=EC=97=90=20=EA=B7=B8=EB=8C=80?= =?UTF-8?q?=EB=A1=9C=20=EC=A7=88=EB=AC=B8=EC=9D=B4=20=EB=82=A8=EC=95=84?= =?UTF-8?q?=EC=9E=88=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/commons/chatbot/Chatbot.tsx | 7 +++++-- .../chatbot/data-access/useRequestAnswer.ts | 16 +++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/components/commons/chatbot/Chatbot.tsx b/src/components/commons/chatbot/Chatbot.tsx index 13f228ff..13d2c50e 100644 --- a/src/components/commons/chatbot/Chatbot.tsx +++ b/src/components/commons/chatbot/Chatbot.tsx @@ -31,16 +31,19 @@ export const Chatbot = () => { const handleUserInput = (event: ChangeEvent) => setQuestion(event.target.value); - const { chatbotAnswerMutation } = useRequestAnswer({ setChatStore, setLoading, setQuestion, question }); + const { chatbotAnswerMutation } = useRequestAnswer({ setChatStore, setLoading }); const handleSubmitUserInput = (event: MouseEvent) => { event.preventDefault(); setLoading(true); if (!question) return; + const currentQuestion = question; + setChatStore((prev) => [...prev, { question, questionDate: getCurrentTime(), answer: '', answerDate: '' }]); - chatbotAnswerMutation(question); + chatbotAnswerMutation(currentQuestion); + setQuestion(''); }; const scrollToBottom = () => { diff --git a/src/components/commons/chatbot/data-access/useRequestAnswer.ts b/src/components/commons/chatbot/data-access/useRequestAnswer.ts index c967fb78..b6e0ab84 100644 --- a/src/components/commons/chatbot/data-access/useRequestAnswer.ts +++ b/src/components/commons/chatbot/data-access/useRequestAnswer.ts @@ -10,26 +10,24 @@ import { Chat } from '@/types'; export type useRequestAnswerProps = { setChatStore: Dispatch>; setLoading: Dispatch>; - setQuestion: Dispatch>; - question: string; }; -export const useRequestAnswer = ({ setChatStore, setLoading, setQuestion, question }: useRequestAnswerProps) => { +export const useRequestAnswer = ({ setChatStore, setLoading }: useRequestAnswerProps) => { const { mutate: chatbotAnswerMutation } = useMutation({ mutationFn: getChatbotAnswer, onSuccess(res) { const { answer } = res.data; + setChatStore((prev: Chat[]) => { const updatedChatStore = [...prev]; - const index = updatedChatStore.findIndex((chat) => chat.question === question); - if (index >= 0) { - updatedChatStore[index].answer = answer; - updatedChatStore[index].answerDate = getCurrentTime(); - } + + updatedChatStore[updatedChatStore.length - 1].answer = answer; + updatedChatStore[updatedChatStore.length - 1].answerDate = getCurrentTime(); + return updatedChatStore; }); + setLoading(false); - setQuestion(''); }, onError(error) { console.error('Error submitting user input:', error); From 43a00b23eea2cfff9627fb37bfc96fd630c760cc Mon Sep 17 00:00:00 2001 From: designsoo Date: Sun, 28 Apr 2024 02:48:23 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=EC=A4=91=EB=B3=B5=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B0=9C=EC=83=9D=ED=95=98=EB=8A=94=20currentIndex?= =?UTF-8?q?=20=EB=B3=80=EC=88=98=20=EC=84=A0=EC=96=B8=20=EB=B0=8F=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/chatbot/data-access/useRequestAnswer.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/components/commons/chatbot/data-access/useRequestAnswer.ts b/src/components/commons/chatbot/data-access/useRequestAnswer.ts index b6e0ab84..d82b20e2 100644 --- a/src/components/commons/chatbot/data-access/useRequestAnswer.ts +++ b/src/components/commons/chatbot/data-access/useRequestAnswer.ts @@ -20,9 +20,15 @@ export const useRequestAnswer = ({ setChatStore, setLoading }: useRequestAnswerP setChatStore((prev: Chat[]) => { const updatedChatStore = [...prev]; - - updatedChatStore[updatedChatStore.length - 1].answer = answer; - updatedChatStore[updatedChatStore.length - 1].answerDate = getCurrentTime(); + const currentChatIndex = updatedChatStore.length - 1; + + if (currentChatIndex >= 0) { + updatedChatStore[currentChatIndex] = { + ...updatedChatStore[currentChatIndex], + answer, + answerDate: getCurrentTime(), + }; + } return updatedChatStore; });