Skip to content

Commit

Permalink
fix: autoSearch data type undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
swgvenghy committed Aug 19, 2024
1 parent 4fc41e5 commit 7dc047a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/hooks/use-auto-complete.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState } from 'react';
import useDebouce from './use-debounce.hooks';
import { searchAutoComplete } from '../api/search-auto-complete';
import useTypeStore from '../store/type-category-store';

interface UseAutoCompleteProps {
content: string;
Expand All @@ -15,6 +16,7 @@ export interface AutoCompleteResult {
export const useAutoComplete = ({ content, delay = 300 }: UseAutoCompleteProps) => {
const [results, setResults] = useState<AutoCompleteResult[]>([]);
const [error, setError] = useState<string | null>(null);
const { type, category } = useTypeStore();

const debouncedContent = useDebouce(content, delay);

Expand All @@ -24,7 +26,7 @@ export const useAutoComplete = ({ content, delay = 300 }: UseAutoCompleteProps)
setError(null);

try {
const response = await searchAutoComplete(debouncedContent);
const response = await searchAutoComplete(debouncedContent, type, category);
setResults(response.data);
} catch (err) {
setError('자동완성 api 호출 실패');
Expand Down
8 changes: 4 additions & 4 deletions src/store/type-category-store.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { create } from 'zustand';

interface TypeCategoryState {
type: null | 'SUSI' | 'PYEONIP' | 'JEONGSI';
category: null | 'ADMISSION_GUIDELINE' | 'PASSING_RESULT' | 'PAST_QUESTIONS' | 'INTERVIEW_PRACTICAL_TEST';
type: undefined | 'SUSI' | 'PYEONIP' | 'JEONGSI';
category: undefined | 'ADMISSION_GUIDELINE' | 'PASSING_RESULT' | 'PAST_QUESTIONS' | 'INTERVIEW_PRACTICAL_TEST';
setSelectedType: (button: TypeCategoryState['type']) => void;
setSelectedCategory: (button: TypeCategoryState['category']) => void;
}

const useTypeStore = create<TypeCategoryState>()((set) => ({
type: null,
category: null,
type: undefined,
category: undefined,
setSelectedType: (button) => set({ type: button }),
setSelectedCategory: (button) => set({ category: button }),
}));
Expand Down
2 changes: 1 addition & 1 deletion src/ui/components/molecule/header/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import IconButton from '../../atom/icon/icon-button';
import MenuDrawer from '../menu-drawer/menu-drawer';

interface HeaderProps {
type: null | 'SUSI' | 'PYEONIP' | 'JEONGSI';
type: undefined | 'SUSI' | 'PYEONIP' | 'JEONGSI';
}

const Header = ({ type }: HeaderProps) => {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/components/molecule/menu-drawer/menu-drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface MenuDrawerProps {
}

const MenuDrawer = ({ open, onClose }: MenuDrawerProps) => {
const [selectedButton, setSelectedButton] = React.useState<'SUSI' | 'PYEONIP' | 'JEONGSI' | null>(null);
const [selectedButton, setSelectedButton] = React.useState<'SUSI' | 'PYEONIP' | 'JEONGSI' | undefined>(undefined);
const { type } = useTypeStore();

useEffect(() => {
Expand Down
24 changes: 16 additions & 8 deletions src/ui/pages/maru-egg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import PresetButton from '../components/atom/preset/preset-button';
const MaruEgg: React.FC = () => {
const { setSelectedType, type, setSelectedCategory, category } = useTypeStore();
const { messages } = useChatStore();
const [selectedTypeButton, setSelectedTypeButton] = React.useState<'SUSI' | 'PYEONIP' | 'JEONGSI' | null>(null);
const [selectedTypeButton, setSelectedTypeButton] = React.useState<'SUSI' | 'PYEONIP' | 'JEONGSI' | undefined>(
undefined,
);
const [selectedCategoryButton, setSelectedCategoryButton] = React.useState<
null | 'ADMISSION_GUIDELINE' | 'PASSING_RESULT' | 'PAST_QUESTIONS' | 'INTERVIEW_PRACTICAL_TEST'
>(null);
undefined | 'ADMISSION_GUIDELINE' | 'PASSING_RESULT' | 'PAST_QUESTIONS' | 'INTERVIEW_PRACTICAL_TEST'
>(undefined);

const handleTypeButtonClick = (selectedType: 'SUSI' | 'PYEONIP' | 'JEONGSI') => {
setSelectedType(selectedType);
Expand Down Expand Up @@ -53,10 +55,10 @@ const MaruEgg: React.FC = () => {
정시
</PresetButton>
</div>
{type !== null && (
{type !== undefined && (
<ChatCard role="user" content={type === 'SUSI' ? '수시' : type === 'JEONGSI' ? '정시' : '편입'} />
)}
{type !== null && (
{type !== undefined && (
<>
<ChatCard content={`알고싶은 내용을 선택해주세요`} role="system" />
<div className="flex flex-wrap space-x-2 space-y-1">
Expand All @@ -78,11 +80,17 @@ const MaruEgg: React.FC = () => {
>
면접등 기출문제
</PresetButton>
<PresetButton
onClick={() => handleCategoryButtonClick('INTERVIEW_PRACTICAL_TEST')}
isSelected={selectedCategoryButton === 'INTERVIEW_PRACTICAL_TEST'}
>
실기관련
</PresetButton>
</div>
</>
)}

{category !== null && (
{category !== undefined && (
<ChatCard
role="user"
content={
Expand All @@ -96,7 +104,7 @@ const MaruEgg: React.FC = () => {
}
/>
)}
{type !== null && category !== null && (
{type !== undefined && category !== undefined && (
<ChatCard
role="system"
content={`안녕하세요 입학처 챗봇 MARU-EGG입니다!
Expand All @@ -108,7 +116,7 @@ const MaruEgg: React.FC = () => {
return <ChatCard key={index} content={msg.content} role={msg.role} />;
})}
</div>
{type !== null && category !== null && (
{type !== undefined && category !== undefined && (
<div className="absolute bottom-0 w-full rounded-bl-2xl rounded-br-2xl bg-white px-3 py-3">
<ChatForm type={type} category={category} />
</div>
Expand Down

0 comments on commit 7dc047a

Please sign in to comment.