From e106d2475b92597daa9bdb5298ead51546d11b25 Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Wed, 25 Oct 2023 10:21:21 +1300 Subject: [PATCH] Add period selectors for insights page --- InsightsPage.tsx | 40 ++++++++++++++++++++++++++++++---------- periods.ts | 3 +++ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/InsightsPage.tsx b/InsightsPage.tsx index f270f52..51100dc 100644 --- a/InsightsPage.tsx +++ b/InsightsPage.tsx @@ -3,10 +3,12 @@ import { useCallback, useState } from "react"; import { View } from "react-native"; import { Text } from "react-native-paper"; import AppBarChart from "./AppBarChart"; -import { PADDING } from "./constants"; +import { MARGIN, PADDING } from "./constants"; import { AppDataSource } from "./data-source"; import DrawerHeader from "./DrawerHeader"; import { DAYS } from "./time"; +import Select from "./Select"; +import { Periods } from "./periods"; export interface WeekCounts { week: number; @@ -15,19 +17,24 @@ export interface WeekCounts { export default function InsightsPage() { const [weekCounts, setWeekCounts] = useState([]); + const [period, setPeriod] = useState(Periods.Monthly); useFocusEffect( useCallback(() => { + let difference = "-1 months"; + if (period === Periods.TwoMonths) difference = "-2 months"; + if (period === Periods.ThreeMonths) difference = "-3 months"; + if (period === Periods.SixMonths) difference = "-6 months"; const select = ` - SELECT strftime('%w', created) as week, COUNT(*) as count - FROM sets - WHERE created IS NOT NULL - GROUP BY week - HAVING week IS NOT NULL - ORDER BY count DESC; - `; + SELECT strftime('%w', created) as week, COUNT(*) as count + FROM sets + WHERE DATE(created) >= DATE('now', 'weekday 0', '${difference}') + GROUP BY week + HAVING week IS NOT NULL + ORDER BY count DESC; + `; AppDataSource.manager.query(select).then(setWeekCounts); - }, []) + }, [period]) ); return ( @@ -39,7 +46,20 @@ export default function InsightsPage() { flexGrow: 1, }} > - Most active days of the week + + Most active days of the week + +