Skip to content

Commit ba43564

Browse files
committed
Add test component for charts
1 parent 26bac48 commit ba43564

File tree

12 files changed

+350
-15
lines changed

12 files changed

+350
-15
lines changed

demo/src/payload.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ export default buildConfig({
3131
siteId: PLAUSIBLE_SITE_ID,
3232
host: PLAUSIBLE_HOST,
3333
},
34+
collections: [
35+
{
36+
slug: Posts.slug,
37+
},
38+
],
3439
}),
3540
],
3641
});

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@
2929
"devDependencies": {
3030
"@types/react-router-dom": "^5.3.3",
3131
"payload": "^1.6.26",
32+
"react": "^18",
3233
"typescript": "^4.5.5"
3334
},
3435
"files": [
3536
"dist",
3637
"types.js",
3738
"types.d.ts"
38-
]
39+
],
40+
"dependencies": {
41+
"react-charts": "^3.0.0-beta.54"
42+
}
3943
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React, { useEffect, useState } from "react";
2+
import type { DashboardAnalyticsConfig } from "../../types";
3+
import { AxisOptions, Chart } from "react-charts";
4+
5+
type Props = {};
6+
7+
type ChartDataType = {
8+
date: string;
9+
pageviews: number;
10+
visitors: number;
11+
};
12+
13+
export const ViewsChart: React.FC<Props> = (props) => {
14+
const [chartData, setChartData] = useState<ChartDataType[]>([]);
15+
16+
/* useEffect(() => {
17+
const getChartData = fetch(`/api/analytics/globalChartData`).then(
18+
(response) => response.json()
19+
);
20+
21+
getChartData.then((data) => {
22+
console.log("fetched data from backend:", data);
23+
setChartData(data);
24+
});
25+
}, []); */
26+
27+
/* const primaryAxis = React.useMemo<AxisOptions<ChartDataType>>(
28+
() => ({
29+
getValue: (datum) => datum.date as unknown as Date,
30+
}),
31+
[]
32+
);
33+
34+
const secondaryAxes = React.useMemo<AxisOptions<ChartDataType>[]>(
35+
() => [
36+
{
37+
getValue: (datum) => datum.visitors,
38+
},
39+
],
40+
[]
41+
); */
42+
43+
return (
44+
<div
45+
style={{
46+
marginBottom: "20px",
47+
}}
48+
>
49+
my chart goes here:
50+
{/* <Chart
51+
options={{
52+
chartData,
53+
primaryAxis,
54+
secondaryAxes,
55+
}}
56+
/> */}
57+
</div>
58+
);
59+
};
60+
61+
export const getViewsChart = (props: any) => <ViewsChart {...props} />;

src/index.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { Config as PayloadConfig } from "payload/config";
2-
import { DashboardAnalyticsConfig } from "./types";
1+
import type { Config as PayloadConfig } from "payload/config";
2+
import type { DashboardAnalyticsConfig } from "./types";
33
import { extendWebpackConfig } from "./extendWebpackConfig";
44
import getProvider from "./providers";
55
import getGlobalAggregateData from "./routes/getGlobalAggregateData";
6+
import getGlobalChartData from "./routes/getGlobalChartData";
7+
import type { CollectionConfig } from "payload/dist/collections/config/types";
8+
import { getViewsChart } from "./components/Charts/ViewsChart";
69

710
const payloadDashboardAnalytics =
811
(incomingConfig: DashboardAnalyticsConfig) =>
912
(config: PayloadConfig): PayloadConfig => {
10-
const { admin } = config;
13+
const { admin, collections } = config;
1114
const { provider } = incomingConfig;
1215
const endpoints = config.endpoints ?? [];
1316
const apiProvider = getProvider(provider);
@@ -18,7 +21,42 @@ const payloadDashboardAnalytics =
1821
...admin,
1922
webpack: extendWebpackConfig(config),
2023
},
21-
endpoints: [...endpoints, getGlobalAggregateData(apiProvider)],
24+
endpoints: [
25+
...endpoints,
26+
getGlobalAggregateData(apiProvider),
27+
getGlobalChartData(apiProvider),
28+
],
29+
...(collections && {
30+
collections: collections.map((collection) => {
31+
const targetCollection = incomingConfig.collections?.find((col) => {
32+
if (col.slug === collection.slug) return true;
33+
return false;
34+
});
35+
36+
if (targetCollection) {
37+
const collectionConfigWithHooks: CollectionConfig = {
38+
...collection,
39+
fields: [
40+
...collection.fields,
41+
{
42+
type: "ui",
43+
name: "viewschart",
44+
admin: {
45+
position: "sidebar",
46+
components: {
47+
Field: (props) => getViewsChart(props),
48+
},
49+
},
50+
},
51+
],
52+
};
53+
54+
return collectionConfigWithHooks;
55+
}
56+
57+
return collection;
58+
}),
59+
}),
2260
};
2361

2462
return processedConfig;

src/providers/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type { Provider } from "../types";
33

44
export type ApiProvider = {
55
getGlobalAggregateData: () => Promise<any>;
6-
/* getGlobalChartData: () => {},
7-
getPageAggregateData: () => {},
6+
getGlobalChartData: () => Promise<any>;
7+
/* getPageAggregateData: () => {},
88
getPageChartData: () => {}, */
99
};
1010

src/providers/plausible/client.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ function client(provider: PlausibleProvider, endpoint?: string) {
77
const url = new URL(`${host}/api/${apiVersion}${endpoint}`);
88
url.searchParams.append("site_id", provider.siteId);
99

10-
const plausibleMetrics = [
11-
"visitors",
12-
"pageviews",
13-
"bounce_rate",
14-
"visit_duration",
15-
];
10+
const plausibleMetrics = ["visitors", "pageviews"];
1611

1712
const baseUrl = String(url.href);
1813
url.searchParams.append("period", "30d");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { PlausibleProvider } from "../../types";
2+
import client from "./client";
3+
4+
async function getGlobalChartData(provider: PlausibleProvider) {
5+
const plausibleClient = client(provider, "/stats/timeseries");
6+
7+
const data = await plausibleClient.fetch().then((response) => {
8+
return response.json();
9+
});
10+
11+
return data;
12+
}
13+
14+
export default getGlobalChartData;

src/providers/plausible/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { PlausibleProvider } from "../../types";
22
import getGlobalAggregateData from "./getGlobalAggregateData";
3+
import getGlobalChartData from "./getGlobalChartData";
34
import { ApiProvider } from "..";
45

56
const plausible = (provider: PlausibleProvider): ApiProvider => {
67
return {
78
getGlobalAggregateData: async () => await getGlobalAggregateData(provider),
9+
getGlobalChartData: async () => await getGlobalChartData(provider),
810
/* getGlobalChartData: () => {},
911
getPageAggregateData: () => {},
1012
getPageChartData: () => {}, */
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Endpoint } from "payload/config";
2+
import { ApiProvider } from "../../providers";
3+
import payload from "payload";
4+
5+
const handler = (provider: ApiProvider) => {
6+
const handler: Endpoint["handler"] = async (req, res, next) => {
7+
try {
8+
const data = await provider.getGlobalChartData();
9+
res.status(200).send(data);
10+
} catch (error) {
11+
payload.logger.error(payload);
12+
res.status(500);
13+
}
14+
};
15+
16+
return handler;
17+
};
18+
19+
export default handler;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Endpoint } from "payload/config";
2+
import handler from "./handler";
3+
import { ApiProvider } from "../../providers";
4+
5+
const getGlobalChartData = (provider: ApiProvider): Endpoint => {
6+
return {
7+
path: "/analytics/globalChartData",
8+
method: "get",
9+
handler: handler(provider),
10+
};
11+
};
12+
13+
export default getGlobalChartData;

0 commit comments

Comments
 (0)