Skip to content

Commit 57c034b

Browse files
committed
Add progress on aggregate page data
1 parent 9bacbb4 commit 57c034b

File tree

13 files changed

+99
-43
lines changed

13 files changed

+99
-43
lines changed

demo/src/payload.config.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default buildConfig({
6262
timeframe: "30d",
6363
idMatcher: (document: any) => `/articles/${document.slug}`,
6464
},
65-
{
65+
/* {
6666
type: "chart",
6767
metrics: ["sessions"],
6868
timeframe: "7d",
@@ -73,18 +73,19 @@ export default buildConfig({
7373
metrics: ["sessionDuration"],
7474
timeframe: "currentMonth",
7575
idMatcher: (document: any) => `/articles/${document.slug}`,
76-
},
77-
/* {
76+
}, */
77+
{
7878
type: "info",
7979
metrics: ["views"],
8080
idMatcher: (document: any) => `/articles/${document.slug}`,
81+
label: "hidden",
8182
},
8283
{
8384
type: "info",
8485
metrics: ["sessions", "sessionDuration"],
8586
timeframe: "6mo",
8687
idMatcher: (document: any) => `/articles/${document.slug}`,
87-
}, */
88+
},
8889
],
8990
},
9091
],

src/components/Charts/AggregateDataWidget.tsx renamed to src/components/Aggregates/AggregateDataWidget.tsx

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import React, {
66
useRef,
77
useMemo,
88
} from "react";
9-
import type { ChartDataPoint, ChartData } from "../../types/data";
9+
import type { AggregateData } from "../../types/data";
1010
import type { PageInfoWidget } from "../../types/widgets";
11-
import type { AxisOptions } from "react-charts";
1211
import { useDocumentInfo } from "payload/components/utilities";
1312
import { MetricMap } from "../../providers/plausible/utilities";
1413
import { useTheme } from "payload/dist/admin/components/utilities/Theme";
@@ -18,7 +17,7 @@ type Props = {
1817
};
1918

2019
const AggregateDataWidget: React.FC<Props> = ({ options }) => {
21-
const [chartData, setChartData] = useState<ChartData>([]);
20+
const [data, setData] = useState<AggregateData>([]);
2221
const [isLoading, setIsLoading] = useState<boolean>(true);
2322
const theme = useTheme();
2423
const { publishedDoc } = useDocumentInfo();
@@ -37,7 +36,7 @@ const AggregateDataWidget: React.FC<Props> = ({ options }) => {
3736

3837
useEffect(() => {
3938
if (pageId) {
40-
const getChartData = fetch(`/api/analytics/pageChartData`, {
39+
const getAggregateData = fetch(`/api/analytics/pageAggregateData`, {
4140
method: "post",
4241
credentials: "include",
4342
headers: {
@@ -51,16 +50,16 @@ const AggregateDataWidget: React.FC<Props> = ({ options }) => {
5150
}),
5251
}).then((response) => response.json());
5352

54-
getChartData.then((data: ChartData) => {
55-
setChartData(data);
53+
getAggregateData.then((data: AggregateData) => {
54+
setData(data);
5655
setIsLoading(false);
5756
});
5857
} else {
5958
setIsLoading(false);
6059
}
6160
}, [publishedDoc, pageId]);
6261

63-
const chartLabel = useMemo(() => {
62+
const heading = useMemo(() => {
6463
if (label) return label;
6564

6665
const metricValues: string[] = [];
@@ -77,9 +76,35 @@ const AggregateDataWidget: React.FC<Props> = ({ options }) => {
7776
<section
7877
style={{
7978
marginBottom: "1.5rem",
79+
border: "1px solid",
80+
borderColor: "var(--theme-elevation-100)",
81+
padding: "0.5rem",
8082
}}
8183
>
82-
aggr data
84+
{label !== "hidden" && (
85+
<h1 style={{ fontSize: "1.25rem", marginBottom: "0.75rem" }}>
86+
{heading} ({timeframeIndicator})
87+
</h1>
88+
)}
89+
<div>
90+
{isLoading ? (
91+
<>Loading...</>
92+
) : (
93+
<ul style={{ margin: "0", listStyle: "none", padding: "0" }}>
94+
{data.map((item, index) => {
95+
return (
96+
<li
97+
key={index}
98+
style={{ display: "flex", justifyContent: "space-between" }}
99+
>
100+
<div style={{ fontWeight: "700" }}>{item.label}</div>
101+
<div>{item.value}</div>
102+
</li>
103+
);
104+
})}
105+
</ul>
106+
)}
107+
</div>
83108
</section>
84109
);
85110
};

src/components/Charts/PageViewsChart.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ const PageViewsChart: React.FC<Props> = ({ options }) => {
3636
else return "";
3737
}, [publishedDoc]);
3838

39-
const timeframeIndicator =
40-
timeframe === "currentMonth"
39+
const timeframeIndicator = useMemo(() => {
40+
return timeframe === "currentMonth"
4141
? new Date().toLocaleString("default", { month: "long" })
4242
: timeframe ?? "30d";
43+
}, [timeframe]);
4344

4445
useEffect(() => {
4546
if (pageId) {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import getPageChartData from "./routes/getPageChartData";
1414
import getPageAggregateData from "./routes/getPageAggregateData";
1515
import type { CollectionConfig } from "payload/dist/collections/config/types";
1616
import { getPageViewsChart } from "./components/Charts/PageViewsChart";
17-
import { getAggregateDataWidget } from "./components/Charts/AggregateDataWidget";
17+
import { getAggregateDataWidget } from "./components/Aggregates/AggregateDataWidget";
1818

1919
const PageWidgetMap: Record<
2020
PageWidgets["type"],

src/providers/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import plausible from "./plausible";
22
import type { Provider } from "../types";
3-
import { ChartWidget, InfoWidget } from "../types/widgets";
3+
import type { ChartWidget, InfoWidget } from "../types/widgets";
4+
import type { ChartData, AggregateData } from "../types/data";
45

56
type BaseOptions = {
67
timeframe?: string;
@@ -23,10 +24,14 @@ export interface PageChartOptions extends BaseOptions {
2324
}
2425

2526
export type ApiProvider = {
26-
getGlobalAggregateData: (options: GlobalAggregateOptions) => Promise<any>;
27-
getGlobalChartData: (options: GlobalChartOptions) => Promise<any>;
28-
getPageAggregateData: (options: PageAggregateOptions) => Promise<any>;
29-
getPageChartData: (options: PageChartOptions) => Promise<any>;
27+
getGlobalAggregateData: (
28+
options: GlobalAggregateOptions
29+
) => Promise<AggregateData>;
30+
getGlobalChartData: (options: GlobalChartOptions) => Promise<ChartData>;
31+
getPageAggregateData: (
32+
options: PageAggregateOptions
33+
) => Promise<AggregateData>;
34+
getPageChartData: (options: PageChartOptions) => Promise<ChartData>;
3035
};
3136

3237
const getProvider = (provider: Provider) => {

src/providers/plausible/getGlobalChartData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { PlausibleProvider } from "../../types/providers";
22
import type { ChartData } from "../../types/data";
33
import type { GlobalChartOptions } from "..";
4-
import { MetricMap } from "./client";
4+
import { MetricMap } from "./utilities";
55
import payload from "payload";
66
import client from "./client";
77

src/providers/plausible/getPageAggregateData.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { PlausibleProvider } from "../../types/providers";
22
import type { PageAggregateOptions } from "..";
3+
import type { AggregateData } from "../../types/data";
4+
import { MetricMap } from "./utilities";
35
import client from "./client";
46

57
async function getPageAggregateData(
@@ -18,11 +20,23 @@ async function getPageAggregateData(
1820

1921
url.searchParams.append("filters", pageFilter);
2022

21-
const data = await plausibleClient.fetch(url.toString()).then((response) => {
22-
return response.json();
23-
});
23+
const data = await plausibleClient
24+
.fetch(url.toString())
25+
.then((response) => response.json());
26+
27+
const processedData: AggregateData = Object.entries(data.results).map(
28+
([label, value]: any) => {
29+
const labelAsMetric = Object.values(MetricMap).find((item) => {
30+
return label === item.value;
31+
});
2432

25-
return data;
33+
return {
34+
label: labelAsMetric?.label ?? label,
35+
value: value.value,
36+
};
37+
}
38+
);
39+
return processedData;
2640
}
2741

2842
export default getPageAggregateData;

src/providers/plausible/getPageChartData.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { PlausibleProvider } from "../../types/providers";
22
import type { PageChartOptions } from "..";
3-
import { MetricMap } from "./client";
3+
import type { ChartData } from "../../types/data";
4+
import { MetricMap } from "./utilities";
45
import payload from "payload";
56
import client from "./client";
67

@@ -29,8 +30,8 @@ async function getPageChartData(
2930
payload.logger.error(error);
3031
});
3132

32-
/* @todo: fix types later */
33-
/* @ts-ignore */
33+
// @todo: fix types later
34+
// @ts-ignore
3435
const dataSeries: ChartData = options.metrics.map((metric) => {
3536
const mappedMetric = Object.entries(MetricMap).find(([key, value]) => {
3637
return metric === key;

src/providers/plausible/utilities.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { AllAvailableMetrics } from "../../types/widgets";
1+
import { MetricsMap } from "../../types/data";
22

3-
export const MetricMap: Record<
4-
AllAvailableMetrics,
5-
{ label: string; value: string }
6-
> = {
3+
export const MetricMap: MetricsMap = {
74
views: {
85
label: "Views",
96
value: "pageviews",

src/routes/getPageAggregateData/handler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { Endpoint } from "payload/config";
1+
import type { Endpoint } from "payload/config";
22
import { ApiProvider } from "../../providers";
3+
import type { AggregateData } from "../../types/data";
34
import payload from "payload";
45

56
const handler = (provider: ApiProvider) => {
67
const handler: Endpoint["handler"] = async (req, res, next) => {
78
try {
89
const { timeframe, metrics, pageId } = req.body;
910

10-
console.log("reached handler with", timeframe, metrics, pageId);
11-
12-
const data = await provider.getPageAggregateData({
11+
const data: AggregateData = await provider.getPageAggregateData({
1312
timeframe: timeframe,
1413
metrics: metrics,
1514
pageId,
1615
});
16+
1717
res.status(200).send(data);
1818
} catch (error) {
1919
payload.logger.error(error);

0 commit comments

Comments
 (0)