Refactored shortUrlsList to take advantage of redux-actions

This commit is contained in:
Alejandro Celaya 2019-03-17 09:11:37 +01:00
parent 51b5f6264d
commit 4a09d99322

View file

@ -1,13 +1,14 @@
import { handleActions } from 'redux-actions';
import { assoc, assocPath, propEq, reject } from 'ramda'; import { assoc, assocPath, propEq, reject } from 'ramda';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { SHORT_URL_TAGS_EDITED } from './shortUrlTags'; import { SHORT_URL_TAGS_EDITED } from './shortUrlTags';
import { SHORT_URL_DELETED } from './shortUrlDeletion'; import { SHORT_URL_DELETED } from './shortUrlDeletion';
/* eslint-disable padding-line-between-statements, newline-after-var */ /* eslint-disable padding-line-between-statements */
export const LIST_SHORT_URLS_START = 'shlink/shortUrlsList/LIST_SHORT_URLS_START'; export const LIST_SHORT_URLS_START = 'shlink/shortUrlsList/LIST_SHORT_URLS_START';
export const LIST_SHORT_URLS_ERROR = 'shlink/shortUrlsList/LIST_SHORT_URLS_ERROR'; export const LIST_SHORT_URLS_ERROR = 'shlink/shortUrlsList/LIST_SHORT_URLS_ERROR';
export const LIST_SHORT_URLS = 'shlink/shortUrlsList/LIST_SHORT_URLS'; export const LIST_SHORT_URLS = 'shlink/shortUrlsList/LIST_SHORT_URLS';
/* eslint-enable padding-line-between-statements, newline-after-var */ /* eslint-enable padding-line-between-statements */
export const shortUrlType = PropTypes.shape({ export const shortUrlType = PropTypes.shape({
shortCode: PropTypes.string, shortCode: PropTypes.string,
@ -22,39 +23,24 @@ const initialState = {
error: false, error: false,
}; };
export default function reducer(state = initialState, action) { export default handleActions({
switch (action.type) { [LIST_SHORT_URLS_START]: (state) => ({ ...state, loading: true, error: false }),
case LIST_SHORT_URLS_START: [LIST_SHORT_URLS]: (state, { shortUrls }) => ({ loading: false, error: false, shortUrls }),
return { ...state, loading: true, error: false }; [LIST_SHORT_URLS_ERROR]: () => ({ loading: false, error: true, shortUrls: {} }),
case LIST_SHORT_URLS: [SHORT_URL_TAGS_EDITED]: (state, action) => { // eslint-disable-line object-shorthand
return { const { data } = state.shortUrls;
loading: false,
error: false,
shortUrls: action.shortUrls,
};
case LIST_SHORT_URLS_ERROR:
return {
loading: false,
error: true,
shortUrls: {},
};
case SHORT_URL_TAGS_EDITED:
const { data } = state.shortUrls;
return assocPath([ 'shortUrls', 'data' ], data.map((shortUrl) => return assocPath([ 'shortUrls', 'data' ], data.map((shortUrl) =>
shortUrl.shortCode === action.shortCode shortUrl.shortCode === action.shortCode
? assoc('tags', action.tags, shortUrl) ? assoc('tags', action.tags, shortUrl)
: shortUrl), state); : shortUrl), state);
case SHORT_URL_DELETED: },
return assocPath( [SHORT_URL_DELETED]: (state, action) => assocPath(
[ 'shortUrls', 'data' ], [ 'shortUrls', 'data' ],
reject(propEq('shortCode', action.shortCode), state.shortUrls.data), reject(propEq('shortCode', action.shortCode), state.shortUrls.data),
state, state,
); ),
default: }, initialState);
return state;
}
}
export const listShortUrls = (buildShlinkApiClient) => (params = {}) => async (dispatch, getState) => { export const listShortUrls = (buildShlinkApiClient) => (params = {}) => async (dispatch, getState) => {
dispatch({ type: LIST_SHORT_URLS_START }); dispatch({ type: LIST_SHORT_URLS_START });