gbs-add-block 0.0.98 → 0.0.99
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/README.md +2 -2
- package/index.cjs +2 -1
- package/package.json +1 -1
- package/source/components/usepaginateddata/usePaginatedData.ts +170 -0
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# GBS Building Blocks 2.0 (v0.0.
|
|
1
|
+
# GBS Building Blocks 2.0 (v0.0.99)
|
|
2
2
|
|
|
3
3
|
Latest and upgraded version of GBS building blocks with headless UI and removed dependencies.
|
|
4
4
|
|
|
@@ -6,7 +6,7 @@ Latest and upgraded version of GBS building blocks with headless UI and removed
|
|
|
6
6
|
|
|
7
7
|
For detailed documentation on usage and props, Please visit: [Building Block Documentation v2.0](https://blackmax-designs.gitbook.io/building-block-v2.0)
|
|
8
8
|
|
|
9
|
-
## What's New 🎉 (Ver 0.0.
|
|
9
|
+
## What's New 🎉 (Ver 0.0.99)
|
|
10
10
|
|
|
11
11
|
- Updated for bug fixes.
|
|
12
12
|
|
package/index.cjs
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { useState, useEffect, useCallback } from "react";
|
|
2
|
+
import {
|
|
3
|
+
exportToExcelHelper,
|
|
4
|
+
exportToPDFHelper,
|
|
5
|
+
} from "@grampro/headless-helpers";
|
|
6
|
+
|
|
7
|
+
export const usePaginatedData = (
|
|
8
|
+
apiEndpoint: string,
|
|
9
|
+
initialPageSize = 10,
|
|
10
|
+
method: "GET" | "POST" = "GET",
|
|
11
|
+
columns: any[] = []
|
|
12
|
+
) => {
|
|
13
|
+
const [data, setData] = useState<any[]>([]);
|
|
14
|
+
const [loading, setLoading] = useState(false);
|
|
15
|
+
const [filters, setFilters] = useState<any[]>([]);
|
|
16
|
+
const [pagination, setPagination] = useState({
|
|
17
|
+
currentPage: 0,
|
|
18
|
+
totalPages: 0,
|
|
19
|
+
totalCount: 0,
|
|
20
|
+
pageSize: initialPageSize,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const fetchData = useCallback(
|
|
24
|
+
async (
|
|
25
|
+
method: "GET" | "POST",
|
|
26
|
+
page: number,
|
|
27
|
+
pageSize: number,
|
|
28
|
+
currentFilters: any[] = [],
|
|
29
|
+
search = "",
|
|
30
|
+
additionalParams: any = {},
|
|
31
|
+
isExportCall: boolean | null = null,
|
|
32
|
+
exportType: string | null = null
|
|
33
|
+
) => {
|
|
34
|
+
try {
|
|
35
|
+
setLoading(true);
|
|
36
|
+
|
|
37
|
+
let response;
|
|
38
|
+
|
|
39
|
+
if (method === "POST") {
|
|
40
|
+
response = await fetch(apiEndpoint, {
|
|
41
|
+
method: "POST",
|
|
42
|
+
headers: {
|
|
43
|
+
"Content-Type": "application/json",
|
|
44
|
+
},
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
page: page + 1,
|
|
47
|
+
pageSize,
|
|
48
|
+
filters: currentFilters,
|
|
49
|
+
search,
|
|
50
|
+
additionalParams,
|
|
51
|
+
isExportCall,
|
|
52
|
+
}),
|
|
53
|
+
});
|
|
54
|
+
} else {
|
|
55
|
+
let url = `${apiEndpoint}?page=${page + 1}&pageSize=${pageSize}`;
|
|
56
|
+
if (currentFilters.length > 0) {
|
|
57
|
+
url += `&filters=${encodeURIComponent(
|
|
58
|
+
JSON.stringify(currentFilters)
|
|
59
|
+
)}`;
|
|
60
|
+
}
|
|
61
|
+
if (search) {
|
|
62
|
+
url += `&search=${encodeURIComponent(search)}`;
|
|
63
|
+
}
|
|
64
|
+
if (isExportCall) {
|
|
65
|
+
url += `&isExportCall=${encodeURIComponent(true)}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
response = await fetch(url);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!response.ok) throw new Error(`Error: ${response.status}`);
|
|
72
|
+
|
|
73
|
+
const result = await response.json();
|
|
74
|
+
|
|
75
|
+
if (!isExportCall) {
|
|
76
|
+
setData(result.data);
|
|
77
|
+
setPagination((prev) => ({
|
|
78
|
+
...prev,
|
|
79
|
+
currentPage: page,
|
|
80
|
+
totalPages: result.pagination.totalPages,
|
|
81
|
+
totalCount: result.pagination.totalItems,
|
|
82
|
+
}));
|
|
83
|
+
} else {
|
|
84
|
+
if (exportType == "excelExport") {
|
|
85
|
+
exportToExcelHelper(
|
|
86
|
+
result.data,
|
|
87
|
+
columns.length > 0 ? columns : [],
|
|
88
|
+
"excel-export-data"
|
|
89
|
+
);
|
|
90
|
+
} else if (exportType == "pdfExport") {
|
|
91
|
+
exportToPDFHelper(
|
|
92
|
+
result.data,
|
|
93
|
+
columns.length > 0 ? columns : [],
|
|
94
|
+
"pdf-export-data",
|
|
95
|
+
{ layout: "portrait", paperSize: "a4" }
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.error(`Failed to fetch data (${method}):`, error);
|
|
101
|
+
} finally {
|
|
102
|
+
setLoading(false);
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
[apiEndpoint]
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
// Handle page changes
|
|
109
|
+
const handlePageChange = useCallback(
|
|
110
|
+
(status: { currentPage: number; totalPages: number }) => {
|
|
111
|
+
if (status.currentPage !== pagination.currentPage) {
|
|
112
|
+
fetchData(method, status.currentPage, pagination.pageSize, filters);
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
[fetchData, pagination.currentPage, pagination.pageSize, filters]
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
// Handle filter changes
|
|
119
|
+
const handleFilterChange = useCallback(
|
|
120
|
+
(newFilters: any[]) => {
|
|
121
|
+
const filterType = newFilters.map((f: any) => ({ ...f, type: "filter" }));
|
|
122
|
+
setFilters(filterType);
|
|
123
|
+
fetchData(method, 0, pagination.pageSize, filterType);
|
|
124
|
+
},
|
|
125
|
+
[fetchData, pagination.pageSize]
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
// Handle search input
|
|
129
|
+
const handleSearch = useCallback(
|
|
130
|
+
(searchTerm: string) => {
|
|
131
|
+
const updatedFilters = filters.filter((f) => {
|
|
132
|
+
return !(f as any).searchTerm;
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const finalFilters = [
|
|
136
|
+
...updatedFilters,
|
|
137
|
+
{ type: "search", value: searchTerm },
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
setFilters(finalFilters);
|
|
141
|
+
fetchData(method, 0, pagination.pageSize, finalFilters);
|
|
142
|
+
},
|
|
143
|
+
[fetchData, pagination.pageSize, filters]
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
// Handle exports
|
|
147
|
+
const handleExports = useCallback(
|
|
148
|
+
(exportType: string) => {
|
|
149
|
+
fetchData(method, 0, pagination.pageSize, [], "", {}, true, exportType);
|
|
150
|
+
},
|
|
151
|
+
[fetchData, method, pagination.pageSize]
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
fetchData(method, 0, pagination.pageSize, filters);
|
|
156
|
+
}, [fetchData, pagination.pageSize]);
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
data,
|
|
160
|
+
loading,
|
|
161
|
+
pagination,
|
|
162
|
+
filters,
|
|
163
|
+
handlePageChange,
|
|
164
|
+
handleFilterChange,
|
|
165
|
+
refetch: () =>
|
|
166
|
+
fetchData(method, pagination.currentPage, pagination.pageSize, filters),
|
|
167
|
+
handleSearch,
|
|
168
|
+
handleExports,
|
|
169
|
+
};
|
|
170
|
+
};
|