57 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-12-18 18:13:24 +01:00
import { fromJS } from 'immutable';
2020-01-03 10:53:58 +01:00
import { cloneDeep, set, get } from 'lodash';
const initialWebhook = {
name: null,
url: null,
headers: [{ key: '', value: '' }],
};
2019-12-18 18:13:24 +01:00
const initialState = fromJS({
2020-01-03 10:53:58 +01:00
initialWebhook: initialWebhook,
modifiedWebhook: initialWebhook,
2019-12-18 18:13:24 +01:00
shouldRefetchData: false,
});
const reducer = (state, action) => {
switch (action.type) {
2020-01-03 10:53:58 +01:00
case 'GET_DATA_SUCCEEDED': {
const data = cloneDeep(action.data);
const headers = get(data, 'headers');
if (Object.keys(headers).length > 0) {
const newHeaders = fromJS(
Object.keys(headers).map(key => {
return { key: key, value: headers[key] };
})
);
set(data, ['headers'], newHeaders);
}
2019-12-18 18:13:24 +01:00
return state
2020-01-03 10:53:58 +01:00
.update('initialWebhook', () => fromJS(data))
.update('modifiedWebhook', () => fromJS(data))
2019-12-18 18:13:24 +01:00
.update('shouldRefetchData', () => false);
2020-01-03 10:53:58 +01:00
}
case 'ON_CHANGE': {
2020-01-02 09:55:26 +01:00
return state.updateIn(
['modifiedWebhook', ...action.keys],
() => action.value
);
2020-01-03 10:53:58 +01:00
}
case 'ADD_NEW_HEADER':
return state.updateIn(['modifiedWebhook', ...action.keys], arr =>
arr.push(fromJS({ key: '', value: '' }))
);
2020-01-02 09:55:26 +01:00
case 'SET_ERRORS': {
return state.update('formErrors', () => fromJS(action.errors));
}
2019-12-18 18:13:24 +01:00
default:
return state;
}
};
export default reducer;
export { initialState };