From 4654bff73707a0d378cc0701a824c321de09a882 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 17 Mar 2019 09:27:01 +0100 Subject: [PATCH] Refactored shortUrlDeletion reducer to takle advantage of redux-actions --- src/short-urls/reducers/shortUrlDeletion.js | 37 +++++---------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/src/short-urls/reducers/shortUrlDeletion.js b/src/short-urls/reducers/shortUrlDeletion.js index 6d2f2f52..74980bc7 100644 --- a/src/short-urls/reducers/shortUrlDeletion.js +++ b/src/short-urls/reducers/shortUrlDeletion.js @@ -1,3 +1,4 @@ +import { createAction, handleActions } from 'redux-actions'; import PropTypes from 'prop-types'; /* eslint-disable padding-line-between-statements, newline-after-var */ @@ -25,34 +26,12 @@ const defaultState = { errorData: {}, }; -export default function reducer(state = defaultState, action) { - switch (action.type) { - case DELETE_SHORT_URL_START: - return { - ...state, - loading: true, - 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 default handleActions({ + [DELETE_SHORT_URL_START]: (state) => ({ ...state, loading: true, error: false }), + [DELETE_SHORT_URL_ERROR]: (state, { errorData }) => ({ ...state, errorData, loading: false, error: true }), + [DELETE_SHORT_URL]: (state, { shortCode }) => ({ ...state, shortCode, loading: false, error: false }), + [RESET_DELETE_SHORT_URL]: () => defaultState, +}, defaultState); export const deleteShortUrl = (buildShlinkApiClient) => (shortCode) => async (dispatch, getState) => { 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 });