Refactored shortUrlCreation reducer to takle advantage of redux-actions

This commit is contained in:
Alejandro Celaya 2019-03-17 09:20:02 +01:00
parent 4894ab9035
commit 3075ccb4b9

View file

@ -1,4 +1,5 @@
import PropTypes from 'prop-types';
import { createAction, handleActions } from 'redux-actions';
/* eslint-disable padding-line-between-statements, newline-after-var */
export const CREATE_SHORT_URL_START = 'shlink/createShortUrl/CREATE_SHORT_URL_START';
@ -21,32 +22,12 @@ const defaultState = {
error: false,
};
export default function reducer(state = defaultState, action) {
switch (action.type) {
case CREATE_SHORT_URL_START:
return {
...state,
saving: true,
error: false,
};
case CREATE_SHORT_URL_ERROR:
return {
...state,
saving: false,
error: true,
};
case CREATE_SHORT_URL:
return {
result: action.result,
saving: false,
error: false,
};
case RESET_CREATE_SHORT_URL:
return defaultState;
default:
return state;
}
}
export default handleActions({
[CREATE_SHORT_URL_START]: (state) => ({ ...state, saving: true, error: false }),
[CREATE_SHORT_URL_ERROR]: (state) => ({ ...state, saving: false, error: true }),
[CREATE_SHORT_URL]: (state, { result }) => ({ result, saving: false, error: false }),
[RESET_CREATE_SHORT_URL]: () => defaultState,
}, defaultState);
export const createShortUrl = (buildShlinkApiClient) => (data) => async (dispatch, getState) => {
dispatch({ type: CREATE_SHORT_URL_START });
@ -63,4 +44,4 @@ export const createShortUrl = (buildShlinkApiClient) => (data) => async (dispatc
}
};
export const resetCreateShortUrl = () => ({ type: RESET_CREATE_SHORT_URL });
export const resetCreateShortUrl = createAction(RESET_CREATE_SHORT_URL);