diff --git a/components/header.js b/components/header.js index 79af7c0..55e55b1 100644 --- a/components/header.js +++ b/components/header.js @@ -7,6 +7,7 @@ const menu = [ { url: "/behaviours", label: "Comportements utilisateur" }, { url: "/pages", label: "Statistiques de visite" }, { url: "/funnel", label: "Tunnel de conversion" }, + { url: "/institutions", label: "Institutions" }, ] function closeMenu() { diff --git a/hooks/fetch-institutions-data.js b/hooks/fetch-institutions-data.js new file mode 100644 index 0000000..5c35b0f --- /dev/null +++ b/hooks/fetch-institutions-data.js @@ -0,0 +1,31 @@ +import { useEffect, useState } from "react" + +import TableService from "../services/tableService.js" + +const useFetchInstitutionsData = () => { + const [dataService, setDataService] = useState(null) + const [loading, setLoading] = useState(true) + const [error, setError] = useState(null) + + useEffect(() => { + fetch(process.env.aidesJeunesStatisticsURL) + .then((response) => { + if (!response.ok) { + throw new Error("Network response was not ok") + } + return response.json() + }) + .then((data) => { + setDataService(new TableService(data.institutions)) + setLoading(false) + }) + .catch((error) => { + setError(error) + setLoading(false) + }) + }, []) + + return { dataService, loading, error } +} + +export default useFetchInstitutionsData diff --git a/pages/institutions.js b/pages/institutions.js new file mode 100644 index 0000000..dd630ac --- /dev/null +++ b/pages/institutions.js @@ -0,0 +1,180 @@ +import { useState, useEffect } from "react" + +import useFetchInstitutionsData from "../hooks/fetch-institutions-data" + +const defaultSortOrder = "desc" +const allType = "Tous" + +const filterData = ( + dataService, + queryName, + queryCode, + selectedType, + sortKey, + sortOrder, +) => { + let filters = {} + + if (queryName) { + filters.name = queryName + } + + if (queryCode) { + filters.code = queryCode + } + + if (selectedType && selectedType !== allType) { + filters.type = selectedType + } + + let results = dataService.filterByKeys(filters).getResult() + + if (sortKey) { + results = dataService.sortByKey(sortKey, sortOrder).getResult() + } + + return results +} + +const InstitutionsStats = () => { + const { dataService, loading, error } = useFetchInstitutionsData() + const [results, setResults] = useState([]) + + const [queryName, setQueryName] = useState("") + const [queryCode, setQueryCode] = useState("") + const [selectedType, setSelectedType] = useState(allType) + const [sortKey, setSortKey] = useState(null) + const [sortOrder, setSortOrder] = useState(defaultSortOrder) + + useEffect(() => { + if (!dataService) { + return + } + + setResults( + filterData( + dataService, + queryName, + queryCode, + selectedType, + sortKey, + sortOrder, + ), + ) + }, [queryName, queryCode, selectedType, sortKey, sortOrder, dataService]) + + const handleHeaderClick = (newSortKey) => { + if (newSortKey === sortKey) { + setSortOrder((prevSortOrder) => + prevSortOrder === "asc" ? "desc" : "asc", + ) + } else { + setSortKey(newSortKey) + setSortOrder(defaultSortOrder) + } + } + + if (loading) { + return
Loading...
+ } + + if (error) { + returnError: {error.message}
+ } + + return ( + <> +Filtrer par nom
+ setQueryName(e.target.value)} + /> +Filtrer par code
+ setQueryCode(e.target.value)} + /> +Filtrer par type
+ +
+ Nom
+ |
+
+ Code
+ |
+
+ Type
+ |
+
+ handleHeaderClick("population")}
+ >
+ Population
+
+ |
+
+ handleHeaderClick("simulationCount")}
+ >
+ Nombre de simulation
+
+ |
+
+ handleHeaderClick("benefitCount")}
+ >
+ Nombre d'aides
+
+ |
+
---|---|---|---|---|---|
{row.name} | +{row.code} | +{row.type} | +{row.population} | +{row.simulationCount} | +{row.benefitCount} | +