Skip to content

Commit e8caaeb

Browse files
authored
composable fixes
1 parent 1cf2bf4 commit e8caaeb

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

resources/js/Composables/useLazyDataTable.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { usePaginatedData } from './usePaginatedData';
22
import cloneDeep from 'lodash-es/cloneDeep';
3-
import {
4-
DataTableFilterMetaData,
5-
DataTableFilterEvent,
6-
DataTableSortEvent,
7-
} from 'primevue';
3+
import { DataTableFilterMetaData, DataTableFilterEvent, DataTableSortEvent } from 'primevue';
84
import { PrimeVueDataFilters } from '@/types';
95

106
export function useLazyDataTable(
@@ -23,6 +19,7 @@ export function useLazyDataTable(
2319
scrollToTop,
2420
fetchData,
2521
paginate,
22+
hardReset,
2623
} = usePaginatedData(initialFilters, only, initialsRows);
2724

2825
function parseEventFilterValues() {
@@ -78,13 +75,9 @@ export function useLazyDataTable(
7875
});
7976
sorting.value.field = '';
8077
sorting.value.order = 1;
81-
pagination.value = {
82-
page: 1,
83-
rows: initialsRows,
84-
};
85-
fetchData().then(() => {
86-
window.history.replaceState(null, '', window.location.pathname);
87-
});
78+
pagination.value.page = 1;
79+
pagination.value.rows = initialsRows;
80+
fetchData();
8881
}
8982

9083
return {
@@ -100,5 +93,6 @@ export function useLazyDataTable(
10093
filter,
10194
sort,
10295
reset,
96+
hardReset,
10397
};
104-
}
98+
}

resources/js/Composables/usePaginatedData.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,21 @@ export function usePaginatedData(
7070
}
7171

7272
function fetchData() {
73-
window.history.replaceState(null, '', window.location.pathname);
7473
return new Promise((resolve, reject) => {
7574
processing.value = true;
76-
router.reload({
77-
only: ['request', ...new Set(only)],
75+
router.visit(window.location.pathname, {
76+
method: 'get',
7877
data: {
7978
filters: filters.value as any,
8079
...pagination.value,
8180
sortField: sorting.value.field,
8281
sortOrder: sorting.value.order,
8382
},
83+
preserveState: true,
84+
preserveUrl: false,
8485
showProgress: true,
86+
replace: true,
87+
only: ['request', ...new Set(only)],
8588
onSuccess: (page) => {
8689
resolve(page);
8790
},
@@ -96,7 +99,11 @@ export function usePaginatedData(
9699
}
97100

98101
function paginate(event: PageState | DataTablePageEvent) {
99-
pagination.value.page = event.page + 1;
102+
if (event.rows != pagination.value.rows) {
103+
pagination.value.page = 1;
104+
} else {
105+
pagination.value.page = event.page + 1;
106+
}
100107
pagination.value.rows = event.rows;
101108
fetchData().then(() => {
102109
scrollToTop();
@@ -111,9 +118,6 @@ export function usePaginatedData(
111118
}
112119

113120
function reset() {
114-
// Alternatively just use: router.get(window.location.pathname);
115-
// Caveat to the above approach, we would lose state from our page not related to pagination/filtering/sorting
116-
117121
const defaultFilters = cloneDeep(initialFilters);
118122
Object.keys(defaultFilters).forEach((key) => {
119123
filters.value[key].value = defaultFilters[key].value;
@@ -127,8 +131,16 @@ export function usePaginatedData(
127131
page: 1,
128132
rows: initialsRows,
129133
};
130-
fetchData().then(() => {
131-
window.history.replaceState(null, '', window.location.pathname);
134+
fetchData();
135+
}
136+
137+
function hardReset() {
138+
router.visit(window.location.pathname, {
139+
method: 'get',
140+
preserveUrl: false,
141+
showProgress: true,
142+
replace: true,
143+
only: ['request', ...new Set(only)],
132144
});
133145
}
134146

@@ -203,6 +215,7 @@ export function usePaginatedData(
203215
paginate,
204216
filter,
205217
reset,
218+
hardReset,
206219
parseUrlParams,
207220
};
208-
}
221+
}

0 commit comments

Comments
 (0)