forked from mirrors/homebox
feat: encode search into url (#131)
* route query helper * encode search parameters into url
This commit is contained in:
parent
b6b2a2d889
commit
b7aacb3cde
2 changed files with 135 additions and 12 deletions
60
frontend/composables/use-route-params.ts
Normal file
60
frontend/composables/use-route-params.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
/* eslint no-redeclare: 0 */
|
||||
import { WritableComputedRef } from "vue";
|
||||
|
||||
export function useRouteQuery(q: string, def: string[]): WritableComputedRef<string[]>;
|
||||
export function useRouteQuery(q: string, def: string): WritableComputedRef<string>;
|
||||
export function useRouteQuery(q: string, def: boolean): WritableComputedRef<boolean>;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function useRouteQuery(q: string, def: any): WritableComputedRef<any> {
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
|
||||
switch (typeof def) {
|
||||
case "string":
|
||||
if (route.query[q] === undefined) {
|
||||
router.push({ query: { ...route.query, [q]: def } });
|
||||
}
|
||||
|
||||
return computed({
|
||||
get: () => {
|
||||
const qv = route.query[q];
|
||||
if (Array.isArray(qv)) {
|
||||
return qv[0];
|
||||
}
|
||||
return qv;
|
||||
},
|
||||
set: v => {
|
||||
const query = { ...route.query, [q]: v };
|
||||
router.push({ query });
|
||||
},
|
||||
});
|
||||
case "object": // array
|
||||
return computed({
|
||||
get: () => {
|
||||
const qv = route.query[q];
|
||||
if (Array.isArray(qv)) {
|
||||
return qv;
|
||||
}
|
||||
return [qv];
|
||||
},
|
||||
set: v => {
|
||||
const query = { ...route.query, [q]: v };
|
||||
router.push({ query });
|
||||
},
|
||||
});
|
||||
case "boolean":
|
||||
return computed({
|
||||
get: () => {
|
||||
const qv = route.query[q];
|
||||
if (Array.isArray(qv)) {
|
||||
return qv[0] === "true";
|
||||
}
|
||||
return qv === "true";
|
||||
},
|
||||
set: v => {
|
||||
const query = { ...route.query, [q]: `${v}` };
|
||||
router.push({ query });
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue