Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public function share(Request $request): array
'auth' => [
'user' => $request->user(),
],
'request' => [
'urlParams' => $request->query(),
],
];
}
}
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@primevue/themes": "^4.2.2",
"@tailwindcss/vite": "^4.0.0",
"@types/lodash-es": "^4.17.12",
"@types/qs": "^6.9.18",
"@typescript-eslint/eslint-plugin": "^8.19.1",
"@typescript-eslint/parser": "^8.19.1",
"@vitejs/plugin-vue": "^5.0.0",
Expand All @@ -27,6 +28,7 @@
"lodash-es": "^4.17.21",
"primeicons": "^7.0.0",
"primevue": "^4.2.2",
"qs": "^6.14.0",
"tailwindcss": "^4.0.0",
"tailwindcss-primeui": "^0.3.2",
"typescript": "^5.7.3",
Expand Down
4 changes: 2 additions & 2 deletions resources/js/Composables/useLazyDataTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { DataTableFilterMetaData, DataTableFilterEvent, DataTableSortEvent } fro
import { PrimeVueDataFilters } from '@/types';

export function useLazyDataTable(
propDataToFetch: string,
initialFilters: PrimeVueDataFilters = {},
only: string[] = ['request'],
initialsRows: number = 20
) {
const {
Expand All @@ -20,7 +20,7 @@ export function useLazyDataTable(
fetchData,
paginate,
hardReset,
} = usePaginatedData(initialFilters, only, initialsRows);
} = usePaginatedData(propDataToFetch, initialFilters, initialsRows);

function parseEventFilterValues() {
Object.keys(filters.value).forEach((key) => {
Expand Down
39 changes: 26 additions & 13 deletions resources/js/Composables/usePaginatedData.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ref, computed, onMounted } from 'vue';
import { router, usePage } from '@inertiajs/vue3';
import { router } from '@inertiajs/vue3';
import { FilterMatchMode } from '@primevue/core/api';
import debounce from 'lodash-es/debounce';
import cloneDeep from 'lodash-es/cloneDeep';
import { PageState, DataTablePageEvent } from 'primevue';
import { PrimeVueDataFilters } from '@/types';
import qs from 'qs';

interface PaginatedFilteredSortedQueryParams {
filters?: PrimeVueDataFilters;
Expand All @@ -23,16 +24,11 @@ interface SortState {
}

export function usePaginatedData(
propDataToFetch: string,
initialFilters: PrimeVueDataFilters = {},
only: string[] = ['request'],
initialsRows: number = 20
) {
const page = usePage<{
request: {
urlParams: PaginatedFilteredSortedQueryParams;
};
}>();

const urlParams = ref<PaginatedFilteredSortedQueryParams>({});
const processing = ref<boolean>(false);
const filters = ref<PrimeVueDataFilters>(cloneDeep(initialFilters));
const sorting = ref<SortState>({
Expand All @@ -48,8 +44,8 @@ export function usePaginatedData(
return (pagination.value.page - 1) * pagination.value.rows;
});
const filteredOrSorted = computed(() => {
const filters = page.props?.request?.urlParams?.filters || {};
const sortField = page.props?.request?.urlParams?.sortField || null;
const filters = urlParams.value?.filters || {};
const sortField = urlParams.value?.sortField || null;
const isFiltering = Object.values(filters).some(
(filter) => filter.value !== null && filter.value !== ''
);
Expand All @@ -62,6 +58,20 @@ export function usePaginatedData(
filterCallback();
}, 300);

function setUrlParams() {
const queryString = window.location.search;
const params = qs.parse(queryString, {
ignoreQueryPrefix: true,
strictNullHandling: true,
decoder: function (str, defaultDecoder) {
// set empty string values to null
const value = defaultDecoder(str);
return value === '' ? null : value;
},
}) as PaginatedFilteredSortedQueryParams;
urlParams.value = { ...params };
}

function scrollToTop() {
window.scrollTo({
top: 0,
Expand All @@ -84,14 +94,15 @@ export function usePaginatedData(
preserveUrl: false,
showProgress: true,
replace: true,
only: ['request', ...new Set(only)],
only: [propDataToFetch],
onSuccess: (page) => {
resolve(page);
},
onError: (errors) => {
reject(errors);
},
onFinish: () => {
setUrlParams();
processing.value = false;
},
});
Expand Down Expand Up @@ -140,7 +151,7 @@ export function usePaginatedData(
preserveUrl: false,
showProgress: true,
replace: true,
only: ['request', ...new Set(only)],
only: [propDataToFetch],
});
}

Expand All @@ -154,6 +165,7 @@ export function usePaginatedData(
filters.value[key].value = new Date(filter.value);
} else if (
typeof filter.value === 'string' &&
filter.value !== '' &&
!isNaN(Number(filter.value))
) {
filters.value[key].value = Number(filter.value);
Expand Down Expand Up @@ -199,7 +211,8 @@ export function usePaginatedData(
}

onMounted(() => {
parseUrlParams(page.props.request.urlParams);
setUrlParams();
parseUrlParams(urlParams.value);
});

return {
Expand Down