diff --git a/frontend/src/hooks/useHomeStats.ts b/frontend/src/hooks/useHomeStats.ts new file mode 100644 index 0000000..0a205de --- /dev/null +++ b/frontend/src/hooks/useHomeStats.ts @@ -0,0 +1,30 @@ +import { gql } from "@apollo/client"; +import type { TypedDocumentNode } from "@apollo/client"; +import { useQuery } from "@apollo/client/react"; + +// Typene for dataene vi henter fra GraphQL. +type HomeStatsData = { + productsCount: number; + stores: { code: string }[]; +}; + +// Henter totalt antall produkter og tilgjengelige butikkjeder. +const HOME_STATS_QUERY: TypedDocumentNode = gql` + query HomeStats { + productsCount + stores { + code + } + } +`; + +export function useHomeStats() { + const { data, loading, error } = useQuery(HOME_STATS_QUERY); + + return { + productCount: data?.productsCount, + storeCount: data?.stores.length, + loading, + error, + }; +} \ No newline at end of file diff --git a/frontend/src/pages/home/HomePage.tsx b/frontend/src/pages/home/HomePage.tsx index 30ba085..b687ad1 100644 --- a/frontend/src/pages/home/HomePage.tsx +++ b/frontend/src/pages/home/HomePage.tsx @@ -1,3 +1,4 @@ +import { useHomeStats } from "../../hooks/useHomeStats"; import { useCategoryCounts } from "../../hooks/useCategoryCounts"; import "./HomePage.css"; @@ -34,13 +35,21 @@ type HomePageProps = { // ========================================== function HomePage({ onSearch, onCategorySelect }: HomePageProps) { - // Henter faktiske produktantall fra GraphQL. + // Henter produktantall per kategori. const { categories, loading: categoriesLoading, error: categoriesError, } = useCategoryCounts(); + // Henter totalt antall varer og butikkjeder. + const { + productCount, + storeCount, + loading: statsLoading, + error: statsError, + } = useHomeStats(); + // ========================================== // RENDER // ========================================== @@ -72,7 +81,11 @@ function HomePage({ onSearch, onCategorySelect }: HomePageProps) {

- 12 400 VARER · 6 KJEDER · OPPDATERT DAGLIG + {statsLoading + ? "HENTER STATISTIKK …" + : statsError || productCount == null || storeCount == null + ? "STATISTIKK IKKE TILGJENGELIG" + : `${productCount.toLocaleString("nb-NO")} VARER · ${storeCount} KJEDER · OPPDATERT DAGLIG`}