0
0
Fork 0

feat: refs #6818 add URL handling vn-link-phone

This commit is contained in:
Jorge Penadés 2024-12-02 10:36:18 +01:00
parent 68b11295bb
commit 7693d193f0
2 changed files with 25 additions and 14 deletions

View File

@ -2,6 +2,7 @@
import { ref, reactive, useAttrs, onBeforeMount, capitalize } from 'vue'; import { ref, reactive, useAttrs, onBeforeMount, capitalize } from 'vue';
import axios from 'axios'; import axios from 'axios';
import { parsePhone } from 'src/filters'; import { parsePhone } from 'src/filters';
import useOpenURL from 'src/composables/useOpenURL';
const props = defineProps({ const props = defineProps({
phoneNumber: { type: [String, Number], default: null }, phoneNumber: { type: [String, Number], default: null },
@ -14,13 +15,14 @@ const config = reactive({
sip: { icon: 'phone', href: `sip:${props.phoneNumber}` }, sip: { icon: 'phone', href: `sip:${props.phoneNumber}` },
'say-simple': { 'say-simple': {
icon: 'vn:saysimple', icon: 'vn:saysimple',
href: null, url: null,
channel: props.channel, channel: props.channel,
}, },
}); });
const type = Object.keys(config).find((key) => key in useAttrs()) || 'sip'; const type = Object.keys(config).find((key) => key in useAttrs()) || 'sip';
onBeforeMount(async () => { onBeforeMount(async () => {
if (!phone.value) return;
let { channel } = config[type]; let { channel } = config[type];
if (type === 'say-simple') { if (type === 'say-simple') {
@ -31,9 +33,14 @@ onBeforeMount(async () => {
phone.value = await parsePhone(props.phoneNumber, props.country.toLowerCase()); phone.value = await parsePhone(props.phoneNumber, props.country.toLowerCase());
config[ config[
type type
].href = `${url}?customerIdentity=%2B${phone.value}&channelId=${channel}`; ].url = `${url}?customerIdentity=%2B${phone.value}&channelId=${channel}`;
} }
}); });
function handleClick() {
if (config[type].url) useOpenURL(config[type].url);
else if (config[type].href) window.location.href = config[type].href;
}
</script> </script>
<template> <template>
<QBtn <QBtn
@ -44,8 +51,7 @@ onBeforeMount(async () => {
size="sm" size="sm"
color="primary" color="primary"
padding="none" padding="none"
:href="config[type].href" @click.stop="handleClick"
@click.stop
> >
<QTooltip> <QTooltip>
{{ capitalize(type).replace('-', '') }} {{ capitalize(type).replace('-', '') }}

View File

@ -7,28 +7,33 @@ describe('parsePhone filter', () => {
vi.spyOn(axios, 'get').mockReturnValue({ data: { prefix: '34' } }); vi.spyOn(axios, 'get').mockReturnValue({ data: { prefix: '34' } });
}); });
it('no phone', () => {
const phone = parsePhone(null, '34');
expect(phone).toBe(undefined);
});
it("adds prefix +34 if it doesn't have one", async () => { it("adds prefix +34 if it doesn't have one", async () => {
const resultado = await parsePhone('123456789', '34'); const phone = await parsePhone('123456789', '34');
expect(resultado).toBe('34123456789'); expect(phone).toBe('34123456789');
}); });
it('maintains prefix +34 if it is already correct', async () => { it('maintains prefix +34 if it is already correct', async () => {
const resultado = await parsePhone('+34123456789', '34'); const phone = await parsePhone('+34123456789', '34');
expect(resultado).toBe('34123456789'); expect(phone).toBe('34123456789');
}); });
it('converts prefix 0034 to +34', async () => { it('converts prefix 0034 to +34', async () => {
const resultado = await parsePhone('0034123456789', '34'); const phone = await parsePhone('0034123456789', '34');
expect(resultado).toBe('34123456789'); expect(phone).toBe('34123456789');
}); });
it('converts prefix 34 without symbol to +34', async () => { it('converts prefix 34 without symbol to +34', async () => {
const resultado = await parsePhone('34123456789', '34'); const phone = await parsePhone('34123456789', '34');
expect(resultado).toBe('34123456789'); expect(phone).toBe('34123456789');
}); });
it('replaces incorrect prefix with the correct one', async () => { it('replaces incorrect prefix with the correct one', async () => {
const resultado = await parsePhone('+44123456789', '34'); const phone = await parsePhone('+44123456789', '34');
expect(resultado).toBe('44123456789'); expect(phone).toBe('44123456789');
}); });
}); });