From 3075ccb4b9b897bb666d3acb0ddc34c8117786f5 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 17 Mar 2019 09:20:02 +0100 Subject: [PATCH] Refactored shortUrlCreation reducer to takle advantage of redux-actions --- src/short-urls/reducers/shortUrlCreation.js | 35 +++++---------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/src/short-urls/reducers/shortUrlCreation.js b/src/short-urls/reducers/shortUrlCreation.js index 8a6d772f..6188feff 100644 --- a/src/short-urls/reducers/shortUrlCreation.js +++ b/src/short-urls/reducers/shortUrlCreation.js @@ -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);