From 805972d0a8e2855584729bbc864a2a9cc1a4f02d Mon Sep 17 00:00:00 2001 From: Lynn Romich Date: Thu, 22 Feb 2024 21:49:59 +0100 Subject: [PATCH] Add frontend for search --- graphite-demo/frontend.jsx | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 graphite-demo/frontend.jsx diff --git a/graphite-demo/frontend.jsx b/graphite-demo/frontend.jsx new file mode 100644 index 0000000..dd6a2a3 --- /dev/null +++ b/graphite-demo/frontend.jsx @@ -0,0 +1,56 @@ +import React, { useEffect, useState } from 'react'; + +const TaskSearch = () => { + const [tasks, setTasks] = useState([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + const [searchQuery, setSearchQuery] = useState(''); + + useEffect(() => { + setLoading(true); + fetch(`/search?query=${encodeURIComponent(searchQuery)}`) + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); + }) + .then(data => { + setTasks(data); + setLoading(false); + }) + .catch(error => { + setError(error.message); + setLoading(false); + }); + }, [searchQuery]); // Depend on searchQuery + + if (loading) { + return
Loading...
; + } + + if (error) { + return
Error: {error}
; + } + + return ( +
+

Task Search

+ setSearchQuery(e.target.value)} + /> + +
+ ); +}; + +export default TaskSearch; \ No newline at end of file