Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions frontend/src/hooks/useHomeStats.ts
Original file line number Diff line number Diff line change
@@ -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<HomeStatsData> = 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,
};
}
17 changes: 15 additions & 2 deletions frontend/src/pages/home/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useHomeStats } from "../../hooks/useHomeStats";
import { useCategoryCounts } from "../../hooks/useCategoryCounts";

import "./HomePage.css";
Expand Down Expand Up @@ -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
// ==========================================
Expand Down Expand Up @@ -72,7 +81,11 @@ function HomePage({ onSearch, onCategorySelect }: HomePageProps) {
</SearchField>

<p className="hero__stats">
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`}
</p>

<CartIllustration />
Expand Down