fix(Phone Parser): store country prefix

FIx #1040
This commit is contained in:
sharevb 2024-05-18 15:08:27 +02:00 committed by ShareVB
parent e876d03608
commit 374caa176e
2 changed files with 32 additions and 2 deletions

View file

@ -1,7 +1,8 @@
import { useRouteQuery } from '@vueuse/router';
import { computed } from 'vue';
import { useStorage } from '@vueuse/core';
export { useQueryParam };
export { useQueryParam, useQueryParamOrStorage };
const transformers = {
number: {
@ -33,3 +34,31 @@ function useQueryParam<T>({ name, defaultValue }: { name: string; defaultValue:
},
});
}
function useQueryParamOrStorage<T>({ name, storageName, defaultValue }: { name: string; storageName: string; defaultValue?: T }) {
const type = typeof defaultValue;
const transformer = transformers[type as keyof typeof transformers] ?? transformers.string;
const storageRef = useStorage(storageName, defaultValue);
const storageDefaultValue = storageRef.value ?? defaultValue;
const proxy = useRouteQuery(name, transformer.toQuery(storageDefaultValue as never));
const ref = computed<T>({
get() {
return transformer.fromQuery(proxy.value) as unknown as T;
},
set(value) {
proxy.value = transformer.toQuery(value as never);
},
});
watch(
ref,
(newValue) => {
storageRef.value = newValue;
},
);
return ref;
}

View file

@ -9,9 +9,10 @@ import {
import { withDefaultOnError } from '@/utils/defaults';
import { booleanToHumanReadable } from '@/utils/boolean';
import { useValidation } from '@/composable/validation';
import { useQueryParamOrStorage } from '@/composable/queryParams';
const rawPhone = ref('');
const defaultCountryCode = ref(getDefaultCountryCode());
const defaultCountryCode = useQueryParamOrStorage({ name: 'country', storageName: 'phone-parser:country', defaultValue: getDefaultCountryCode() });
const validation = useValidation({
source: rawPhone,
rules: [