Skip to content

Commit

Permalink
migrating chatbot context to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
tahmid-saj committed Oct 4, 2024
1 parent 5ad7d34 commit 84c1ead
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
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<string> => {
if (validateChatBotMessageInput(messageInput)) return chatbotResponse

const res = await getChatBotResponse(messageInput)
return res
}

// initial state
export const ChatBotContext = createContext({
export const ChatBotContext = createContext<ChatbotContextType>({
chatbotResponse: "",
getChatbotResponse: () => {}
})

// chatbot provider
export const ChatBotProvider = ({ children }) => {
const [chatbotResponse, setChatBotResponse] = useState("")
export const ChatBotProvider: FC<ChatbotProviderProps> = ({ children }) => {
const [chatbotResponse, setChatBotResponse] = useState<string>("")

const getChatbotResponse = async (messageInput) => {
const getChatbotResponse = async (messageInput: string): Promise<void> => {
const resChatBot = await getChatbotResponseHelper(chatbotResponse, messageInput)
setChatBotResponse(resChatBot)
}
Expand Down
14 changes: 14 additions & 0 deletions src/contexts/shared/chatbot/chatbot.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

// chatbot types

import { ReactNode } from "react";

export interface ChatbotContextType {
chatbotResponse: string;

getChatbotResponse: (messageInput: string) => void;
}

export interface ChatbotProviderProps {
children: ReactNode;
}

0 comments on commit 84c1ead

Please sign in to comment.