Refactored shortUrlVisits reducer to take advantage of redux-actions

This commit is contained in:
Alejandro Celaya 2019-03-17 09:36:07 +01:00
parent 7ff7318089
commit 468e34aa3d

View file

@ -1,13 +1,14 @@
import { createAction, handleActions } from 'redux-actions';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { flatten, prop, range, splitEvery } from 'ramda'; import { flatten, prop, range, splitEvery } from 'ramda';
/* eslint-disable padding-line-between-statements, newline-after-var */ /* eslint-disable padding-line-between-statements */
export const GET_SHORT_URL_VISITS_START = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_START'; export const GET_SHORT_URL_VISITS_START = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_START';
export const GET_SHORT_URL_VISITS_ERROR = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_ERROR'; export const GET_SHORT_URL_VISITS_ERROR = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_ERROR';
export const GET_SHORT_URL_VISITS = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS'; export const GET_SHORT_URL_VISITS = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS';
export const GET_SHORT_URL_VISITS_LARGE = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_LARGE'; export const GET_SHORT_URL_VISITS_LARGE = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_LARGE';
export const GET_SHORT_URL_VISITS_CANCEL = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_CANCEL'; export const GET_SHORT_URL_VISITS_CANCEL = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_CANCEL';
/* eslint-enable padding-line-between-statements, newline-after-var */ /* eslint-enable padding-line-between-statements */
export const shortUrlVisitsType = PropTypes.shape({ export const shortUrlVisitsType = PropTypes.shape({
visits: PropTypes.array, visits: PropTypes.array,
@ -23,45 +24,30 @@ const initialState = {
cancelLoad: false, cancelLoad: false,
}; };
export default function reducer(state = initialState, action) { export default handleActions({
switch (action.type) { [GET_SHORT_URL_VISITS_START]: (state) => ({
case GET_SHORT_URL_VISITS_START: ...state,
return { loading: true,
...state, loadingLarge: false,
loading: true, cancelLoad: false,
loadingLarge: false, }),
cancelLoad: false, [GET_SHORT_URL_VISITS_ERROR]: (state) => ({
}; ...state,
case GET_SHORT_URL_VISITS_ERROR: loading: false,
return { loadingLarge: false,
...state, error: true,
loading: false, cancelLoad: false,
loadingLarge: false, }),
error: true, [GET_SHORT_URL_VISITS]: (state, { visits }) => ({
cancelLoad: false, visits,
}; loading: false,
case GET_SHORT_URL_VISITS: loadingLarge: false,
return { error: false,
visits: action.visits, cancelLoad: false,
loading: false, }),
loadingLarge: false, [GET_SHORT_URL_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }),
error: false, [GET_SHORT_URL_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }),
cancelLoad: false, }, initialState);
};
case GET_SHORT_URL_VISITS_LARGE:
return {
...state,
loadingLarge: true,
};
case GET_SHORT_URL_VISITS_CANCEL:
return {
...state,
cancelLoad: true,
};
default:
return state;
}
}
export const getShortUrlVisits = (buildShlinkApiClient) => (shortCode, dates) => async (dispatch, getState) => { export const getShortUrlVisits = (buildShlinkApiClient) => (shortCode, dates) => async (dispatch, getState) => {
dispatch({ type: GET_SHORT_URL_VISITS_START }); dispatch({ type: GET_SHORT_URL_VISITS_START });
@ -124,4 +110,4 @@ export const getShortUrlVisits = (buildShlinkApiClient) => (shortCode, dates) =>
} }
}; };
export const cancelGetShortUrlVisits = () => ({ type: GET_SHORT_URL_VISITS_CANCEL }); export const cancelGetShortUrlVisits = createAction(GET_SHORT_URL_VISITS_CANCEL);