Refactored shortUrlDeletion reducer to takle advantage of redux-actions

This commit is contained in:
Alejandro Celaya 2019-03-17 09:27:01 +01:00
parent 3075ccb4b9
commit 4654bff737

View file

@ -1,3 +1,4 @@
import { createAction, handleActions } from 'redux-actions';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
/* eslint-disable padding-line-between-statements, newline-after-var */ /* eslint-disable padding-line-between-statements, newline-after-var */
@ -25,34 +26,12 @@ const defaultState = {
errorData: {}, errorData: {},
}; };
export default function reducer(state = defaultState, action) { export default handleActions({
switch (action.type) { [DELETE_SHORT_URL_START]: (state) => ({ ...state, loading: true, error: false }),
case DELETE_SHORT_URL_START: [DELETE_SHORT_URL_ERROR]: (state, { errorData }) => ({ ...state, errorData, loading: false, error: true }),
return { [DELETE_SHORT_URL]: (state, { shortCode }) => ({ ...state, shortCode, loading: false, error: false }),
...state, [RESET_DELETE_SHORT_URL]: () => defaultState,
loading: true, }, defaultState);
error: false,
};
case DELETE_SHORT_URL_ERROR:
return {
...state,
loading: false,
error: true,
errorData: action.errorData,
};
case DELETE_SHORT_URL:
return {
...state,
shortCode: action.shortCode,
loading: false,
error: false,
};
case RESET_DELETE_SHORT_URL:
return defaultState;
default:
return state;
}
}
export const deleteShortUrl = (buildShlinkApiClient) => (shortCode) => async (dispatch, getState) => { export const deleteShortUrl = (buildShlinkApiClient) => (shortCode) => async (dispatch, getState) => {
dispatch({ type: DELETE_SHORT_URL_START }); dispatch({ type: DELETE_SHORT_URL_START });
@ -70,6 +49,6 @@ export const deleteShortUrl = (buildShlinkApiClient) => (shortCode) => async (di
} }
}; };
export const resetDeleteShortUrl = () => ({ type: RESET_DELETE_SHORT_URL }); export const resetDeleteShortUrl = createAction(RESET_DELETE_SHORT_URL);
export const shortUrlDeleted = (shortCode) => ({ type: SHORT_URL_DELETED, shortCode }); export const shortUrlDeleted = (shortCode) => ({ type: SHORT_URL_DELETED, shortCode });