2020-10-02 17:03:25 +02:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2020-10-05 20:31:51 +02:00
|
|
|
import { ACTIONS as LBRY_REDUX_ACTIONS } from 'lbry-redux';
|
2020-10-02 17:03:25 +02:00
|
|
|
|
|
|
|
const reducers = {};
|
|
|
|
const defaultState = {
|
|
|
|
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,
|
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
|
|
|
};
|
|
|
|
|
2020-10-05 20:31:51 +02:00
|
|
|
reducers[LBRY_REDUX_ACTIONS.USER_STATE_POPULATE] = state => {
|
|
|
|
const { syncReady } = state;
|
|
|
|
if (!syncReady) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
prefsReady: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Object.assign({}, state);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
reducers[ACTIONS.SET_PREFS_READY] = (state, action) => Object.assign({}, state, { prefsReady: action.data });
|
|
|
|
|
2020-10-02 17:03:25 +02:00
|
|
|
reducers[ACTIONS.GET_SYNC_STARTED] = state =>
|
|
|
|
Object.assign({}, state, {
|
|
|
|
getSyncIsPending: true,
|
|
|
|
getSyncErrorMessage: null,
|
|
|
|
});
|
|
|
|
|
2020-10-05 20:31:51 +02:00
|
|
|
reducers[ACTIONS.SET_SYNC_LOCK] = (state, action) =>
|
|
|
|
Object.assign({}, state, {
|
|
|
|
syncLocked: action.data,
|
|
|
|
});
|
|
|
|
|
2020-10-02 17:03:25 +02:00
|
|
|
reducers[ACTIONS.GET_SYNC_COMPLETED] = (state, action) =>
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
reducers[ACTIONS.GET_SYNC_FAILED] = (state, action) =>
|
|
|
|
Object.assign({}, state, {
|
|
|
|
getSyncIsPending: false,
|
|
|
|
getSyncErrorMessage: action.data.error,
|
|
|
|
});
|
|
|
|
|
|
|
|
reducers[ACTIONS.SET_SYNC_STARTED] = state =>
|
|
|
|
Object.assign({}, state, {
|
|
|
|
setSyncIsPending: true,
|
|
|
|
setSyncErrorMessage: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
reducers[ACTIONS.SET_SYNC_FAILED] = (state, action) =>
|
|
|
|
Object.assign({}, state, {
|
|
|
|
setSyncIsPending: false,
|
|
|
|
setSyncErrorMessage: action.data.error,
|
|
|
|
});
|
|
|
|
|
|
|
|
reducers[ACTIONS.SET_SYNC_COMPLETED] = (state, action) =>
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
reducers[ACTIONS.SYNC_APPLY_STARTED] = state =>
|
|
|
|
Object.assign({}, state, {
|
|
|
|
syncApplyPasswordError: false,
|
|
|
|
syncApplyIsPending: true,
|
|
|
|
syncApplyErrorMessage: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
reducers[ACTIONS.SYNC_APPLY_COMPLETED] = state =>
|
|
|
|
Object.assign({}, state, {
|
|
|
|
syncApplyIsPending: false,
|
|
|
|
syncApplyErrorMessage: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
reducers[ACTIONS.SYNC_APPLY_FAILED] = (state, action) =>
|
|
|
|
Object.assign({}, state, {
|
|
|
|
syncApplyIsPending: false,
|
|
|
|
syncApplyErrorMessage: action.data.error,
|
|
|
|
});
|
|
|
|
|
|
|
|
reducers[ACTIONS.SYNC_APPLY_BAD_PASSWORD] = state =>
|
|
|
|
Object.assign({}, state, {
|
|
|
|
syncApplyPasswordError: true,
|
|
|
|
});
|
|
|
|
|
2020-11-12 20:47:39 +01:00
|
|
|
reducers[LBRY_REDUX_ACTIONS.SYNC_FATAL_ERROR] = (state, action) => {
|
|
|
|
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;
|
|
|
|
|
|
|
|
export default function syncReducer(state = defaultState, action) {
|
|
|
|
const handler = reducers[action.type];
|
|
|
|
if (handler) return handler(state, action);
|
|
|
|
return state;
|
|
|
|
}
|