-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathusePaginatedDataTable.ts
More file actions
117 lines (105 loc) · 3.35 KB
/
usePaginatedDataTable.ts
File metadata and controls
117 lines (105 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { toRaw } from 'vue';
import type { Page, PageProps } from '@inertiajs/core';
import { DataTableFilterMetaData, DataTableFilterEvent, DataTableSortEvent } from 'primevue';
import { PrimeVueDataFilters, InertiaRouterFetchCallbacks } from '@/types';
import { usePaginatedData } from './usePaginatedData';
export function usePaginatedDataTable(
propDataToFetch: string | string[],
initialFilters: PrimeVueDataFilters = {},
initialRows: number = 20
) {
const {
processing,
filters,
sorting,
pagination,
firstDatasetIndex,
filteredOrSorted,
debounceInputFilter,
scrollToTop,
fetchData,
paginate,
hardReset,
} = usePaginatedData(propDataToFetch, initialFilters, initialRows);
function parseEventFilterValues() {
Object.keys(filters.value).forEach((key) => {
const filter = filters.value[key];
// empty arrays can cause filtering issues, set to null instead
if (Array.isArray(filter.value) && filter.value.length === 0) {
filters.value[key].value = null;
}
});
}
/**
* "Override" parent composable function
* Event-driven filtering rather than reactive state
*/
function filter(event: DataTableFilterEvent): void {
pagination.value.page = 1;
const newFilters: PrimeVueDataFilters = {};
Object.entries(event.filters).forEach(([key, rawFilter]) => {
if (
rawFilter &&
typeof rawFilter === 'object' &&
'matchMode' in rawFilter
) {
newFilters[key] = rawFilter as DataTableFilterMetaData;
}
});
filters.value = newFilters;
parseEventFilterValues();
fetchData({
onFinish: () => {
scrollToTop();
},
});
}
function sort(event: DataTableSortEvent): void {
pagination.value.page = 1;
sorting.value.field = event.sortField ? String(event.sortField) : '';
sorting.value.order = event.sortOrder || 1;
fetchData({
onFinish: () => {
scrollToTop();
},
});
}
/**
* "Override" parent composable function
* usePaginatedData() resets sorting.value state as a new object, this will not work for DataTable's
*/
function reset(options: InertiaRouterFetchCallbacks = {}): Promise<Page<PageProps>> {
const { onFinish: onFinishCallback, onSuccess, onError } = options;
const defaultFilters = structuredClone(toRaw(initialFilters));
Object.keys(defaultFilters).forEach((key) => {
filters.value[key].value = defaultFilters[key].value;
});
sorting.value.field = '';
sorting.value.order = 1;
pagination.value.page = 1;
pagination.value.rows = initialRows;
return fetchData({
onSuccess,
onError,
onFinish: () => {
scrollToTop();
onFinishCallback?.();
},
});
}
return {
processing,
filters,
sorting,
pagination,
firstDatasetIndex,
filteredOrSorted,
debounceInputFilter,
fetchData,
paginate,
filter,
sort,
reset,
hardReset,
};
}