tooot/src/utils/slices/instances/updatePush.ts

32 lines
1001 B
TypeScript
Raw Normal View History

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'
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 }
): 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({
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
}
}
)