-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from MARU-EGG/refactor-maru-egg-page-component
[USER] refactor: Chatform components
- Loading branch information
Showing
9 changed files
with
217 additions
and
157 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// src/hooks/useChatForm.ts | ||
import { useState, useCallback } from 'react'; | ||
import { useAutoComplete } from './use-auto-complete.hooks'; | ||
import useChatStore from '../store/chat-store'; | ||
import { postQuestion } from '../api/post-question'; | ||
import { SearchById } from '../api/admin-search-by-id'; | ||
import useTypeStore from '../store/type-category-store'; | ||
|
||
const useChatForm = () => { | ||
const [content, setContent] = useState<string>(''); | ||
const [selectedId, setSelectedId] = useState<number | undefined>(undefined); | ||
const [disabled, setDisabled] = useState(false); | ||
const [autoOpen, setAutoOpen] = useState(false); | ||
const { results } = useAutoComplete({ content }); | ||
const { type, category } = useTypeStore(); | ||
|
||
const handleChange = useCallback((value: string) => { | ||
setContent(value); | ||
setAutoOpen(true); | ||
setSelectedId(undefined); | ||
}, []); | ||
|
||
const handleSelect = useCallback((value: string, id: number) => { | ||
setSelectedId(id); | ||
setContent(value); | ||
setDisabled(value.trim() === ''); | ||
setAutoOpen(false); | ||
}, []); | ||
|
||
const handleSubmit = useCallback( | ||
async (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
try { | ||
useChatStore.getState().addMessage({ content, role: 'user' }); | ||
useChatStore.getState().addMessage({ content: '답변을 생성중입니다...', role: 'system' }); | ||
useChatStore.getState().setLoading(true); | ||
setContent(''); | ||
setAutoOpen(false); | ||
setDisabled(true); | ||
if (selectedId === undefined) { | ||
const response = await postQuestion(category, type, content); | ||
useChatStore.getState().updateLastMessage(response.answer.content); | ||
useChatStore.getState().setLoading(false); | ||
setDisabled(false); | ||
} else { | ||
const response = await SearchById(selectedId); | ||
useChatStore.getState().updateLastMessage(response.answer.content); | ||
useChatStore.getState().setLoading(false); | ||
setSelectedId(undefined); | ||
setDisabled(false); | ||
} | ||
} catch (error) { | ||
useChatStore.getState().setLoading(false); | ||
useChatStore.getState().updateLastMessage('답변 생성에 실패했습니다. 새로고침해주세요'); | ||
} | ||
}, | ||
[content, selectedId, category, type], | ||
); | ||
|
||
return { | ||
content, | ||
autoOpen, | ||
disabled, | ||
results, | ||
handleChange, | ||
handleSelect, | ||
handleSubmit, | ||
}; | ||
}; | ||
|
||
export default useChatForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// src/ui/components/organism/chat-section/chat-section.tsx | ||
import React from 'react'; | ||
import ChatCard from '../../atom/chat-card/chat-card'; | ||
import PresetButton from '../../atom/preset/preset-button'; | ||
import useTypeStore, { TypeCategoryState } from '../../../../store/type-category-store'; | ||
import useChatStore from '../../../../store/chat-store'; | ||
|
||
const ChatSection: React.FC = () => { | ||
const { setSelectedType, type, setSelectedCategory, category } = useTypeStore(); | ||
const { messages } = useChatStore(); | ||
const [selectedTypeButton, setSelectedTypeButton] = React.useState<TypeCategoryState['type']>(undefined); | ||
const [selectedCategoryButton, setSelectedCategoryButton] = React.useState<TypeCategoryState['category']>(undefined); | ||
|
||
const handleTypeButtonClick = (selectedType: TypeCategoryState['type']) => { | ||
setSelectedType(selectedType); | ||
setSelectedTypeButton(selectedType); | ||
}; | ||
|
||
const handleCategoryButtonClick = (selectedCategory: TypeCategoryState['category']) => { | ||
setSelectedCategory(selectedCategory); | ||
setSelectedCategoryButton(selectedCategory); | ||
}; | ||
|
||
return ( | ||
<div className="max-h-screen-minus-header w-full overflow-y-auto px-4 pb-24 pt-16"> | ||
<ChatCard | ||
content={`안녕하세요 입학처 챗봇 MARU-EGG입니다! | ||
궁금하신 내용 안내 도와드리겠습니다. | ||
알아보고 싶은 전형을 선택해주세요!`} | ||
role="system" | ||
/> | ||
<div className="flex space-x-2"> | ||
<PresetButton onClick={() => handleTypeButtonClick('SUSI')} isSelected={selectedTypeButton === 'SUSI'}> | ||
수시 | ||
</PresetButton> | ||
<PresetButton onClick={() => handleTypeButtonClick('PYEONIP')} isSelected={selectedTypeButton === 'PYEONIP'}> | ||
편입 | ||
</PresetButton> | ||
<PresetButton onClick={() => handleTypeButtonClick('JEONGSI')} isSelected={selectedTypeButton === 'JEONGSI'}> | ||
정시 | ||
</PresetButton> | ||
</div> | ||
{type !== undefined && ( | ||
<ChatCard role="user" content={type === 'SUSI' ? '수시' : type === 'JEONGSI' ? '정시' : '편입'} /> | ||
)} | ||
{type !== undefined && ( | ||
<> | ||
<ChatCard content={`알고싶은 내용을 선택해주세요`} role="system" /> | ||
<div className="flex w-2/5 flex-wrap space-y-2"> | ||
<PresetButton | ||
onClick={() => handleCategoryButtonClick('ADMISSION_GUIDELINE')} | ||
isSelected={selectedCategoryButton === 'ADMISSION_GUIDELINE'} | ||
> | ||
모집관련내용 | ||
</PresetButton> | ||
<PresetButton | ||
onClick={() => handleCategoryButtonClick('PASSING_RESULT')} | ||
isSelected={selectedCategoryButton === 'PASSING_RESULT'} | ||
> | ||
전년도 입시결과 | ||
</PresetButton> | ||
<PresetButton | ||
onClick={() => handleCategoryButtonClick('PAST_QUESTIONS')} | ||
isSelected={selectedCategoryButton === 'PAST_QUESTIONS'} | ||
> | ||
면접등 기출문제 | ||
</PresetButton> | ||
</div> | ||
</> | ||
)} | ||
|
||
{category !== undefined && ( | ||
<ChatCard | ||
role="user" | ||
content={ | ||
category === 'ADMISSION_GUIDELINE' | ||
? '모집관련내용' | ||
: category === 'PASSING_RESULT' | ||
? '전년도 입시결과' | ||
: '면접 등 기출문제' | ||
} | ||
/> | ||
)} | ||
{type !== undefined && category !== undefined && ( | ||
<ChatCard | ||
role="system" | ||
content={`안녕하세요 입학처 챗봇 MARU-EGG입니다! | ||
궁금하신 내용 안내 도와드리겠습니다. | ||
`} | ||
/> | ||
)} | ||
{messages.map((msg, index) => { | ||
return <ChatCard key={index} content={msg.content} role={msg.role} />; | ||
})} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ChatSection; |
Oops, something went wrong.