diff --git a/src/tags/reducers/tagsList.js b/src/tags/reducers/tagsList.js index 9b4fe65e..e7d21df7 100644 --- a/src/tags/reducers/tagsList.js +++ b/src/tags/reducers/tagsList.js @@ -1,14 +1,15 @@ +import { handleActions } from 'redux-actions'; import { isEmpty, reject } from 'ramda'; import { buildShlinkApiClientWithAxios as buildShlinkApiClient } from '../../utils/services/ShlinkApiClientBuilder'; import { TAG_DELETED } from './tagDelete'; import { TAG_EDITED } from './tagEdit'; -/* eslint-disable padding-line-between-statements, newline-after-var */ +/* eslint-disable padding-line-between-statements */ const LIST_TAGS_START = 'shlink/tagsList/LIST_TAGS_START'; const LIST_TAGS_ERROR = 'shlink/tagsList/LIST_TAGS_ERROR'; const LIST_TAGS = 'shlink/tagsList/LIST_TAGS'; const FILTER_TAGS = 'shlink/tagsList/FILTER_TAGS'; -/* eslint-enable padding-line-between-statements, newline-after-var */ +/* eslint-enable padding-line-between-statements */ const defaultState = { tags: [], @@ -17,54 +18,28 @@ const defaultState = { error: false, }; -export default function reducer(state = defaultState, action) { - switch (action.type) { - case LIST_TAGS_START: - return { - ...state, - loading: true, - error: false, - }; - case LIST_TAGS_ERROR: - return { - ...state, - loading: false, - error: true, - }; - case LIST_TAGS: - return { - tags: action.tags, - filteredTags: action.tags, - loading: false, - error: false, - }; - case TAG_DELETED: - return { - ...state, +const renameTag = (oldName, newName) => (tag) => tag === oldName ? newName : tag; +const rejectTag = (tags, tagToReject) => reject((tag) => tag === tagToReject, tags); - // FIXME This should be optimized somehow... - tags: reject((tag) => tag === action.tag, state.tags), - filteredTags: reject((tag) => tag === action.tag, state.filteredTags), - }; - case TAG_EDITED: - const renameTag = (tag) => tag === action.oldName ? action.newName : tag; - - return { - ...state, - - // FIXME This should be optimized somehow... - tags: state.tags.map(renameTag).sort(), - filteredTags: state.filteredTags.map(renameTag).sort(), - }; - case FILTER_TAGS: - return { - ...state, - filteredTags: state.tags.filter((tag) => tag.toLowerCase().match(action.searchTerm)), - }; - default: - return state; - } -} +export default handleActions({ + [LIST_TAGS_START]: (state) => ({ ...state, loading: true, error: false }), + [LIST_TAGS_ERROR]: (state) => ({ ...state, loading: false, error: true }), + [LIST_TAGS]: (state, { tags }) => ({ tags, filteredTags: tags, loading: false, error: false }), + [TAG_DELETED]: (state, { tag }) => ({ + ...state, + tags: rejectTag(state.tags, tag), + filteredTags: rejectTag(state.filteredTags, tag), + }), + [TAG_EDITED]: (state, { oldName, newName }) => ({ + ...state, + tags: state.tags.map(renameTag(oldName, newName)).sort(), + filteredTags: state.filteredTags.map(renameTag(oldName, newName)).sort(), + }), + [FILTER_TAGS]: (state, { searchTerm }) => ({ + ...state, + filteredTags: state.tags.filter((tag) => tag.toLowerCase().match(searchTerm)), + }), +}, defaultState); export const _listTags = (buildShlinkApiClient, force = false) => async (dispatch, getState) => { const { tagsList, selectedServer } = getState();