2020-03-21 18:24:10 +01:00
|
|
|
import { Fragment, useState, useEffect, useRef, useCallback } from 'react';
|
2020-03-23 21:22:41 +01:00
|
|
|
import { useRouter } from 'next/router';
|
2020-03-21 23:54:08 +01:00
|
|
|
import HeadTag from '../../components/HeadTag';
|
|
|
|
import FunctionCard from '../../components/FunctionCard/FunctionCard';
|
|
|
|
import Loader from '../../components/Loader';
|
|
|
|
import '../../public/css/pages/functions.css';
|
2020-03-24 09:54:50 +01:00
|
|
|
import api from '../../utils/api';
|
2020-03-21 23:54:08 +01:00
|
|
|
import useAPI from '../../hooks/useAPI';
|
2020-03-19 16:13:51 +01:00
|
|
|
|
2020-03-20 18:26:55 +01:00
|
|
|
const Functions = () => {
|
|
|
|
|
2020-03-23 21:22:41 +01:00
|
|
|
const { categoryId } = useRouter().query;
|
|
|
|
|
2020-03-20 23:35:51 +01:00
|
|
|
// State de recherche et de catégories
|
2020-03-21 16:43:37 +01:00
|
|
|
const [, categories] = useAPI('/categories');
|
2020-03-24 09:54:50 +01:00
|
|
|
const [inputSearch, setInputSearch] = useState({ search: "", selectedCategory: categoryId || "0" });
|
2020-03-20 18:26:55 +01:00
|
|
|
|
2020-03-20 23:35:51 +01:00
|
|
|
// State pour afficher les fonctions
|
2020-03-21 16:43:37 +01:00
|
|
|
const [functionsData, setFunctionsData] = useState({ hasMore: true, rows: [] });
|
2020-03-20 18:26:55 +01:00
|
|
|
const [isLoadingFunctions, setLoadingFunctions] = useState(true);
|
|
|
|
const [pageFunctions, setPageFunctions] = useState(1);
|
|
|
|
|
2020-03-21 18:24:10 +01:00
|
|
|
// Permet la pagination au scroll
|
|
|
|
const observer = useRef();
|
|
|
|
const lastFunctionCardRef = useCallback((node) => {
|
|
|
|
if (isLoadingFunctions) return;
|
|
|
|
if (observer.current) observer.current.disconnect();
|
|
|
|
observer.current = new IntersectionObserver((entries) => {
|
|
|
|
if (entries[0].isIntersecting && functionsData.hasMore) {
|
|
|
|
setPageFunctions(pageFunctions + 1);
|
|
|
|
}
|
|
|
|
}, { threshold: 1 });
|
|
|
|
if (node) observer.current.observe(node);
|
|
|
|
}, [isLoadingFunctions, functionsData.hasMore]);
|
|
|
|
|
2020-03-21 16:43:37 +01:00
|
|
|
const getFunctionsData = () => {
|
|
|
|
setLoadingFunctions(true);
|
|
|
|
return new Promise(async (next) => {
|
|
|
|
const result = await api.get(`/functions?page=${pageFunctions}&limit=10&categoryId=${inputSearch.selectedCategory}&search=${inputSearch.search}`);
|
|
|
|
setLoadingFunctions(false);
|
|
|
|
next(result.data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:22:41 +01:00
|
|
|
// Récupère la catégorie avec la query categoryId
|
|
|
|
useEffect(() => {
|
|
|
|
if (categoryId) {
|
|
|
|
handleChange({ target: { name: "selectedCategory", value: categoryId } });
|
|
|
|
}
|
|
|
|
}, [categoryId]);
|
|
|
|
|
2020-03-20 23:35:51 +01:00
|
|
|
// Récupère les fonctions si la page change
|
2020-03-20 18:26:55 +01:00
|
|
|
useEffect(() => {
|
2020-03-21 16:43:37 +01:00
|
|
|
getFunctionsData().then((data) => setFunctionsData({
|
|
|
|
hasMore: data.hasMore,
|
|
|
|
rows: [...functionsData.rows, ...data.rows]
|
|
|
|
}));
|
2020-03-20 18:26:55 +01:00
|
|
|
}, [pageFunctions]);
|
2020-03-19 16:13:51 +01:00
|
|
|
|
2020-03-20 23:35:51 +01:00
|
|
|
// Récupère les fonctions si la catégorie/recherche change
|
|
|
|
useEffect(() => {
|
2020-03-21 16:43:37 +01:00
|
|
|
getFunctionsData().then((data) => setFunctionsData(data));
|
2020-03-20 23:35:51 +01:00
|
|
|
}, [inputSearch.selectedCategory, inputSearch.search]);
|
|
|
|
|
|
|
|
const handleChange = (event) => {
|
|
|
|
const inputSearchNew = { ...inputSearch };
|
|
|
|
inputSearchNew[event.target.name] = event.target.value;
|
|
|
|
setInputSearch(inputSearchNew);
|
|
|
|
}
|
|
|
|
|
2020-03-20 18:26:55 +01:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<HeadTag
|
|
|
|
title="Fonctions"
|
|
|
|
description="Liste des fonctions."
|
|
|
|
image="/images/FunctionProject_icon_small.png"
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="container text-center">
|
|
|
|
<div className="row justify-content-center">
|
|
|
|
<h1 className="Functions__title">Fonctions</h1>
|
|
|
|
</div>
|
2020-03-21 16:43:37 +01:00
|
|
|
|
2020-03-20 18:26:55 +01:00
|
|
|
<div className="Functions__search-container row justify-content-center">
|
2020-03-25 18:22:03 +01:00
|
|
|
<select name="selectedCategory" value={inputSearch.selectedCategory} onChange={handleChange} className="Functions__select Functions__form-control">
|
2020-03-20 18:26:55 +01:00
|
|
|
<option value="0">Toutes catégories</option>
|
|
|
|
{categories.map((category) => (
|
|
|
|
<option key={category.id} value={category.id} className="Functions__select-option" style={{ backgroundColor: category.color }}>{category.name}</option>
|
|
|
|
))}
|
|
|
|
</select>
|
2020-03-25 18:22:03 +01:00
|
|
|
<input value={inputSearch.search} onChange={handleChange} type="search" className="Functions__form-control Functions__search-input" name="search" id="search" placeholder="🔎 Rechercher..."></input>
|
2020-03-20 18:26:55 +01:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="row justify-content-center">
|
2020-03-21 18:24:10 +01:00
|
|
|
{functionsData.rows.map((f, index) => {
|
|
|
|
// Si c'est le dernier élément
|
|
|
|
if (functionsData.rows.length === index + 1) {
|
2020-03-23 14:42:26 +01:00
|
|
|
return <FunctionCard ref={lastFunctionCardRef} key={f.id} slug={f.slug} image={f.image} title={f.title} description={f.description} category={f.categorie} publicationDate={new Date(f.createdAt).toLocaleDateString('fr-FR')} type={f.type} />;
|
2020-03-21 18:24:10 +01:00
|
|
|
}
|
2020-03-23 14:42:26 +01:00
|
|
|
return <FunctionCard key={f.id} slug={f.slug} image={f.image} title={f.title} description={f.description} category={f.categorie} publicationDate={new Date(f.createdAt).toLocaleDateString('fr-FR')} type={f.type} />;
|
2020-03-21 18:24:10 +01:00
|
|
|
})}
|
2020-03-20 18:26:55 +01:00
|
|
|
</div>
|
2020-03-21 23:03:30 +01:00
|
|
|
{isLoadingFunctions && <Loader />}
|
2020-03-19 16:13:51 +01:00
|
|
|
</div>
|
2020-03-20 18:26:55 +01:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
2020-03-19 16:13:51 +01:00
|
|
|
|
|
|
|
export default Functions;
|