basic search (to be changed)

This commit is contained in:
Victor Turgeon 2022-10-29 14:17:59 -07:00
parent 9707ae8d12
commit 840178147a
2 changed files with 72 additions and 47 deletions

View File

@ -1,31 +1,33 @@
import { useState } from 'react'; import { useState } from 'react';
import { Navigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
async function fetchData(query, preview) {
const ResearchBar = () => {
const navigate = useNavigate();
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 response = await fetch(`https://localhost:7292/api/Search?query=${query}&preview=${preview}&deep=true`);
const json = await response.json(); const json = await response.json();
return { json }; return { json };
} }
function useInput(defaultValue) { const handleValueChange = async (value) => {
const [value, setValue] = useState(defaultValue); setValue(value);
function onChange(e) {
setValue(e.target.value);
if (value.length > 1)
fetchData(value, true);
}
return {
value,
onChange,
};
} }
const ResearchBar = () => {
const input = useInput();
return ( return (
<div className="research-container"> <div className="research-container">
<input className="research-input" placeholder="Rechercher..." {...input}></input> <input className="research-input" value={value} onChange={(e) => handleValueChange(e.target.value)} placeholder="Rechercher..."></input>
<button className="research-btn" /*onClick={fetchData(input.value, false)}*/> <button className="research-btn" onClick={() => search(value)}>
<div className="fa fa-search" /> <div className="fa fa-search" />
</button> </button>
</div> </div>

View File

@ -4,19 +4,28 @@ import ItemList from "../components/ItemList";
import { useState } from 'react'; import { useState } from 'react';
import Sorting from "../components/Sorting" import Sorting from "../components/Sorting"
import Filters from '../components/Filters'; import Filters from '../components/Filters';
import { useLocation } from 'react-router-dom';
const Morceaux = (startingProducts) => { const Morceaux = () => {
const [products, setProducts] = useState([]); const [products, setProducts] = useState([]);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(true);
const [order, setOrder] = useState(""); const [order, setOrder] = useState("");
const [filterPrice, setFilterPrice] = useState(""); const [filterPrice, setFilterPrice] = useState("");
const [filterState, setFilterState] = useState(""); const [filterState, setFilterState] = useState("");
const [isSearch, setIsSearch] = useState(false);
const { state } = useLocation();
useEffect(() => { useEffect(() => {
document.title = 'Morceaux'; document.title = 'Morceaux';
setIsLoading(true); setIsLoading(true);
if (state != null) {
setProducts([...state.searchResults.json]);
setIsSearch(true);
}
else {
async function fetchData() { async function fetchData() {
const response = await fetch(`https://localhost:7292/api/Inventory`); const response = await fetch(`https://localhost:7292/api/Inventory`);
const json = await response.json(); const json = await response.json();
@ -24,6 +33,8 @@ const Morceaux = (startingProducts) => {
setProducts([...json]); setProducts([...json]);
} }
fetchData(); fetchData();
}
setIsLoading(false); setIsLoading(false);
}, []); }, []);
@ -49,6 +60,10 @@ const Morceaux = (startingProducts) => {
const handleOrder = async (sortBy) => { const handleOrder = async (sortBy) => {
if (isSearch) {
}
else {
setOrder(sortBy); setOrder(sortBy);
var url = `https://localhost:7292/api/Inventory?order=${sortBy}&filterPrice=${filterPrice}&filterState=${filterState}`; var url = `https://localhost:7292/api/Inventory?order=${sortBy}&filterPrice=${filterPrice}&filterState=${filterState}`;
setIsLoading(true); setIsLoading(true);
@ -58,8 +73,14 @@ const Morceaux = (startingProducts) => {
setProducts([...json]); setProducts([...json]);
setIsLoading(false); setIsLoading(false);
} }
}
const handleFilters = async (price, state) => { const handleFilters = async (price, state) => {
if (isSearch) {
}
{
setFilterPrice(price); setFilterPrice(price);
setFilterState(state); setFilterState(state);
var url = `https://localhost:7292/api/Inventory?order=${order}&filterPrice=${price}&filterState=${state}`; var url = `https://localhost:7292/api/Inventory?order=${order}&filterPrice=${price}&filterState=${state}`;
@ -70,6 +91,8 @@ const Morceaux = (startingProducts) => {
setIsLoading(false); setIsLoading(false);
} }
}
return ( return (
<div className="morceaux"> <div className="morceaux">
<div className="morceaux-options-container"> <div className="morceaux-options-container">
@ -86,9 +109,9 @@ const Morceaux = (startingProducts) => {
<div className={isLoading ? "cat-load" : "d-none cat-load"} /> <div className={isLoading ? "cat-load" : "d-none cat-load"} />
<script type="text/javascript" async src="https://tenor.com/embed.js"></script> <script type="text/javascript" async src="https://tenor.com/embed.js"></script>
<div> <div>
<Button onClick={handleNextItems} className='btn-load-more'> {!isSearch && <Button onClick={handleNextItems} className='btn-load-more'>
... ...
</Button> </Button>}
</div> </div>
</div> </div>
); );