2024-11-21 16:11:23 +02:00
|
|
|
<template>
|
2024-11-25 22:58:10 +02:00
|
|
|
<CreateMessageDeliveryOption :isLoading :label :method v-model="model" />
|
2024-11-21 16:11:23 +02:00
|
|
|
|
|
|
|
|
<!-- Ошибка разрешений -->
|
2024-11-25 01:08:46 +02:00
|
|
|
<p v-if="isError && model === method" class="text-red-500 mb-2">
|
|
|
|
|
{{ error }}.<br />
|
|
|
|
|
<button @click="retry()" class="text-blue-500 underline">Повторить</button>
|
2024-11-21 16:11:23 +02:00
|
|
|
</p>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { type QRCodeDocument } from '@/types/DBDocumentTypes'
|
|
|
|
|
import { type DeliveryOption, DELIVERY_METHOD_WEB_PUSH } from '@/constants/deliveryOptions'
|
2024-11-25 01:08:46 +02:00
|
|
|
import { computed } from 'vue'
|
|
|
|
|
import { useQuery } from '@tanstack/vue-query'
|
2024-11-21 16:11:23 +02:00
|
|
|
|
2024-11-25 01:08:46 +02:00
|
|
|
import { getVapidPublicKey, saveWebPushSubscription } from '@/api/webPush'
|
2024-11-24 11:51:45 +02:00
|
|
|
|
2024-11-21 16:11:23 +02:00
|
|
|
type DeliveryOptionWebPush = Extract<DeliveryOption, { method: typeof DELIVERY_METHOD_WEB_PUSH }>
|
|
|
|
|
|
|
|
|
|
defineProps<{
|
|
|
|
|
label: DeliveryOptionWebPush['label']
|
|
|
|
|
method: DeliveryOptionWebPush['method']
|
|
|
|
|
}>()
|
|
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
|
(e: 'change'): void
|
|
|
|
|
}>()
|
|
|
|
|
|
|
|
|
|
const model = defineModel<null | QRCodeDocument['messageDeliveryMethod']>()
|
|
|
|
|
|
|
|
|
|
// Функция запроса разрешений и подписки
|
2024-11-25 01:08:46 +02:00
|
|
|
async function requestNotificationPermission() {
|
2024-11-21 16:11:23 +02:00
|
|
|
if (!('Notification' in window)) {
|
2024-11-25 01:08:46 +02:00
|
|
|
throw new Error('Уведомления не поддерживаются этим браузером.')
|
2024-11-21 16:11:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2024-11-25 01:08:46 +02:00
|
|
|
const applicationServerKeyPromise = getVapidPublicKey()
|
|
|
|
|
|
2024-11-21 16:11:23 +02:00
|
|
|
const permission = await Notification.requestPermission()
|
|
|
|
|
if (permission !== 'granted') {
|
2024-11-25 01:08:46 +02:00
|
|
|
throw new Error('Разрешение на уведомления не предоставлено.')
|
2024-11-21 16:11:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const registration = await navigator.serviceWorker.ready
|
2024-11-25 22:58:10 +02:00
|
|
|
|
2024-11-21 16:11:23 +02:00
|
|
|
const subscription = await registration.pushManager.subscribe({
|
|
|
|
|
userVisibleOnly: true,
|
2024-11-25 01:08:46 +02:00
|
|
|
applicationServerKey: await applicationServerKeyPromise,
|
2024-11-21 16:11:23 +02:00
|
|
|
})
|
|
|
|
|
|
2024-11-25 01:08:46 +02:00
|
|
|
const webPushSubscriptionDoc = await saveWebPushSubscription(subscription)
|
|
|
|
|
emit('change')
|
|
|
|
|
|
|
|
|
|
return webPushSubscriptionDoc
|
2024-11-21 16:11:23 +02:00
|
|
|
} catch (err) {
|
2024-11-25 01:08:46 +02:00
|
|
|
throw new Error('Ошибка при создании Push-подписки: ' + (err as Error).message)
|
2024-11-21 16:11:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-25 01:08:46 +02:00
|
|
|
const {
|
|
|
|
|
isLoading,
|
|
|
|
|
isError,
|
|
|
|
|
error,
|
|
|
|
|
refetch: retry,
|
|
|
|
|
} = useQuery({
|
|
|
|
|
enabled: computed(() => model.value === DELIVERY_METHOD_WEB_PUSH),
|
|
|
|
|
queryKey: ['createPushSubscription'],
|
|
|
|
|
queryFn: () => requestNotificationPermission(),
|
|
|
|
|
retry: false,
|
|
|
|
|
})
|
2024-11-21 16:11:23 +02:00
|
|
|
</script>
|