Skip to content

Commit

Permalink
(#79) ๐Ÿš€ feat: ์ฑ—๋ด‡ ํด๋กœ๋ฐ” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๊ตฌํ˜„ ๋ฐ input field์— ์—ฐ๋™
Browse files Browse the repository at this point in the history
  • Loading branch information
inaemon committed Nov 27, 2024
1 parent b27867d commit d148c40
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
34 changes: 31 additions & 3 deletions src/api/ChatbotApiHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

import { getGuide } from "./ChatbotApiService";
import { GuideGetParamsType } from "./ChatbotApiType";
import { postClova, getGuide } from "./ChatbotApiService";
import { ClovaPostParamsType, GuideGetParamsType } from "./ChatbotApiType";

import { getStadiumChatApiData } from "@/src/constants/ChatbotData";


export const handleGetGuideAnswer = async ({stadiumName, categoryName, orderNumber}: GuideGetParamsType) => {
try {
const stadiumApiData = getStadiumChatApiData(stadiumName);
Expand All @@ -17,7 +18,7 @@ export const handleGetGuideAnswer = async ({stadiumName, categoryName, orderNumb
}
);

console.log("์ฑ—๋ด‡ ๊ฐ€์ด๋“œ ๋ฐ์ดํ„ฐ ๋ฐ›์•˜๋‹น >> ", response);
//console.log("์ฑ—๋ด‡ ๊ฐ€์ด๋“œ ๋ฐ์ดํ„ฐ ๋ฐ›์•˜๋‹น >> ", response);

// ํŒŒ์‹ฑ
const data = response.payload;
Expand All @@ -35,4 +36,31 @@ export const handleGetGuideAnswer = async ({stadiumName, categoryName, orderNumb
} catch (error) {
console.error('์ฑ—๋ด‡ ๊ฐ€์ด๋“œ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:', error);
}
};


export const handleGetClovaAnswer = async ({message}: GuideGetParamsType) => {
try {
// API ์š”์ฒญ
const response = await postClova(
{
message: message,
}
);

console.log("์ฑ—๋ด‡ ํด๋กœ๋ฐ” ๋ฐ์ดํ„ฐ ๋ฐ›์•˜๋‹น >> ", response);

// ํŒŒ์‹ฑ
const data = response.payload;
const parsedData = {
answer: data.answer, // ํด๋กœ๋ฐ” ๋‹ต๋ณ€
};

//console.log(parsedData);

return parsedData;

} catch (error) {
console.error('์ฑ—๋ด‡ ํด๋กœ๋ฐ” ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:', error);
}
};
47 changes: 44 additions & 3 deletions src/pages/Chatbot/components/input/ChatbotInputField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@ import FAQCategoryButton from "./FAQCategoryButton";
// ์™ธ๋ถ€ ํด๋ฆญ์‹œ ๋‹ซํžˆ๋Š” ์ด๋ฒคํŠธ ํ›…
import { useOutsideClick } from "@/src/hooks/useOutsideClick";

import { handleGetClovaAnswer } from "@/src/api/ChatbotApiHandler";

interface Props {
isStadiumSelected: boolean; // boolean ๊ฐ’์„ props๋กœ ๋ฐ›์Œ
onSelect: (category: string) => void;
onResponseUpdate: (answer: string) => void;
}

const ChatbotInputField = ({isStadiumSelected, onSelect}: Props) => {
const ChatbotInputField = ({isStadiumSelected, onSelect, onResponseUpdate}: Props) => {
const [isFAQCategoryVisible, setIsFAQCategoryVisible] = useState(false);
const [inputClovaMessage, setInputClovaMessage] = useState(""); // ํด๋กœ๋ฐ”์—๊ฒŒ ๋ณด๋‚ผ ๋ฉ”์‹œ์ง€


// ์™ธ๋ถ€ ํด๋ฆญ ์‹œ ์‹คํ–‰๋  ํ•จ์ˆ˜
// FAQCategory ์ปดํฌ๋„ŒํŠธ ์™ธ๋ถ€ ํด๋ฆญ ์‹œ ์นดํ…Œ๊ณ ๋ฆฌ๋ฐ” ๋‹ซ๊ธฐ
const closeFAQCategory = () => {
setIsFAQCategoryVisible(false);
};

// FAQCategory ์ปดํฌ๋„ŒํŠธ ๊ฐ์ง€๋ฅผ ์œ„ํ•œ ref
const categoryRef = useOutsideClick(closeFAQCategory);

// ์นดํ…Œ๊ณ ๋ฆฌ๋ฐ” or ์นดํ…Œ๊ณ ๋ฆฌ ๋ฒ„ํŠผ์„ ํ† ๊ธ€๋กœ ๋ Œ๋”๋ง
const renderFAQCategory = () => {
if(isFAQCategoryVisible) {
return (
Expand All @@ -34,6 +41,35 @@ const ChatbotInputField = ({isStadiumSelected, onSelect}: Props) => {
}
};

// ์ „์†ก ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ํด๋กœ๋ฐ”์—๊ฒŒ ๋ฉ”์‹œ์ง€ ์ „์†ก
const handleSendButton = () => {
// ๋นˆ ๋ฉ”์‹œ์ง€์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์Œ
if (inputClovaMessage.trim() === "") {
return;
}

async () => {
try {
// ๋ฉ”์‹œ์ง€๋ฅผ API ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „์†ก ๋ฐ ์‘๋‹ต ๋ฐ›๊ธฐ
const response = await handleGetClovaAnswer({
inputClovaMessage,
});

if (!response) {
return; // response๊ฐ€ ์—†์œผ๋ฉด ์ข…๋ฃŒ
}

// ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์— ์—…๋ฐ์ดํŠธ
onResponseUpdate(
response.answer ?? "",
);

} catch (error) {
//alert("API ํ˜ธ์ถœ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.");
}
}
};

return (
<>
{/* ํ”Œ๋Ÿฌ์Šค ๋ฒ„ํŠผ ๋ฆฌ์ŠคํŠธ */}
Expand All @@ -47,9 +83,14 @@ const ChatbotInputField = ({isStadiumSelected, onSelect}: Props) => {
type="text"
placeholder="์ฑ—๋ด‡์—๊ฒŒ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ"
className="border-none bg-transparent text-grayscale-60 text-xs font-medium outline-none px-2"
value={inputClovaMessage} // input ๊ฐ’ ์ดˆ๊ธฐํ™”
onChange={(e) => setInputClovaMessage(e.target.value)} // input ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ๋•Œ ์ƒํƒœ ์—…๋ฐ์ดํŠธ
/>
</div>
<button className="ml-2 bg-main-50 rounded-full p-2 flex items-center justify-center text-xxs hover:bg-mainhover-50">
<button
className="ml-2 bg-main-50 rounded-full p-2 flex items-center justify-center text-xxs hover:bg-mainhover-50"
onClick={handleSendButton}
>
<Image src={chatbotClickIcon} alt="๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ ์•„์ด์ฝ˜" width={20} height={20} />
</button>
</div>
Expand Down

0 comments on commit d148c40

Please sign in to comment.