2021-02-20 19:12:44 +01:00
|
|
|
import { createAsyncThunk } from '@reduxjs/toolkit'
|
2021-02-27 16:33:54 +01:00
|
|
|
import { RootState } from '@root/store'
|
2022-01-02 22:28:33 +01:00
|
|
|
import { isDevelopment } from '@utils/checkEnvironment'
|
2022-05-28 19:24:08 +02:00
|
|
|
import { InstanceLatest } from '@utils/migrations/instances/migration'
|
2021-02-27 16:33:54 +01:00
|
|
|
import * as Notifications from 'expo-notifications'
|
|
|
|
import pushRegister from './push/register'
|
|
|
|
import pushUnregister from './push/unregister'
|
2021-02-20 19:12:44 +01:00
|
|
|
|
2021-02-27 16:33:54 +01:00
|
|
|
export const updateInstancePush = createAsyncThunk(
|
2021-02-20 19:12:44 +01:00
|
|
|
'instances/updatePush',
|
|
|
|
async (
|
2021-02-27 16:33:54 +01:00
|
|
|
disable: boolean,
|
|
|
|
{ getState }
|
2022-05-28 19:24:08 +02:00
|
|
|
): Promise<InstanceLatest['push']['keys']['auth'] | undefined> => {
|
2021-02-27 16:33:54 +01:00
|
|
|
const state = getState() as RootState
|
2022-01-02 22:28:33 +01:00
|
|
|
const expoToken = isDevelopment
|
|
|
|
? 'DEVELOPMENT_TOKEN_1'
|
|
|
|
: (
|
|
|
|
await Notifications.getExpoPushTokenAsync({
|
2022-05-15 00:40:49 +02:00
|
|
|
experienceId: '@xmflsct/tooot',
|
2022-05-16 23:44:49 +02:00
|
|
|
applicationId: 'com.xmflsct.app.tooot'
|
2022-01-02 22:28:33 +01:00
|
|
|
})
|
|
|
|
).data
|
2021-02-27 16:33:54 +01:00
|
|
|
|
|
|
|
if (disable) {
|
|
|
|
return await pushRegister(state, expoToken)
|
2021-02-20 19:12:44 +01:00
|
|
|
} else {
|
2021-02-27 16:33:54 +01:00
|
|
|
return await pushUnregister(state, expoToken)
|
2021-02-20 19:12:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|