MCPcopy Create free account
hub / github.com/codemistic/Web-Development / Home

Function Home

Django_React_Todo/frontend/src/pages/Home.jsx:23–103  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

21 });
22
23function Home() {
24 const [lang, setLang] = useState("");
25 const [postID, setPostID] = useState(null);
26 const [postTitle, setPostTitle] = useState(null);
27
28 // Request for GET information
29 const { data: tasks, isLoading, isError,} = useQuery(["todos"], () => {
30 return fetch("http://127.0.0.1:8000/api/task-list/").then((t) => t.json());
31 });
32
33 // POST request to the server
34 const mutation = useMutation(
35 (body) => fetcher("http://127.0.0.1:8000/api/task-create/", body),
36 {
37 onSuccess(data) {
38 console.log("Got response from backend", data);
39 client.invalidateQueries("todos");
40 setLang(" ");
41 },
42 onError(error) {
43 console.log("Got error from backend", error);
44 },
45 }
46 );
47
48 if (isLoading) return <h1>Loading...</h1>;
49
50 if (isError) return <h1>Error with request</h1>;
51
52 // Calling POST Request
53 function callMutation() {
54 mutation.mutate({ title: lang });
55 }
56
57 // Setting PostID null and passing as props to Tasks.jsx so that it can be redirected to Home.jsx
58 function backButton() {
59 return setPostID(null);
60 }
61
62 // Passing the Tasks.jsx with PostID and PostTitle
63 function settingPost(task) {
64 setPostID(task.id);
65 setPostTitle(task.title);
66 }
67
68 // In the event that postID is not null, task components will be rendered
69 if (postID !== null) {
70 return (
71 <Tasks postID={postID} backButton={backButton} postTitle={postTitle} fetcher={fetcher} />
72 );
73 }
74
75 return (
76 <div className="Home_container">
77 <div className="Home_Add">
78 <div className="Home_task-input">
79 <input type="text" name="todo_title" value={lang} onChange={(e) => setLang(e.target.value)} placeholder="Add Todo..." />
80 </div>

Callers

nothing calls this directly

Calls 2

fetcherFunction · 0.85
settingPostFunction · 0.85

Tested by

no test coverage detected