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-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,
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|