2021-12-17 05:08:16 +01:00
|
|
|
// @flow
|
2020-10-02 17:03:25 +02:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
|
|
|
|
|
|
|
const reducers = {};
|
2021-12-17 05:08:16 +01:00
|
|
|
|
|
|
|
const defaultState: SyncState = {
|
2020-10-02 17:03:25 +02:00
|
|
|
hasSyncedWallet: false,
|
|
|
|
syncHash: null,
|
|
|
|
syncData: null,
|
|
|
|
setSyncErrorMessage: null,
|
|
|
|
getSyncErrorMessage: null,
|
|
|
|
syncApplyErrorMessage: '',
|
|
|
|
syncApplyIsPending: false,
|
|
|
|
syncApplyPasswordError: false,
|
|
|
|
getSyncIsPending: false,
|
|
|
|
setSyncIsPending: false,
|
2020-10-05 20:31:51 +02:00
|
|
|
prefsReady: false,
|
|
|
|
syncLocked: false,
|
2021-12-23 09:27:03 +01:00
|
|
|
sharedStateSyncId: -1,
|
2020-10-02 17:03:25 +02:00
|
|
|
hashChanged: false,
|
2020-11-12 18:38:28 +01:00
|
|
|
fatalError: false,
|
2020-10-02 17:03:25 +02:00
|
|
|
};
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.USER_STATE_POPULATE] = (state: SyncState) => {
|
|
|
|
// $FlowFixMe - 'syncReady' doesn't exist. A bug?
|
2020-10-05 20:31:51 +02:00
|
|
|
const { syncReady } = state;
|
|
|
|
if (!syncReady) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
prefsReady: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Object.assign({}, state);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.SET_PREFS_READY] = (state: SyncState, action: any) =>
|
|
|
|
Object.assign({}, state, { prefsReady: action.data });
|
2020-10-05 20:31:51 +02:00
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.GET_SYNC_STARTED] = (state: SyncState) =>
|
2020-10-02 17:03:25 +02:00
|
|
|
Object.assign({}, state, {
|
|
|
|
getSyncIsPending: true,
|
|
|
|
getSyncErrorMessage: null,
|
|
|
|
});
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.SET_SYNC_LOCK] = (state: SyncState, action: any) =>
|
2020-10-05 20:31:51 +02:00
|
|
|
Object.assign({}, state, {
|
|
|
|
syncLocked: action.data,
|
|
|
|
});
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.GET_SYNC_COMPLETED] = (state: SyncState, action: any) =>
|
2020-10-02 17:03:25 +02:00
|
|
|
Object.assign({}, state, {
|
|
|
|
syncHash: action.data.syncHash,
|
|
|
|
syncData: action.data.syncData,
|
|
|
|
hasSyncedWallet: action.data.hasSyncedWallet,
|
|
|
|
getSyncIsPending: false,
|
|
|
|
hashChanged: action.data.hashChanged,
|
2020-11-12 18:38:28 +01:00
|
|
|
fatalError: action.data.fatalError,
|
2020-10-02 17:03:25 +02:00
|
|
|
});
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.GET_SYNC_FAILED] = (state: SyncState, action: any) =>
|
2020-10-02 17:03:25 +02:00
|
|
|
Object.assign({}, state, {
|
|
|
|
getSyncIsPending: false,
|
|
|
|
getSyncErrorMessage: action.data.error,
|
|
|
|
});
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.SET_SYNC_STARTED] = (state: SyncState) =>
|
2020-10-02 17:03:25 +02:00
|
|
|
Object.assign({}, state, {
|
|
|
|
setSyncIsPending: true,
|
|
|
|
setSyncErrorMessage: null,
|
|
|
|
});
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.SET_SYNC_FAILED] = (state: SyncState, action: any) =>
|
2020-10-02 17:03:25 +02:00
|
|
|
Object.assign({}, state, {
|
|
|
|
setSyncIsPending: false,
|
|
|
|
setSyncErrorMessage: action.data.error,
|
|
|
|
});
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.SET_SYNC_COMPLETED] = (state: SyncState, action: any) =>
|
2020-10-02 17:03:25 +02:00
|
|
|
Object.assign({}, state, {
|
|
|
|
setSyncIsPending: false,
|
|
|
|
setSyncErrorMessage: null,
|
|
|
|
hasSyncedWallet: true, // sync was successful, so the user has a synced wallet at this point
|
|
|
|
syncHash: action.data.syncHash,
|
|
|
|
});
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.SYNC_APPLY_STARTED] = (state: SyncState) =>
|
2020-10-02 17:03:25 +02:00
|
|
|
Object.assign({}, state, {
|
|
|
|
syncApplyPasswordError: false,
|
|
|
|
syncApplyIsPending: true,
|
|
|
|
syncApplyErrorMessage: '',
|
|
|
|
});
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.SYNC_APPLY_COMPLETED] = (state: SyncState) =>
|
2020-10-02 17:03:25 +02:00
|
|
|
Object.assign({}, state, {
|
|
|
|
syncApplyIsPending: false,
|
|
|
|
syncApplyErrorMessage: '',
|
|
|
|
});
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.SYNC_APPLY_FAILED] = (state: SyncState, action: any) =>
|
2020-10-02 17:03:25 +02:00
|
|
|
Object.assign({}, state, {
|
|
|
|
syncApplyIsPending: false,
|
|
|
|
syncApplyErrorMessage: action.data.error,
|
|
|
|
});
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.SYNC_APPLY_BAD_PASSWORD] = (state: SyncState) =>
|
2020-10-02 17:03:25 +02:00
|
|
|
Object.assign({}, state, {
|
|
|
|
syncApplyPasswordError: true,
|
|
|
|
});
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
reducers[ACTIONS.SYNC_FATAL_ERROR] = (state: SyncState) => {
|
2020-11-12 20:47:39 +01:00
|
|
|
return Object.assign({}, state, {
|
2020-11-12 19:32:18 +01:00
|
|
|
fatalError: true,
|
|
|
|
});
|
2020-11-12 20:47:39 +01:00
|
|
|
};
|
2020-11-12 19:32:18 +01:00
|
|
|
|
2020-10-02 17:03:25 +02:00
|
|
|
reducers[ACTIONS.SYNC_RESET] = () => defaultState;
|
|
|
|
|
2021-12-23 09:27:03 +01:00
|
|
|
reducers[ACTIONS.SHARED_STATE_SYNC_ID_CHANGED] = (state: SyncState, action: any) => {
|
|
|
|
return { ...state, sharedStateSyncId: action.data };
|
|
|
|
};
|
|
|
|
|
2021-12-17 05:08:16 +01:00
|
|
|
export default function syncReducer(state: SyncState = defaultState, action: any) {
|
2020-10-02 17:03:25 +02:00
|
|
|
const handler = reducers[action.type];
|
|
|
|
if (handler) return handler(state, action);
|
|
|
|
return state;
|
|
|
|
}
|