La recherche fonctionne avec les filtres et le tris dans le ui.

This commit is contained in:
Victor Turgeon 2022-10-30 13:44:57 -07:00
parent 8a5b00624e
commit 81ff4ad4f7
2 changed files with 37 additions and 41 deletions

View File

@ -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) => {

View File

@ -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);
}