refactor: move states into array

-remove states chatPrompt and botMessage
-add these states as object in chatLog array
This commit is contained in:
sushant dhimal 2023-01-05 22:27:31 +05:45
parent 0825807cc8
commit 76fb98f113
2 changed files with 69 additions and 56 deletions

View File

@ -7,15 +7,17 @@ import NavPrompt from "./components/NavPrompt";
function App() { function App() {
const [showMenu, setShowMenu] = useState(false); const [showMenu, setShowMenu] = useState(false);
const [chatPrompt, setChatPrompt] = useState("Hello bot!");
const [botMessage, setBotMessage] = useState(
"Hey human! I am an AI. Tell me how can I help you."
);
const [inputPrompt, setInputPrompt] = useState(""); const [inputPrompt, setInputPrompt] = useState("");
const [chatLog, setChatLog] = useState([
{
chatPrompt: "Hello bot!",
botMessage: "Hey human! I am an AI. Tell me how can I help you.",
},
]);
const handleSubmit = (e) => { const handleSubmit = (e) => {
e.preventDefault(); e.preventDefault();
console.log("form runs"); setChatLog([...chatLog, { chatPrompt: inputPrompt }]);
async function callAPI() { async function callAPI() {
const response = await fetch("http://localhost:4000/", { const response = await fetch("http://localhost:4000/", {
method: "POST", method: "POST",
@ -23,9 +25,13 @@ function App() {
body: JSON.stringify({ message: inputPrompt }), body: JSON.stringify({ message: inputPrompt }),
}); });
const data = await response.json(); const data = await response.json();
console.log("data", data.botResponse); setChatLog([
setChatPrompt(inputPrompt); ...chatLog,
setBotMessage(data.botResponse); {
chatPrompt: inputPrompt,
botMessage: data.botResponse,
},
]);
} }
callAPI(); callAPI();
setInputPrompt(""); setInputPrompt("");
@ -54,7 +60,7 @@ function App() {
{showMenu && ( {showMenu && (
<nav> <nav>
<div className="navItems"> <div className="navItems">
<NewChat /> <NewChat setChatLog={setChatLog} />
<NavPrompt /> <NavPrompt />
</div> </div>
<div className="navCloseIcon"> <div className="navCloseIcon">
@ -75,12 +81,15 @@ function App() {
)} )}
<aside className="sideMenu"> <aside className="sideMenu">
<NewChat /> <NewChat setChatLog={setChatLog} />
<NavPrompt /> <NavPrompt />
</aside> </aside>
<section className="chatBox"> <section className="chatBox">
<div className="chatLog"> <div className="chatLogWrapper">
{chatLog.length > 0 &&
chatLog.map((chat, idx) => (
<div className="chatLog" key={idx}>
<div className="chatPromptMainContainer"> <div className="chatPromptMainContainer">
<div className="chatPromptWrapper"> <div className="chatPromptWrapper">
<Avatar bg="#5437DB" className="userSVG"> <Avatar bg="#5437DB" className="userSVG">
@ -100,9 +109,10 @@ function App() {
<circle cx={12} cy={7} r={4} /> <circle cx={12} cy={7} r={4} />
</svg> </svg>
</Avatar> </Avatar>
<div id="chatPrompt">{chatPrompt}</div> <div id="chatPrompt">{chat.chatPrompt}</div>
</div> </div>
</div> </div>
<div className="botMessageMainContainer"> <div className="botMessageMainContainer">
<div className="botMessageWrapper"> <div className="botMessageWrapper">
<Avatar bg="#11a27f" className="openaiSVG"> <Avatar bg="#11a27f" className="openaiSVG">
@ -120,10 +130,12 @@ function App() {
/> />
</svg> </svg>
</Avatar> </Avatar>
<div id="botMessage">{botMessage}</div> <div id="botMessage">{chat.botMessage}</div>
</div> </div>
</div> </div>
</div> </div>
))}
</div>
<form onSubmit={handleSubmit}> <form onSubmit={handleSubmit}>
<div className="inputPromptWrapper"> <div className="inputPromptWrapper">
@ -135,6 +147,7 @@ function App() {
rows="1" rows="1"
value={inputPrompt} value={inputPrompt}
onChange={(e) => setInputPrompt(e.target.value)} onChange={(e) => setInputPrompt(e.target.value)}
autoFocus
></input> ></input>
<p></p> <p></p>
</div> </div>

View File

@ -1,8 +1,8 @@
import React from "react"; import React from "react";
const NewChat = () => { const NewChat = ({ setChatLog }) => {
return ( return (
<div className="sideMenuButton"> <div className="sideMenuButton" onClick={() => setChatLog([])}>
<span>+</span> <span>+</span>
New chat New chat
</div> </div>