tooot/src/utils/slices/contextsSlice.ts

77 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-01-18 00:23:40 +01:00
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { RootState } from '@root/store'
2021-02-02 22:50:38 +01:00
import * as Updates from 'expo-updates'
2021-01-18 00:23:40 +01:00
import * as StoreReview from 'expo-store-review'
export type ContextsState = {
storeReview: {
context: Readonly<number>
current: number
shown: boolean
}
publicRemoteNotice: {
context: Readonly<number>
current: number
hidden: boolean
}
2021-02-11 23:42:13 +01:00
previousTab: 'Tab-Local' | 'Tab-Public' | 'Tab-Notifications' | 'Tab-Me'
2021-01-18 00:23:40 +01:00
}
export const contextsInitialState = {
// After 3 successful postings
storeReview: {
context: 3,
current: 0,
shown: false
},
// After public remote settings has been used once
publicRemoteNotice: {
context: 1,
current: 0,
hidden: false
2021-02-11 23:42:13 +01:00
},
2022-01-16 23:26:05 +01:00
previousTab: 'Tab-Me'
2021-01-18 00:23:40 +01:00
}
const contextsSlice = createSlice({
2021-02-20 19:12:44 +01:00
name: 'contexts',
2021-01-18 00:23:40 +01:00
initialState: contextsInitialState as ContextsState,
reducers: {
updateStoreReview: (state, action: PayloadAction<1>) => {
2021-04-16 14:58:18 +02:00
if (Updates.releaseChannel.includes('release')) {
2021-01-22 01:34:20 +01:00
state.storeReview.current = state.storeReview.current + action.payload
if (state.storeReview.current === state.storeReview.context) {
StoreReview.isAvailableAsync().then(() => StoreReview.requestReview())
}
2021-01-18 00:23:40 +01:00
}
},
updatePublicRemoteNotice: (state, action: PayloadAction<1>) => {
state.publicRemoteNotice.current =
state.publicRemoteNotice.current + action.payload
if (
state.publicRemoteNotice.current === state.publicRemoteNotice.context
) {
state.publicRemoteNotice.hidden = true
}
2021-02-11 23:42:13 +01:00
},
updatePreviousTab: (
state,
action: PayloadAction<ContextsState['previousTab']>
) => {
state.previousTab = action.payload
2021-01-18 00:23:40 +01:00
}
}
})
export const getPublicRemoteNotice = (state: RootState) =>
state.contexts.publicRemoteNotice
2021-02-11 23:42:13 +01:00
export const getPreviousTab = (state: RootState) => state.contexts.previousTab
2021-11-15 23:43:35 +01:00
export const getContexts = (state: RootState) => state.contexts
2021-01-18 00:23:40 +01:00
export const {
updateStoreReview,
2021-02-11 23:42:13 +01:00
updatePublicRemoteNotice,
2022-01-16 23:26:05 +01:00
updatePreviousTab
2021-01-18 00:23:40 +01:00
} = contextsSlice.actions
export default contextsSlice.reducer