tooot/src/utils/slices/contextsSlice.ts

63 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'
2022-12-11 01:37:26 +01:00
import { ContextsLatest } from '@utils/migrations/contexts/migration'
import Constants from 'expo-constants'
2021-01-18 00:23:40 +01:00
import * as StoreReview from 'expo-store-review'
2022-12-11 01:37:26 +01:00
export const contextsInitialState: ContextsLatest = {
2022-11-12 18:01:17 +01:00
// After 10 successful postings
2021-01-18 00:23:40 +01:00
storeReview: {
2022-11-12 18:01:17 +01:00
context: 10,
2021-01-18 00:23:40 +01:00
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-12-11 01:37:26 +01:00
previousTab: 'Tab-Me',
previousSegment: 'Local'
2021-01-18 00:23:40 +01:00
}
const contextsSlice = createSlice({
2021-02-20 19:12:44 +01:00
name: 'contexts',
2022-12-11 01:37:26 +01:00
initialState: contextsInitialState,
2021-01-18 00:23:40 +01:00
reducers: {
updateStoreReview: (state, action: PayloadAction<1>) => {
if (Constants.expoConfig?.extra?.environment === 'release') {
2021-01-22 01:34:20 +01:00
state.storeReview.current = state.storeReview.current + action.payload
if (state.storeReview.current === state.storeReview.context) {
2022-12-11 01:37:26 +01:00
StoreReview?.isAvailableAsync().then(() => StoreReview.requestReview())
2021-01-22 01:34:20 +01:00
}
2021-01-18 00:23:40 +01:00
}
},
updatePublicRemoteNotice: (state, action: PayloadAction<1>) => {
2022-12-11 01:37:26 +01:00
state.publicRemoteNotice.current = state.publicRemoteNotice.current + action.payload
if (state.publicRemoteNotice.current === state.publicRemoteNotice.context) {
2021-01-18 00:23:40 +01:00
state.publicRemoteNotice.hidden = true
}
2021-02-11 23:42:13 +01:00
},
2022-12-11 01:37:26 +01:00
updatePreviousTab: (state, action: PayloadAction<ContextsLatest['previousTab']>) => {
2021-02-11 23:42:13 +01:00
state.previousTab = action.payload
2022-12-11 01:37:26 +01:00
},
updatePreviousSegment: (state, action: PayloadAction<ContextsLatest['previousSegment']>) => {
state.previousSegment = action.payload
2021-01-18 00:23:40 +01:00
}
}
})
2022-12-11 01:37:26 +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
2022-12-11 01:37:26 +01:00
export const getPreviousSegment = (state: RootState) => state.contexts.previousSegment
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-12-11 01:37:26 +01:00
updatePreviousTab,
updatePreviousSegment
2021-01-18 00:23:40 +01:00
} = contextsSlice.actions
export default contextsSlice.reducer