AdGuardHome/client/src/reducers/install.js

75 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-01-18 17:17:48 +00:00
import { combineReducers } from 'redux';
import { handleActions } from 'redux-actions';
import { reducer as formReducer } from 'redux-form';
2019-01-21 08:55:39 +00:00
import nanoid from 'nanoid';
2019-01-18 17:17:48 +00:00
import * as actions from '../actions/install';
2019-01-21 08:55:39 +00:00
import { INSTALL_FIRST_STEP } from '../helpers/constants';
2019-01-18 17:17:48 +00:00
const install = handleActions({
[actions.getDefaultAddressesRequest]: state => ({ ...state, processingDefault: true }),
[actions.getDefaultAddressesFailure]: state => ({ ...state, processingDefault: false }),
[actions.getDefaultAddressesSuccess]: (state, { payload }) => {
2019-02-01 16:52:42 +00:00
const values = payload;
values.web.ip = state.web.ip;
values.dns.ip = state.dns.ip;
const newState = { ...state, ...values, processingDefault: false };
2019-01-18 17:17:48 +00:00
return newState;
},
[actions.nextStep]: state => ({ ...state, step: state.step + 1 }),
[actions.prevStep]: state => ({ ...state, step: state.step - 1 }),
[actions.setAllSettingsRequest]: state => ({ ...state, processingSubmit: true }),
[actions.setAllSettingsFailure]: state => ({ ...state, processingSubmit: false }),
[actions.setAllSettingsSuccess]: state => ({ ...state, processingSubmit: false }),
}, {
2019-01-21 08:55:39 +00:00
step: INSTALL_FIRST_STEP,
2019-01-18 17:17:48 +00:00
processingDefault: true,
2019-02-01 16:52:42 +00:00
web: {
ip: '0.0.0.0',
port: 80,
warning: '',
},
dns: {
ip: '0.0.0.0',
port: 53,
warning: '',
},
interfaces: {},
2019-01-18 17:17:48 +00:00
});
2019-01-21 08:55:39 +00:00
const toasts = handleActions({
[actions.addErrorToast]: (state, { payload }) => {
const errorToast = {
id: nanoid(),
message: payload.error.toString(),
type: 'error',
};
const newState = { ...state, notices: [...state.notices, errorToast] };
return newState;
},
[actions.addSuccessToast]: (state, { payload }) => {
const successToast = {
id: nanoid(),
message: payload,
type: 'success',
};
const newState = { ...state, notices: [...state.notices, successToast] };
return newState;
},
[actions.removeToast]: (state, { payload }) => {
const filtered = state.notices.filter(notice => notice.id !== payload);
const newState = { ...state, notices: filtered };
return newState;
},
}, { notices: [] });
2019-01-18 17:17:48 +00:00
export default combineReducers({
install,
2019-01-21 08:55:39 +00:00
toasts,
2019-01-18 17:17:48 +00:00
form: formReducer,
});