forked from mingjm3/recip_elf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomePage.js
79 lines (75 loc) · 2.23 KB
/
HomePage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { React, useState, useContext } from "react";
import "./HomePage.css";
import "./components/Elfbar";
import Elfbar from "./components/Elfbar";
import { IngredientsContext } from "./components/IngredientsProvider";
import { UserProfileContext } from "./components/auth/UserProfileProvider";
function HomePage() {
const { ingredients } = useContext(IngredientsContext);
const { server, token, dietaryRestrictions } = useContext(UserProfileContext);
const [cuisine, setCuisine] = useState("american");
const [chatResponse, setChatResponse] = useState("");
const [toggle, setToggle] = useState(false);
const handleGenerate = async (e) => {
e.preventDefault();
setToggle(true);
if (chatResponse) {
setChatResponse("");
}
// send ingredients to backend
const body = {
foods: ingredients,
cuisine,
dietaryRestrictions,
};
const res = await fetch(`${server}/recipegen`, {
method: "POST",
body: JSON.stringify(body),
headers: {
"content-type": "application/json",
authorization: `Bearer ${token}`,
},
});
const data = await res.json();
setChatResponse(data.response);
};
return (
<div className="home">
<div>
<Elfbar />
</div>
<div className="line-4"></div>
<div className="prompt">
<h1>Generate your customized recipes according to your grocery!</h1>
<p>
Click Generate to Recieve Chat GPT Prompt. Train the AI Bot with the
Message
</p>
<div className="g">
<form
onSubmit={(e) => {
handleGenerate(e);
}}
>
<label>
Cuisine:
<input
name="cuisine"
type="text"
placeholder="name of cuisine"
value={cuisine}
onChange={(e) => setCuisine(e.target.value)}
/>
</label>
<button className="b mb-5" type="submit">
Generate
</button>
</form>
{toggle && <p className="gText">{chatResponse || "loading"}</p>}
</div>
<p className="generator"></p>
</div>
</div>
);
}
export default HomePage;