shlink-web-client/src/short-urls/reducers/shortUrlsList.js

64 lines
1.9 KiB
JavaScript
Raw Normal View History

import { assoc, assocPath } from 'ramda';
import PropTypes from 'prop-types';
import shlinkApiClient from '../../api/ShlinkApiClient';
import { SHORT_URL_TAGS_EDITED } from './shortUrlTags';
2018-06-15 22:49:25 +03:00
/* eslint-disable padding-line-between-statements, newline-after-var */
2018-07-29 20:25:22 +03:00
const LIST_SHORT_URLS_START = 'shlink/shortUrlsList/LIST_SHORT_URLS_START';
const LIST_SHORT_URLS_ERROR = 'shlink/shortUrlsList/LIST_SHORT_URLS_ERROR';
export const LIST_SHORT_URLS = 'shlink/shortUrlsList/LIST_SHORT_URLS';
/* eslint-enable padding-line-between-statements, newline-after-var */
export const shortUrlType = PropTypes.shape({
tags: PropTypes.arrayOf(PropTypes.string),
shortCode: PropTypes.string,
originalUrl: PropTypes.string,
});
const initialState = {
2018-08-05 09:13:12 +03:00
shortUrls: {},
loading: true,
};
export default function reducer(state = initialState, action) {
2018-06-15 22:49:25 +03:00
switch (action.type) {
case LIST_SHORT_URLS_START:
return { ...state, loading: true, error: false };
2018-06-15 22:49:25 +03:00
case LIST_SHORT_URLS:
return {
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) =>
shortUrl.shortCode === action.shortCode
? assoc('tags', action.tags, shortUrl)
: shortUrl), state);
2018-06-15 22:49:25 +03:00
default:
return state;
}
}
export const _listShortUrls = (shlinkApiClient, params = {}) => async (dispatch) => {
dispatch({ type: LIST_SHORT_URLS_START });
try {
const shortUrls = await shlinkApiClient.listShortUrls(params);
dispatch({ type: LIST_SHORT_URLS, shortUrls, params });
} catch (e) {
dispatch({ type: LIST_SHORT_URLS_ERROR, params });
}
};
export const listShortUrls = (params = {}) => _listShortUrls(shlinkApiClient, params);