From cc7f4b354df48f5e73b8cf7e82b578bbb796b7c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Thu, 14 May 2020 01:35:24 +0200 Subject: [PATCH] fix(frontend): add selectable timeframe and styling to listens report --- frontend/src/components/ReportListens.tsx | 170 +++++++++++++++++----- 1 file changed, 133 insertions(+), 37 deletions(-) diff --git a/frontend/src/components/ReportListens.tsx b/frontend/src/components/ReportListens.tsx index d6b4cb3..1d6073c 100644 --- a/frontend/src/components/ReportListens.tsx +++ b/frontend/src/components/ReportListens.tsx @@ -9,6 +9,7 @@ import { Tooltip, XAxis, YAxis, + TooltipProps, } from "recharts"; import { getListensReport } from "../api/api"; import { ListenReportItem } from "../api/entities/listen-report-item"; @@ -51,7 +52,30 @@ export const ReportListens: React.FC = () => {

Listen Report

-
+
+
+
+ + +
+
{isLoading && (
Loading Listens @@ -63,7 +87,7 @@ export const ReportListens: React.FC = () => {
)} {report.length > 0 && ( -
+
)} @@ -82,41 +106,113 @@ const ReportGraph: React.FC<{ date: getTime(date), })); + const ReportTooltip: React.FC = ({ + active, + payload, + label, + }) => { + if (!active || payload === undefined) { + return null; + } + + const [{ value: listens }] = payload; + const date = format( + label as number, + dateFormatFromTimeFrame(options.timeFrame) + ); + + return ( +
+

{date}

+

+ Listens: {listens} +

+
+ ); + }; + return ( - - - - - - - - - - format(date, "P")} - /> - - [value, "Listens"]} /> - - - +
+ + + + + + + + + + + format(date, shortDateFormatFromTimeFrame(options.timeFrame)) + } + /> + + + + + +
); }; + +const shortDateFormatFromTimeFrame = ( + timeFrame: "day" | "week" | "month" | "year" +): string => { + const FORMAT_DAY = "P"; + const FORMAT_WEEK = "'Week' w yyyy"; + const FORMAT_MONTH = "LLL yyyy"; + const FORMAT_YEAR = "yyyy"; + const FORMAT_DEFAULT = FORMAT_DAY; + + switch (timeFrame) { + case "day": + return FORMAT_DAY; + case "week": + return FORMAT_WEEK; + case "month": + return FORMAT_MONTH; + case "year": + return FORMAT_YEAR; + default: + return FORMAT_DEFAULT; + } +}; + +const dateFormatFromTimeFrame = ( + timeFrame: "day" | "week" | "month" | "year" +): string => { + const FORMAT_DAY = "PPPP"; + const FORMAT_WEEK = "'Week starting on' PPPP"; + const FORMAT_MONTH = "LLLL yyyy"; + const FORMAT_YEAR = "yyyy"; + const FORMAT_DEFAULT = FORMAT_DAY; + + switch (timeFrame) { + case "day": + return FORMAT_DAY; + case "week": + return FORMAT_WEEK; + case "month": + return FORMAT_MONTH; + case "year": + return FORMAT_YEAR; + default: + return FORMAT_DEFAULT; + } +};