From 84c1eadfc187d7c9f5d0d59a03f38ca615810d23 Mon Sep 17 00:00:00 2001 From: tahmid-saj Date: Thu, 3 Oct 2024 23:11:30 -0400 Subject: [PATCH] migrating chatbot context to ts --- .../{chatbot.context.js => chatbot.context.tsx} | 14 ++++++++------ src/contexts/shared/chatbot/chatbot.types.ts | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) rename src/contexts/shared/chatbot/{chatbot.context.js => chatbot.context.tsx} (57%) create mode 100644 src/contexts/shared/chatbot/chatbot.types.ts diff --git a/src/contexts/shared/chatbot/chatbot.context.js b/src/contexts/shared/chatbot/chatbot.context.tsx similarity index 57% rename from src/contexts/shared/chatbot/chatbot.context.js rename to src/contexts/shared/chatbot/chatbot.context.tsx index 0cf8e27..e8d1686 100644 --- a/src/contexts/shared/chatbot/chatbot.context.js +++ b/src/contexts/shared/chatbot/chatbot.context.tsx @@ -1,9 +1,11 @@ -import { useState, createContext } from "react"; +import { useState, createContext, FC } from "react"; import { validateChatBotMessageInput } from "../../../utils/validations/chatbot.validation"; import { getChatBotResponse } from "../../../utils/api-requests/chatbot.requests"; +import { ChatbotContextType, ChatbotProviderProps } from "./chatbot.types" + // helper functions -const getChatbotResponseHelper = async (chatbotResponse, messageInput) => { +const getChatbotResponseHelper = async (chatbotResponse: string, messageInput: string): Promise => { if (validateChatBotMessageInput(messageInput)) return chatbotResponse const res = await getChatBotResponse(messageInput) @@ -11,16 +13,16 @@ const getChatbotResponseHelper = async (chatbotResponse, messageInput) => { } // initial state -export const ChatBotContext = createContext({ +export const ChatBotContext = createContext({ chatbotResponse: "", getChatbotResponse: () => {} }) // chatbot provider -export const ChatBotProvider = ({ children }) => { - const [chatbotResponse, setChatBotResponse] = useState("") +export const ChatBotProvider: FC = ({ children }) => { + const [chatbotResponse, setChatBotResponse] = useState("") - const getChatbotResponse = async (messageInput) => { + const getChatbotResponse = async (messageInput: string): Promise => { const resChatBot = await getChatbotResponseHelper(chatbotResponse, messageInput) setChatBotResponse(resChatBot) } diff --git a/src/contexts/shared/chatbot/chatbot.types.ts b/src/contexts/shared/chatbot/chatbot.types.ts new file mode 100644 index 0000000..19fc637 --- /dev/null +++ b/src/contexts/shared/chatbot/chatbot.types.ts @@ -0,0 +1,14 @@ + +// chatbot types + +import { ReactNode } from "react"; + +export interface ChatbotContextType { + chatbotResponse: string; + + getChatbotResponse: (messageInput: string) => void; +} + +export interface ChatbotProviderProps { + children: ReactNode; +} \ No newline at end of file