2018-12-21 12:58:51 +03:00
|
|
|
import { assoc, assocPath, propEq, reject } from 'ramda';
|
2018-08-26 00:39:27 +03:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { SHORT_URL_TAGS_EDITED } from './shortUrlTags';
|
2018-09-16 11:47:17 +03:00
|
|
|
import { SHORT_URL_DELETED } from './shortUrlDeletion';
|
2018-06-15 22:49:25 +03:00
|
|
|
|
2018-08-26 11:49:23 +03:00
|
|
|
/* eslint-disable padding-line-between-statements, newline-after-var */
|
2018-12-21 12:58:51 +03:00
|
|
|
export const LIST_SHORT_URLS_START = 'shlink/shortUrlsList/LIST_SHORT_URLS_START';
|
|
|
|
export const LIST_SHORT_URLS_ERROR = 'shlink/shortUrlsList/LIST_SHORT_URLS_ERROR';
|
2018-07-15 11:28:39 +03:00
|
|
|
export const LIST_SHORT_URLS = 'shlink/shortUrlsList/LIST_SHORT_URLS';
|
2018-08-26 11:49:23 +03:00
|
|
|
/* eslint-enable padding-line-between-statements, newline-after-var */
|
2018-07-15 11:28:39 +03:00
|
|
|
|
2018-08-26 00:39:27 +03:00
|
|
|
export const shortUrlType = PropTypes.shape({
|
|
|
|
tags: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
shortCode: PropTypes.string,
|
|
|
|
originalUrl: PropTypes.string,
|
|
|
|
});
|
|
|
|
|
2018-07-15 11:39:05 +03:00
|
|
|
const initialState = {
|
2018-08-05 09:13:12 +03:00
|
|
|
shortUrls: {},
|
2018-07-15 11:39:05 +03:00
|
|
|
loading: true,
|
2018-12-21 12:58:51 +03:00
|
|
|
error: false,
|
2018-07-15 11:39:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function reducer(state = initialState, action) {
|
2018-06-15 22:49:25 +03:00
|
|
|
switch (action.type) {
|
2018-07-15 11:39:05 +03:00
|
|
|
case LIST_SHORT_URLS_START:
|
2018-07-24 20:17:01 +03:00
|
|
|
return { ...state, loading: true, error: false };
|
2018-06-15 22:49:25 +03:00
|
|
|
case LIST_SHORT_URLS:
|
2018-07-15 11:39:05 +03:00
|
|
|
return {
|
|
|
|
loading: false,
|
2018-07-24 20:17:01 +03:00
|
|
|
error: false,
|
2018-08-26 00:39:27 +03:00
|
|
|
shortUrls: action.shortUrls,
|
2018-07-15 11:39:05 +03:00
|
|
|
};
|
2018-07-24 20:17:01 +03:00
|
|
|
case LIST_SHORT_URLS_ERROR:
|
|
|
|
return {
|
|
|
|
loading: false,
|
|
|
|
error: true,
|
2018-12-21 12:58:51 +03:00
|
|
|
shortUrls: {},
|
2018-07-24 20:17:01 +03:00
|
|
|
};
|
2018-08-18 18:14:33 +03:00
|
|
|
case SHORT_URL_TAGS_EDITED:
|
|
|
|
const { data } = state.shortUrls;
|
2018-08-26 00:39:27 +03:00
|
|
|
|
|
|
|
return assocPath([ 'shortUrls', 'data' ], data.map((shortUrl) =>
|
2018-08-18 18:14:33 +03:00
|
|
|
shortUrl.shortCode === action.shortCode
|
|
|
|
? assoc('tags', action.tags, shortUrl)
|
2018-08-26 00:39:27 +03:00
|
|
|
: shortUrl), state);
|
2018-09-16 11:47:17 +03:00
|
|
|
case SHORT_URL_DELETED:
|
|
|
|
return assocPath(
|
|
|
|
[ 'shortUrls', 'data' ],
|
2018-12-21 12:58:51 +03:00
|
|
|
reject(propEq('shortCode', action.shortCode), state.shortUrls.data),
|
2018-09-16 11:47:17 +03:00
|
|
|
state,
|
|
|
|
);
|
2018-06-15 22:49:25 +03:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 21:45:09 +03:00
|
|
|
export const listShortUrls = (buildShlinkApiClient) => (params = {}) => async (dispatch, getState) => {
|
2018-07-28 11:41:05 +03:00
|
|
|
dispatch({ type: LIST_SHORT_URLS_START });
|
2018-07-15 11:28:39 +03:00
|
|
|
|
2018-12-18 21:45:09 +03:00
|
|
|
const { selectedServer = {} } = getState();
|
2018-12-21 12:58:51 +03:00
|
|
|
const { listShortUrls } = buildShlinkApiClient(selectedServer);
|
2018-12-18 12:14:25 +03:00
|
|
|
|
2018-07-28 11:41:05 +03:00
|
|
|
try {
|
2018-12-21 12:58:51 +03:00
|
|
|
const shortUrls = await listShortUrls(params);
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2018-07-28 11:41:05 +03:00
|
|
|
dispatch({ type: LIST_SHORT_URLS, shortUrls, params });
|
|
|
|
} catch (e) {
|
|
|
|
dispatch({ type: LIST_SHORT_URLS_ERROR, params });
|
|
|
|
}
|
2018-06-17 18:12:16 +03:00
|
|
|
};
|