From 2e5fbb6fa6af98b2876e63f78300772db5fc2731 Mon Sep 17 00:00:00 2001 From: Nikolai R Kristiansen Date: Sat, 19 Feb 2022 17:21:44 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Split=20out=20stats=20into=20multip?= =?UTF-8?q?le=20sub=20views?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- frontend/src/Routes.tsx | 4 + frontend/src/features/stats/OrderStats.tsx | 79 +++++++++++++ frontend/src/features/stats/ProductStats.tsx | 40 +++++++ frontend/src/features/stats/Stats.module.css | 3 + frontend/src/features/stats/Stats.tsx | 111 +------------------ frontend/src/features/stats/StatsView.tsx | 41 +++++++ 7 files changed, 171 insertions(+), 109 deletions(-) create mode 100644 frontend/src/features/stats/OrderStats.tsx create mode 100644 frontend/src/features/stats/ProductStats.tsx create mode 100644 frontend/src/features/stats/StatsView.tsx diff --git a/README.md b/README.md index 534526a..256d60d 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ npm start ## TODO - update emails -- Stats: subnav, products over time grouped by month compared to last month, move per product stats to subview +- Stats: products over time - Light on dark colors - inventory views diff --git a/frontend/src/Routes.tsx b/frontend/src/Routes.tsx index ff351e5..f2c9655 100644 --- a/frontend/src/Routes.tsx +++ b/frontend/src/Routes.tsx @@ -5,6 +5,8 @@ import { UserCreate } from "./features/auth/UserCreate"; import { Deposit } from "./features/deposit/Deposit"; import { Log } from "./features/log/Log"; import { Register } from "./features/register/Register"; +import { OrderStats } from "./features/stats/OrderStats"; +import { ProductStats } from "./features/stats/ProductStats"; import { Stats } from "./features/stats/Stats"; import { UserDetail } from "./features/users/UserDetail"; import { UserList } from "./features/users/UserList"; @@ -14,6 +16,8 @@ export const Routes: React.FC = () => ( } /> } /> } /> + } /> + } /> } /> } /> } /> diff --git a/frontend/src/features/stats/OrderStats.tsx b/frontend/src/features/stats/OrderStats.tsx new file mode 100644 index 0000000..3368edd --- /dev/null +++ b/frontend/src/features/stats/OrderStats.tsx @@ -0,0 +1,79 @@ +import { useQuery } from "@apollo/client"; +import { Bar, BarDatum } from "@nivo/bar"; +import React from "react"; + +import { OrderStatsDocument, OrderStatsByTime } from "../../types"; +import { StatsView } from "./StatsView"; + +const days = { 1: "Mandag", 2: "Tirsdag", 3: "Onsdag", 4: "Torsdag", 5: "Fredag", 6: "Lørdag", 7: "Søndag" }; + +const months = { + 1: "Januar", + 2: "Februar", + 3: "Mars", + 4: "April", + 5: "Mai", + 6: "Juni", + 7: "Juli", + 8: "August", + 9: "September", + 10: "Oktober", + 11: "November", + 12: "Desember", +}; + +const OrderBar = ({ data }: { data: BarDatum[] }) => ( + d.id.toString()} + axisLeft={{ legend: "Ordre", legendPosition: "middle", legendOffset: -50 }} + /> +); + +export const OrderStats = () => { + const { data, loading } = useQuery(OrderStatsDocument); + + return ( + +

Ordre per time

+ ({ + id: `${stat.period.toString().padStart(2, "0")}:00`, + label: stat.period, + value: stat.count, + }))} + /> +

Ordre per ukedag

+ ({ + /* @ts-expect-error Here, stat.period can only be a key in days range. */ + id: days[stat.period], + label: stat.period, + value: stat.count, + }))} + /> +

Ordre per måned

+ ({ + /* @ts-expect-error Here, stat.period can only be a key in months range. */ + id: months[stat.period], + label: stat.period, + value: stat.count, + }))} + /> +

Ordre per år

+ ({ + id: stat.period, + label: stat.period, + value: stat.count, + }))} + /> +
+ ); +}; diff --git a/frontend/src/features/stats/ProductStats.tsx b/frontend/src/features/stats/ProductStats.tsx new file mode 100644 index 0000000..e54fd7d --- /dev/null +++ b/frontend/src/features/stats/ProductStats.tsx @@ -0,0 +1,40 @@ +import { useQuery } from "@apollo/client"; +import { Pie } from "@nivo/pie"; +import React from "react"; + +import { ProductStatsDocument } from "../../types"; +import styles from "./Stats.module.css"; +import { StatsView } from "./StatsView"; + +type UserCount = { + name: string; + count: number; +}; + +export const ProductStats = () => { + const { data, loading } = useQuery(ProductStatsDocument, { + variables: { active: true }, + }); + + return ( + +
+ {(data?.allProducts || []).map((product) => ( +
+ {product.name} + ({ + id: uc.name, + label: uc.name, + value: uc.count, + }))} + width={400} + height={300} + /> +
+ ))} +
+
+ ); +}; diff --git a/frontend/src/features/stats/Stats.module.css b/frontend/src/features/stats/Stats.module.css index c6ad36a..35ddfd2 100644 --- a/frontend/src/features/stats/Stats.module.css +++ b/frontend/src/features/stats/Stats.module.css @@ -4,3 +4,6 @@ gap: 1rem; grid-template-columns: repeat(3, 1fr); } +.statsNav { + margin-bottom: 1rem; +} \ No newline at end of file diff --git a/frontend/src/features/stats/Stats.tsx b/frontend/src/features/stats/Stats.tsx index d765341..8a6d7bb 100644 --- a/frontend/src/features/stats/Stats.tsx +++ b/frontend/src/features/stats/Stats.tsx @@ -1,113 +1,8 @@ -import { useQuery } from "@apollo/client"; -import { Bar, BarDatum } from "@nivo/bar"; -import { Pie } from "@nivo/pie"; import React from "react"; -import { OrderStatsDocument, ProductStatsDocument, OrderStatsByTime } from "../../types"; -import styles from "./Stats.module.css"; - -type UserCount = { - name: string; - count: number; -}; - -const days = { 1: "Mandag", 2: "Tirsdag", 3: "Onsdag", 4: "Torsdag", 5: "Fredag", 6: "Lørdag", 7: "Søndag" }; - -const months = { - 1: "Januar", - 2: "Februar", - 3: "Mars", - 4: "April", - 5: "Mai", - 6: "Juni", - 7: "Juli", - 8: "August", - 9: "September", - 10: "Oktober", - 11: "November", - 12: "Desember", -}; - -const OrderBar = ({ data }: { data: BarDatum[] }) => ( - d.id.toString()} - axisLeft={{ legend: "Ordre", legendPosition: "middle", legendOffset: -50 }} - /> -); - +import { StatsView } from "./StatsView"; export const Stats = () => { - const { data: productData, loading: productLoading } = useQuery(ProductStatsDocument, { - variables: { active: true }, - }); - const { data: orderData, loading: orderLoading } = useQuery(OrderStatsDocument); + const loading = false; - const loading = orderLoading || productLoading; - - if (loading) { - return
Loading...
; - } - - return ( -
-

Stats

-

Produkter

-
- {(productData?.allProducts || []).map((product) => ( -
- {product.name} - ({ - id: uc.name, - label: uc.name, - value: uc.count, - }))} - width={400} - height={300} - /> -
- ))} -
-

Ordre per time

- ({ - id: `${stat.period.toString().padStart(2, "0")}:00`, - label: stat.period, - value: stat.count, - }))} - /> -

Ordre per ukedag

- ({ - /* @ts-expect-error Here, stat.period can only be a key in days range. */ - id: days[stat.period], - label: stat.period, - value: stat.count, - }))} - /> -

Ordre per måned

- ({ - /* @ts-expect-error Here, stat.period can only be a key in months range. */ - id: months[stat.period], - label: stat.period, - value: stat.count, - }))} - /> -

Ordre per år

- ({ - id: stat.period, - label: stat.period, - value: stat.count, - }))} - /> -
- ); + return TODO; }; diff --git a/frontend/src/features/stats/StatsView.tsx b/frontend/src/features/stats/StatsView.tsx new file mode 100644 index 0000000..f077120 --- /dev/null +++ b/frontend/src/features/stats/StatsView.tsx @@ -0,0 +1,41 @@ +import React from "react"; +import { NavLink } from "react-router-dom"; + +import styles from "./Stats.module.css"; +const navLinkStyles = ({ isActive }: { isActive: boolean }) => (isActive ? "nav-link active" : "nav-link"); + +export const StatsNav = () => ( +
+
    +
  • + + Oversikt + +
  • +
  • + + Produkter + +
  • +
  • + + Ordre + +
  • +
  • + + Folk + +
  • +
+
+); + +export const StatsView = ({ children, loading }: { children: React.ReactNode; loading: boolean }) => ( +
+

Statistikk

+ + {loading &&
Loading...
} + {!loading && children} +
+);