From 824a1d0bc458531af894b98e704168e0841d4e06 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Sat, 31 Dec 2022 11:44:05 -0900 Subject: [PATCH] add integer support --- frontend/composables/use-route-params.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/frontend/composables/use-route-params.ts b/frontend/composables/use-route-params.ts index d38f409..c06d2db 100644 --- a/frontend/composables/use-route-params.ts +++ b/frontend/composables/use-route-params.ts @@ -4,6 +4,7 @@ import { WritableComputedRef } from "vue"; export function useRouteQuery(q: string, def: string[]): WritableComputedRef; export function useRouteQuery(q: string, def: string): WritableComputedRef; export function useRouteQuery(q: string, def: boolean): WritableComputedRef; +export function useRouteQuery(q: string, def: number): WritableComputedRef; // eslint-disable-next-line @typescript-eslint/no-explicit-any export function useRouteQuery(q: string, def: any): WritableComputedRef { const route = useRoute(); @@ -56,5 +57,21 @@ export function useRouteQuery(q: string, def: any): WritableComputedRef { router.push({ query }); }, }); + case "number": + return computed({ + get: () => { + const qv = route.query[q]; + if (Array.isArray(qv) && qv.length > 0) { + return parseInt(qv[0] as string, 10); + } + return parseInt(qv as string, 10) || def; + }, + set: v => { + const query = { ...route.query, [q]: `${v}` }; + router.push({ query }); + }, + }); } + + throw new Error("Invalid type"); }