From c6116b9da1c44b87ffe3a02f11f7786d226484ea Mon Sep 17 00:00:00 2001 From: Divlo Date: Fri, 20 Mar 2020 23:35:51 +0100 Subject: [PATCH] =?UTF-8?q?frontend:=20Trier=20par=20cat=C3=A9gorie=20et?= =?UTF-8?q?=20recherche?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/pages/functions.js | 53 +++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/frontend/pages/functions.js b/frontend/pages/functions.js index c8654c2..fb6c7b5 100644 --- a/frontend/pages/functions.js +++ b/frontend/pages/functions.js @@ -7,8 +7,11 @@ import api from '../config/api'; const Functions = () => { - const [categories, setCategories] = useState([]); + // State de recherche et de catégories + const [categories, setCategories] = useState([]); + const [inputSearch, setInputSearch] = useState({ search: "", selectedCategory: "0" }); + // State pour afficher les fonctions const [functions, setFunctions] = useState([]); const [isLoadingFunctions, setLoadingFunctions] = useState(true); const [pageFunctions, setPageFunctions] = useState(1); @@ -21,11 +24,11 @@ const Functions = () => { setCategories(result.data); }) .catch((error) => console.error(error)); - }, []); + }, []); - // Récupère les fonctions + // Récupère les fonctions si la page change useEffect(() => { - api.get(`/functions?page=${pageFunctions}&limit=10`) + api.get(`/functions?page=${pageFunctions}&limit=10&categoryId=${inputSearch.selectedCategory}&search=${inputSearch.search}`) .then((result) => { setLoadingFunctions(false); sethasMoreFunctions(result.data.hasMore); @@ -34,11 +37,29 @@ const Functions = () => { .catch((error) => console.error(error)); }, [pageFunctions]); + // Récupère les fonctions si la catégorie/recherche change + useEffect(() => { + api.get(`/functions?page=${pageFunctions}&limit=10&categoryId=${inputSearch.selectedCategory}&search=${inputSearch.search}`) + .then((result) => { + setLoadingFunctions(false); + sethasMoreFunctions(result.data.hasMore); + setFunctions(result.data.rows); + }) + .catch((error) => console.error(error)); + }, [inputSearch.selectedCategory, inputSearch.search]); + const loadMore = () => { setLoadingFunctions(true); setPageFunctions(pageFunctions + 1); } + const handleChange = (event) => { + setLoadingFunctions(true); + const inputSearchNew = { ...inputSearch }; + inputSearchNew[event.target.name] = event.target.value; + setInputSearch(inputSearchNew); + } + return ( {
- {categories.map((category) => ( ))} - +
- {functions.map((f) => ( ))} - - { - !isLoadingFunctions && hasMoreFunctions - ? - - : !hasMoreFunctions ? - null - : -

Chargement...

- }
+ { + !isLoadingFunctions && hasMoreFunctions + ? + + : !hasMoreFunctions ? + null + : +

Chargement...

+ }
);