mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-24 08:43:51 +03:00
Refactored tagsList reducer to take advantage of redux-actions
This commit is contained in:
parent
fcfab79bed
commit
740aacbbf1
1 changed files with 24 additions and 49 deletions
|
@ -1,14 +1,15 @@
|
||||||
|
import { handleActions } from 'redux-actions';
|
||||||
import { isEmpty, reject } from 'ramda';
|
import { isEmpty, reject } from 'ramda';
|
||||||
import { buildShlinkApiClientWithAxios as buildShlinkApiClient } from '../../utils/services/ShlinkApiClientBuilder';
|
import { buildShlinkApiClientWithAxios as buildShlinkApiClient } from '../../utils/services/ShlinkApiClientBuilder';
|
||||||
import { TAG_DELETED } from './tagDelete';
|
import { TAG_DELETED } from './tagDelete';
|
||||||
import { TAG_EDITED } from './tagEdit';
|
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_START = 'shlink/tagsList/LIST_TAGS_START';
|
||||||
const LIST_TAGS_ERROR = 'shlink/tagsList/LIST_TAGS_ERROR';
|
const LIST_TAGS_ERROR = 'shlink/tagsList/LIST_TAGS_ERROR';
|
||||||
const LIST_TAGS = 'shlink/tagsList/LIST_TAGS';
|
const LIST_TAGS = 'shlink/tagsList/LIST_TAGS';
|
||||||
const FILTER_TAGS = 'shlink/tagsList/FILTER_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 = {
|
const defaultState = {
|
||||||
tags: [],
|
tags: [],
|
||||||
|
@ -17,54 +18,28 @@ const defaultState = {
|
||||||
error: false,
|
error: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function reducer(state = defaultState, action) {
|
const renameTag = (oldName, newName) => (tag) => tag === oldName ? newName : tag;
|
||||||
switch (action.type) {
|
const rejectTag = (tags, tagToReject) => reject((tag) => tag === tagToReject, tags);
|
||||||
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,
|
|
||||||
|
|
||||||
// FIXME This should be optimized somehow...
|
export default handleActions({
|
||||||
tags: reject((tag) => tag === action.tag, state.tags),
|
[LIST_TAGS_START]: (state) => ({ ...state, loading: true, error: false }),
|
||||||
filteredTags: reject((tag) => tag === action.tag, state.filteredTags),
|
[LIST_TAGS_ERROR]: (state) => ({ ...state, loading: false, error: true }),
|
||||||
};
|
[LIST_TAGS]: (state, { tags }) => ({ tags, filteredTags: tags, loading: false, error: false }),
|
||||||
case TAG_EDITED:
|
[TAG_DELETED]: (state, { tag }) => ({
|
||||||
const renameTag = (tag) => tag === action.oldName ? action.newName : tag;
|
...state,
|
||||||
|
tags: rejectTag(state.tags, tag),
|
||||||
return {
|
filteredTags: rejectTag(state.filteredTags, tag),
|
||||||
...state,
|
}),
|
||||||
|
[TAG_EDITED]: (state, { oldName, newName }) => ({
|
||||||
// FIXME This should be optimized somehow...
|
...state,
|
||||||
tags: state.tags.map(renameTag).sort(),
|
tags: state.tags.map(renameTag(oldName, newName)).sort(),
|
||||||
filteredTags: state.filteredTags.map(renameTag).sort(),
|
filteredTags: state.filteredTags.map(renameTag(oldName, newName)).sort(),
|
||||||
};
|
}),
|
||||||
case FILTER_TAGS:
|
[FILTER_TAGS]: (state, { searchTerm }) => ({
|
||||||
return {
|
...state,
|
||||||
...state,
|
filteredTags: state.tags.filter((tag) => tag.toLowerCase().match(searchTerm)),
|
||||||
filteredTags: state.tags.filter((tag) => tag.toLowerCase().match(action.searchTerm)),
|
}),
|
||||||
};
|
}, defaultState);
|
||||||
default:
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const _listTags = (buildShlinkApiClient, force = false) => async (dispatch, getState) => {
|
export const _listTags = (buildShlinkApiClient, force = false) => async (dispatch, getState) => {
|
||||||
const { tagsList, selectedServer } = getState();
|
const { tagsList, selectedServer } = getState();
|
||||||
|
|
Loading…
Add table
Reference in a new issue