Refactored shortUrlDetail reducer to take advantage of redux-actions

This commit is contained in:
Alejandro Celaya 2019-03-17 09:38:37 +01:00
parent 468e34aa3d
commit fcfab79bed

View file

@ -1,11 +1,12 @@
import { handleActions } from 'redux-actions';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { shortUrlType } from '../../short-urls/reducers/shortUrlsList'; import { shortUrlType } from '../../short-urls/reducers/shortUrlsList';
/* eslint-disable padding-line-between-statements, newline-after-var */ /* eslint-disable padding-line-between-statements */
export const GET_SHORT_URL_DETAIL_START = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_START'; export const GET_SHORT_URL_DETAIL_START = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_START';
export const GET_SHORT_URL_DETAIL_ERROR = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_ERROR'; export const GET_SHORT_URL_DETAIL_ERROR = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_ERROR';
export const GET_SHORT_URL_DETAIL = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL'; export const GET_SHORT_URL_DETAIL = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL';
/* eslint-enable padding-line-between-statements, newline-after-var */ /* eslint-enable padding-line-between-statements */
export const shortUrlDetailType = PropTypes.shape({ export const shortUrlDetailType = PropTypes.shape({
shortUrl: shortUrlType, shortUrl: shortUrlType,
@ -19,29 +20,11 @@ const initialState = {
error: false, error: false,
}; };
export default function reducer(state = initialState, action) { export default handleActions({
switch (action.type) { [GET_SHORT_URL_DETAIL_START]: (state) => ({ ...state, loading: true }),
case GET_SHORT_URL_DETAIL_START: [GET_SHORT_URL_DETAIL_ERROR]: (state) => ({ ...state, loading: false, error: true }),
return { [GET_SHORT_URL_DETAIL]: (state, { shortUrl }) => ({ shortUrl, loading: false, error: false }),
...state, }, initialState);
loading: true,
};
case GET_SHORT_URL_DETAIL_ERROR:
return {
...state,
loading: false,
error: true,
};
case GET_SHORT_URL_DETAIL:
return {
shortUrl: action.shortUrl,
loading: false,
error: false,
};
default:
return state;
}
}
export const getShortUrlDetail = (buildShlinkApiClient) => (shortCode) => async (dispatch, getState) => { export const getShortUrlDetail = (buildShlinkApiClient) => (shortCode) => async (dispatch, getState) => {
dispatch({ type: GET_SHORT_URL_DETAIL_START }); dispatch({ type: GET_SHORT_URL_DETAIL_START });