Files
Investidor-V2/resources/js/lib/territorial-search-params.ts
T

25 lines
840 B
TypeScript

const territoryKeys = [
'province_id',
'district_id',
'admin_post_id',
'locality_id',
];
/** No fallback scope: an explicit empty path means all; a selected path replaces its old descendants. */
export function territorialSearchParams(
entries: Iterable<[string, FormDataEntryValue]>,
path?: { id: string; location_type: string }[],
): URLSearchParams {
const params = new URLSearchParams();
for (const [key, value] of entries)
if (typeof value === 'string' && value !== '' && key !== 'page')
params.set(key, value);
if (path) {
territoryKeys.forEach((key) => params.delete(key));
for (const node of path) {
const key = `${node.location_type}_id`;
if (territoryKeys.includes(key)) params.set(key, node.id);
}
}
return params;
}