@campxdev/campx-web-utils 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/components/ActivityLog.tsx +96 -0
- package/src/index.tsx +1 -1
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@campxdev/campx-web-utils",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"main": "./export.ts",
|
|
5
5
|
"private": false,
|
|
6
6
|
"peerDependencies": {
|
|
7
|
-
"@campxdev/react-blueprint": ">=1.
|
|
7
|
+
"@campxdev/react-blueprint": ">=1.7.0",
|
|
8
8
|
"@emotion/react": ">=^11.13.3",
|
|
9
9
|
"@emotion/styled": ">=^11.13.0",
|
|
10
10
|
"@mui/icons-material": ">=6.1.5",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"react-redux": "=>9.1.2"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@campxdev/react-blueprint": "1.
|
|
17
|
+
"@campxdev/react-blueprint": "1.7.0",
|
|
18
18
|
"@hookform/resolvers": "^3.9.0",
|
|
19
19
|
"@mui/x-date-pickers": "^7.22.1",
|
|
20
20
|
"axios": "^1.7.2",
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { ActivityLogView } from '@campxdev/react-blueprint';
|
|
2
|
+
import { Store } from 'pullstate';
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
|
+
import { useInfiniteQuery } from 'react-query';
|
|
5
|
+
import { axios } from '../config/axios';
|
|
6
|
+
|
|
7
|
+
function formatDate(date: Date): string {
|
|
8
|
+
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type ActivityLogType = {
|
|
12
|
+
activitiesData: any[];
|
|
13
|
+
lastTimestamp: number | null;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type ActivityLogStoreType = {
|
|
17
|
+
[logType: string]: ActivityLogType;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const ActivityLogStore = new Store<ActivityLogStoreType>({});
|
|
21
|
+
|
|
22
|
+
export default function ActivityLog({
|
|
23
|
+
apiEndpoint,
|
|
24
|
+
params,
|
|
25
|
+
}: {
|
|
26
|
+
apiEndpoint: string;
|
|
27
|
+
params: Record<string, any>;
|
|
28
|
+
}) {
|
|
29
|
+
const [fromDate, setFromDate] = useState<Date | null>(null);
|
|
30
|
+
const [toDate, setToDate] = useState<Date | null>(null);
|
|
31
|
+
|
|
32
|
+
const { activitiesData } = ActivityLogStore.useState(
|
|
33
|
+
(s) => s[params.logType] || { activitiesData: [], lastTimestamp: null },
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const fetchActivities = async ({ pageParam = null }) => {
|
|
37
|
+
const response = await axios.get(apiEndpoint, {
|
|
38
|
+
params: {
|
|
39
|
+
...params,
|
|
40
|
+
lastTimestamp: pageParam,
|
|
41
|
+
...(fromDate && { fromDate: formatDate(fromDate) }),
|
|
42
|
+
...(toDate && { toDate: formatDate(toDate) }),
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
return response.data;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const { fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } =
|
|
49
|
+
useInfiniteQuery(
|
|
50
|
+
['activities', params.logType, fromDate, toDate],
|
|
51
|
+
fetchActivities,
|
|
52
|
+
{
|
|
53
|
+
getNextPageParam: (lastPage) =>
|
|
54
|
+
lastPage?.length
|
|
55
|
+
? new Date(lastPage[lastPage.length - 1]?.timestamp).getTime()
|
|
56
|
+
: null,
|
|
57
|
+
onSuccess: (data) => {
|
|
58
|
+
ActivityLogStore.update((s) => {
|
|
59
|
+
const currentData = s[params.logType] || {
|
|
60
|
+
activitiesData: [],
|
|
61
|
+
lastTimestamp: null,
|
|
62
|
+
};
|
|
63
|
+
currentData.activitiesData = data.pages.flatMap(
|
|
64
|
+
(page) => page ?? [],
|
|
65
|
+
);
|
|
66
|
+
currentData.lastTimestamp = data.pages[data.pages.length - 1]
|
|
67
|
+
?.timestamp
|
|
68
|
+
? new Date(data.pages[data.pages.length - 1].timestamp).getTime()
|
|
69
|
+
: null;
|
|
70
|
+
s[params.logType] = currentData;
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
enabled: activitiesData.length === 0 || !!fromDate || !!toDate,
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
if (fromDate || toDate) {
|
|
79
|
+
fetchNextPage();
|
|
80
|
+
}
|
|
81
|
+
}, [fromDate, toDate, fetchNextPage]);
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<ActivityLogView
|
|
85
|
+
activitiesData={activitiesData}
|
|
86
|
+
isFetchingNextPage={isFetchingNextPage}
|
|
87
|
+
fetchNextPage={fetchNextPage}
|
|
88
|
+
hasNextPage={hasNextPage}
|
|
89
|
+
fromDate={fromDate}
|
|
90
|
+
toDate={toDate}
|
|
91
|
+
isLoading={isLoading}
|
|
92
|
+
setFromDate={setFromDate}
|
|
93
|
+
setToDate={setToDate}
|
|
94
|
+
/>
|
|
95
|
+
);
|
|
96
|
+
}
|