2021-03-04 01:03:53 +01:00
|
|
|
import apiGeneral from '@api/general'
|
2021-06-21 11:59:29 +02:00
|
|
|
import apiTooot from '@api/tooot'
|
2021-03-04 01:03:53 +01:00
|
|
|
import { displayMessage } from '@components/Message'
|
2021-08-29 15:25:38 +02:00
|
|
|
import navigationRef from '@helpers/navigationRef'
|
2021-03-04 01:03:53 +01:00
|
|
|
import { Dispatch } from '@reduxjs/toolkit'
|
2021-06-21 11:59:29 +02:00
|
|
|
import { disableAllPushes, Instance } from '@utils/slices/instancesSlice'
|
2021-03-04 01:03:53 +01:00
|
|
|
import * as Notifications from 'expo-notifications'
|
|
|
|
import { useEffect } from 'react'
|
|
|
|
import { TFunction } from 'react-i18next'
|
|
|
|
|
|
|
|
export interface Params {
|
|
|
|
mode: 'light' | 'dark'
|
2021-03-28 23:31:10 +02:00
|
|
|
t: TFunction<'screens'>
|
2021-03-04 01:03:53 +01:00
|
|
|
instances: Instance[]
|
|
|
|
dispatch: Dispatch<any>
|
|
|
|
}
|
|
|
|
|
2021-08-29 15:25:38 +02:00
|
|
|
const pushUseConnect = ({ mode, t, instances, dispatch }: Params) => {
|
2021-03-04 01:03:53 +01:00
|
|
|
return useEffect(() => {
|
|
|
|
const connect = async () => {
|
|
|
|
const expoToken = (
|
|
|
|
await Notifications.getExpoPushTokenAsync({
|
|
|
|
experienceId: '@xmflsct/tooot'
|
|
|
|
})
|
|
|
|
).data
|
|
|
|
|
2021-06-21 11:59:29 +02:00
|
|
|
apiTooot({
|
2021-12-12 22:09:18 +01:00
|
|
|
method: 'get',
|
|
|
|
url: `push/connect/${expoToken}`,
|
2021-03-12 00:19:45 +01:00
|
|
|
sentry: true
|
2021-03-14 00:47:55 +01:00
|
|
|
}).catch(error => {
|
|
|
|
if (error.status == 410) {
|
|
|
|
displayMessage({
|
|
|
|
mode,
|
|
|
|
type: 'error',
|
|
|
|
duration: 'long',
|
2021-03-28 23:31:10 +02:00
|
|
|
message: t('pushError.message'),
|
|
|
|
description: t('pushError.description'),
|
2021-03-14 00:47:55 +01:00
|
|
|
onPress: () => {
|
2021-08-29 15:25:38 +02:00
|
|
|
navigationRef.navigate('Screen-Tabs', {
|
2021-03-14 00:47:55 +01:00
|
|
|
screen: 'Tab-Me',
|
|
|
|
params: {
|
|
|
|
screen: 'Tab-Me-Root'
|
|
|
|
}
|
|
|
|
})
|
2021-08-29 15:25:38 +02:00
|
|
|
navigationRef.navigate('Screen-Tabs', {
|
2021-03-14 00:47:55 +01:00
|
|
|
screen: 'Tab-Me',
|
|
|
|
params: {
|
|
|
|
screen: 'Tab-Me-Settings'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-03-04 01:03:53 +01:00
|
|
|
|
2021-03-14 00:47:55 +01:00
|
|
|
dispatch(disableAllPushes())
|
2021-03-04 01:03:53 +01:00
|
|
|
|
2021-03-14 00:47:55 +01:00
|
|
|
instances.forEach(instance => {
|
|
|
|
if (instance.push.global.value) {
|
|
|
|
apiGeneral<{}>({
|
|
|
|
method: 'delete',
|
|
|
|
domain: instance.url,
|
|
|
|
url: 'api/v1/push/subscription',
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${instance.token}`
|
|
|
|
}
|
|
|
|
}).catch(() => console.log('error!!!'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-03-04 01:03:53 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const pushEnabled = instances.filter(instance => instance.push.global.value)
|
|
|
|
if (pushEnabled.length) {
|
2021-12-12 22:09:18 +01:00
|
|
|
connect()
|
2021-03-04 01:03:53 +01:00
|
|
|
}
|
|
|
|
}, [instances])
|
|
|
|
}
|
|
|
|
|
|
|
|
export default pushUseConnect
|