@campxdev/campx-web-utils 0.4.1 → 0.4.3

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 CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@campxdev/campx-web-utils",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "main": "./export.ts",
5
5
  "private": false,
6
6
  "peerDependencies": {
7
- "@campxdev/react-blueprint": ">=1.6.6",
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.6.6",
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
+ }
package/src/index.tsx CHANGED
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import ReactDOM from 'react-dom/client';
3
- import './index.css';
4
3
  import App from './App';
4
+ import './index.css';
5
5
  import reportWebVitals from './reportWebVitals';
6
6
 
7
7
  const root = ReactDOM.createRoot(
@@ -0,0 +1,16 @@
1
+ import { SingleSelect, SingleSelectProps } from '@campxdev/react-blueprint';
2
+ import { axios } from '../config/axios';
3
+
4
+ export const ProgramSelector = (props: SingleSelectProps) => {
5
+ return (
6
+ <SingleSelect
7
+ dbLabelProps={{
8
+ labelKey: 'branchCode',
9
+ subLabelKey: 'branchDisplay',
10
+ }}
11
+ {...props}
12
+ externalAxios={axios}
13
+ optionsApiEndPoint="/dropdowns/programs"
14
+ />
15
+ );
16
+ };
@@ -0,0 +1,20 @@
1
+ import { SingleSelect, SingleSelectProps } from '@campxdev/react-blueprint';
2
+ import { axios } from '../config/axios';
3
+
4
+ export const RegulationSelector = (props: SingleSelectProps) => {
5
+ return (
6
+ <SingleSelect
7
+ label="Regulation"
8
+ dbValueProps={{
9
+ valueKey: 'regulation_id',
10
+ }}
11
+ dbLabelProps={{
12
+ labelKey: 'r.name',
13
+ subLabelKey: 'batch',
14
+ }}
15
+ {...props}
16
+ optionsApiEndPoint="dropdowns/regulations"
17
+ externalAxios={axios}
18
+ />
19
+ );
20
+ };
@@ -6,5 +6,7 @@ export * from './FeeTypeSelector';
6
6
  export * from './HostelFloorSelector';
7
7
  export * from './HostelRoomSelector';
8
8
  export * from './PrintFormatSelector';
9
+ export * from './ProgramSelector';
10
+ export * from './RegulationSelector';
9
11
  export * from './SemesterSelector';
10
12
  export * from './YearRangeSelector';