Changeset View
Changeset View
Standalone View
Standalone View
native/redux/redux-setup.js
// @flow | // @flow | ||||
import { AppState as NativeAppState, Platform, Alert } from 'react-native'; | import { AppState as NativeAppState, Platform, Alert } from 'react-native'; | ||||
import Orientation from 'react-native-orientation-locker'; | import Orientation from 'react-native-orientation-locker'; | ||||
import { createStore, applyMiddleware, type Store, compose } from 'redux'; | import { createStore, applyMiddleware, type Store, compose } from 'redux'; | ||||
import { persistStore, persistReducer } from 'redux-persist'; | import { persistStore, persistReducer } from 'redux-persist'; | ||||
import thunk from 'redux-thunk'; | import thunk from 'redux-thunk'; | ||||
import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js'; | import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js'; | ||||
import { setDeviceTokenActionTypes } from 'lib/actions/device-actions.js'; | |||||
import { siweAuthActionTypes } from 'lib/actions/siwe-actions.js'; | import { siweAuthActionTypes } from 'lib/actions/siwe-actions.js'; | ||||
import { | import { | ||||
logOutActionTypes, | logOutActionTypes, | ||||
deleteAccountActionTypes, | deleteAccountActionTypes, | ||||
logInActionTypes, | logInActionTypes, | ||||
} from 'lib/actions/user-actions.js'; | } from 'lib/actions/user-actions.js'; | ||||
import { reduceDeviceToken } from 'lib/reducers/device-token-reducer.js'; | |||||
import baseReducer from 'lib/reducers/master-reducer.js'; | import baseReducer from 'lib/reducers/master-reducer.js'; | ||||
import { processThreadStoreOperations } from 'lib/reducers/thread-reducer.js'; | import { processThreadStoreOperations } from 'lib/reducers/thread-reducer.js'; | ||||
import { | import { | ||||
invalidSessionDowngrade, | invalidSessionDowngrade, | ||||
invalidSessionRecovery, | invalidSessionRecovery, | ||||
} from 'lib/shared/account-utils.js'; | } from 'lib/shared/account-utils.js'; | ||||
import { isStaff } from 'lib/shared/user-utils.js'; | import { isStaff } from 'lib/shared/user-utils.js'; | ||||
import { defaultEnabledApps } from 'lib/types/enabled-apps.js'; | import { defaultEnabledApps } from 'lib/types/enabled-apps.js'; | ||||
import { defaultCalendarFilters } from 'lib/types/filter-types.js'; | import { defaultCalendarFilters } from 'lib/types/filter-types.js'; | ||||
import type { Dispatch, BaseAction } from 'lib/types/redux-types.js'; | import type { Dispatch, BaseAction } from 'lib/types/redux-types.js'; | ||||
import { rehydrateActionType } from 'lib/types/redux-types.js'; | import { rehydrateActionType } from 'lib/types/redux-types.js'; | ||||
import type { SetSessionPayload } from 'lib/types/session-types.js'; | import type { SetSessionPayload } from 'lib/types/session-types.js'; | ||||
import { | import { defaultConnectionInfo } from 'lib/types/socket-types.js'; | ||||
defaultConnectionInfo, | |||||
incrementalStateSyncActionType, | |||||
} from 'lib/types/socket-types.js'; | |||||
import type { ThreadStoreOperation } from 'lib/types/thread-types.js'; | import type { ThreadStoreOperation } from 'lib/types/thread-types.js'; | ||||
import { updateTypes } from 'lib/types/update-types.js'; | |||||
import { reduxLoggerMiddleware } from 'lib/utils/action-logger.js'; | import { reduxLoggerMiddleware } from 'lib/utils/action-logger.js'; | ||||
import { setNewSessionActionType } from 'lib/utils/action-utils.js'; | import { setNewSessionActionType } from 'lib/utils/action-utils.js'; | ||||
import { convertMessageStoreOperationsToClientDBOperations } from 'lib/utils/message-ops-utils.js'; | import { convertMessageStoreOperationsToClientDBOperations } from 'lib/utils/message-ops-utils.js'; | ||||
import { convertThreadStoreOperationsToClientDBOperations } from 'lib/utils/thread-ops-utils.js'; | import { convertThreadStoreOperationsToClientDBOperations } from 'lib/utils/thread-ops-utils.js'; | ||||
import { | import { | ||||
resetUserStateActionType, | resetUserStateActionType, | ||||
recordNotifPermissionAlertActionType, | recordNotifPermissionAlertActionType, | ||||
▲ Show 20 Lines • Show All 234 Lines • ▼ Show 20 Lines | return { | ||||
...action.payload, | ...action.payload, | ||||
}, | }, | ||||
}; | }; | ||||
} else if (action.type === updateDeviceOrientationActionType) { | } else if (action.type === updateDeviceOrientationActionType) { | ||||
return { | return { | ||||
...state, | ...state, | ||||
deviceOrientation: action.payload, | deviceOrientation: action.payload, | ||||
}; | }; | ||||
} else if (action.type === setDeviceTokenActionTypes.success) { | |||||
return { | |||||
...state, | |||||
deviceToken: action.payload, | |||||
}; | |||||
} else if (action.type === updateThreadLastNavigatedActionType) { | } else if (action.type === updateThreadLastNavigatedActionType) { | ||||
const { threadID, time } = action.payload; | const { threadID, time } = action.payload; | ||||
if (state.messageStore.threads[threadID]) { | if (state.messageStore.threads[threadID]) { | ||||
state = { | state = { | ||||
...state, | ...state, | ||||
messageStore: { | messageStore: { | ||||
...state.messageStore, | ...state.messageStore, | ||||
threads: { | threads: { | ||||
Show All 10 Lines | function reducer(state: AppState = defaultState, action: Action) { | ||||
} | } | ||||
if (action.type === setNewSessionActionType) { | if (action.type === setNewSessionActionType) { | ||||
sessionInvalidationAlert(action.payload); | sessionInvalidationAlert(action.payload); | ||||
state = { | state = { | ||||
...state, | ...state, | ||||
cookie: action.payload.sessionChange.cookie, | cookie: action.payload.sessionChange.cookie, | ||||
}; | }; | ||||
} else if (action.type === incrementalStateSyncActionType) { | |||||
let wipeDeviceToken = false; | |||||
for (const update of action.payload.updatesResult.newUpdates) { | |||||
if ( | |||||
update.type === updateTypes.BAD_DEVICE_TOKEN && | |||||
update.deviceToken === state.deviceToken | |||||
) { | |||||
wipeDeviceToken = true; | |||||
break; | |||||
} | |||||
} | |||||
if (wipeDeviceToken) { | |||||
state = { | |||||
...state, | |||||
deviceToken: null, | |||||
}; | |||||
} | |||||
} | } | ||||
if (action.type === setStoreLoadedActionType) { | if (action.type === setStoreLoadedActionType) { | ||||
return { | return { | ||||
...state, | ...state, | ||||
storeLoaded: true, | storeLoaded: true, | ||||
}; | }; | ||||
} | } | ||||
if (action.type === setClientDBStoreActionType) { | if (action.type === setClientDBStoreActionType) { | ||||
Show All 12 Lines | if (action.type === setClientDBStoreActionType) { | ||||
) { | ) { | ||||
// If user is logged out now, was logged out at the time action was | // If user is logged out now, was logged out at the time action was | ||||
// dispatched or their ID changed between action dispatch and a | // dispatched or their ID changed between action dispatch and a | ||||
// call to reducer we ignore the SQLite data since it is not valid | // call to reducer we ignore the SQLite data since it is not valid | ||||
return state; | return state; | ||||
} | } | ||||
} | } | ||||
state = { | |||||
...state, | |||||
deviceToken: reduceDeviceToken(state.deviceToken, action), | |||||
}; | |||||
const baseReducerResult = baseReducer(state, (action: BaseAction)); | const baseReducerResult = baseReducer(state, (action: BaseAction)); | ||||
state = baseReducerResult.state; | state = baseReducerResult.state; | ||||
const { storeOperations } = baseReducerResult; | const { storeOperations } = baseReducerResult; | ||||
const { | const { | ||||
draftStoreOperations, | draftStoreOperations, | ||||
threadStoreOperations, | threadStoreOperations, | ||||
messageStoreOperations, | messageStoreOperations, | ||||
▲ Show 20 Lines • Show All 181 Lines • Show Last 20 Lines |