diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/ResearchBar.js b/GrossesMitaines/grosses-mitaines-ui/src/components/ResearchBar.js index 691f15b..7783435 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/ResearchBar.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/ResearchBar.js @@ -9,15 +9,7 @@ const ResearchBar = () => { const [value, setValue] = useState(""); const search = async (query) => { - const results = await fetchData(query, false); - console.log(results); - navigate('/morceaux', { state: { searchResults: results } }); - } - - const fetchData = async (query, preview) => { - const response = await fetch(`https://localhost:7292/api/Search?query=${query}&preview=${preview}&deep=true`); - const json = await response.json(); - return { json }; + navigate('/morceaux', { state: { query: query} }); } const handleValueChange = async (value) => { diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Morceaux.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Morceaux.js index 614e5dc..e54d1e6 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Morceaux.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Morceaux.js @@ -14,6 +14,7 @@ const Morceaux = () => { const [filterPrice, setFilterPrice] = useState(""); const [filterState, setFilterState] = useState(""); const [isSearch, setIsSearch] = useState(false); + const [query, setQuery] = useState(""); const { state } = useLocation(); @@ -21,18 +22,26 @@ const Morceaux = () => { document.title = 'Morceaux'; setIsLoading(true); + + async function fetchData(isSearch) { + var url = ""; + + url = isSearch ? `https://localhost:7292/api/Search?query=${state.query}` : `https://localhost:7292/api/Inventory`; + + const response = await fetch(url); + const json = await response.json(); + if (json.length > 0) + setProducts([...json]); + } + + if (state != null) { - setProducts([...state.searchResults.json]); + setQuery(state.query); setIsSearch(true); + fetchData(true); } else { - async function fetchData() { - const response = await fetch(`https://localhost:7292/api/Inventory`); - const json = await response.json(); - if (json.length > 0) - setProducts([...json]); - } - fetchData(); + fetchData(false); } setIsLoading(false); @@ -60,36 +69,31 @@ const Morceaux = () => { const handleOrder = async (sortBy) => { - if (isSearch) { + setIsLoading(true); + setOrder(sortBy); - } - else { - setOrder(sortBy); - var url = `https://localhost:7292/api/Inventory?order=${sortBy}&filterPrice=${filterPrice}&filterState=${filterState}`; - setIsLoading(true); - const response = await fetch(url); - const json = await response.json(); - if (json.length > 0) - setProducts([...json]); - setIsLoading(false); - } + var url = isSearch ? `https://localhost:7292/api/Search?query=${query}&filterPrice=${filterPrice}&filterState=${filterState}&order=${sortBy}` + : `https://localhost:7292/api/Inventory?order=${sortBy}&filterPrice=${filterPrice}&filterState=${filterState}`; + + const response = await fetch(url); + const json = await response.json(); + setProducts([...json]); + setIsLoading(false); } const handleFilters = async (price, state) => { + setIsLoading(true); - if (isSearch) { + setFilterPrice(price); + setFilterState(state); - } - { - setFilterPrice(price); - setFilterState(state); - var url = `https://localhost:7292/api/Inventory?order=${order}&filterPrice=${price}&filterState=${state}`; - setIsLoading(true); - const response = await fetch(url); - const json = await response.json(); - setProducts([...json]); - setIsLoading(false); - } + var url = isSearch ? `https://localhost:7292/api/Search?query=${query}&filterPrice=${price}&filterState=${state}&order=${order}` + : `https://localhost:7292/api/Inventory?order=${order}&filterPrice=${price}&filterState=${state}`; + + const response = await fetch(url); + const json = await response.json(); + setProducts([...json]); + setIsLoading(false); }