2018-08-12 19:49:57 +03:00
|
|
|
import { curry } from 'ramda';
|
2018-08-26 00:39:27 +03:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import shlinkApiClient from '../../api/ShlinkApiClient';
|
2018-09-01 11:33:16 +03:00
|
|
|
import { shortUrlType } from '../../short-urls/reducers/shortUrlsList';
|
2018-07-29 20:25:22 +03:00
|
|
|
|
2018-08-26 11:49:23 +03:00
|
|
|
/* eslint-disable padding-line-between-statements, newline-after-var */
|
2018-07-29 20:25:22 +03:00
|
|
|
const GET_SHORT_URL_VISITS_START = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_START';
|
|
|
|
const GET_SHORT_URL_VISITS_ERROR = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_ERROR';
|
|
|
|
const GET_SHORT_URL_VISITS = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS';
|
2018-08-26 11:49:23 +03:00
|
|
|
/* eslint-enable padding-line-between-statements, newline-after-var */
|
2018-07-29 20:25:22 +03:00
|
|
|
|
2018-08-26 20:19:45 +03:00
|
|
|
export const shortUrlVisitsType = PropTypes.shape({
|
2018-08-26 00:39:27 +03:00
|
|
|
shortUrl: shortUrlType,
|
|
|
|
visits: PropTypes.array,
|
|
|
|
loading: PropTypes.bool,
|
|
|
|
error: PropTypes.bool,
|
2018-08-26 20:19:45 +03:00
|
|
|
});
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2018-07-29 20:25:22 +03:00
|
|
|
const initialState = {
|
2018-07-30 22:34:06 +03:00
|
|
|
shortUrl: {},
|
2018-07-29 20:25:22 +03:00
|
|
|
visits: [],
|
|
|
|
loading: false,
|
2018-08-26 00:39:27 +03:00
|
|
|
error: false,
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
|
|
|
|
2018-08-26 00:39:27 +03:00
|
|
|
export default function dispatch(state = initialState, action) {
|
2018-07-29 20:25:22 +03:00
|
|
|
switch (action.type) {
|
|
|
|
case GET_SHORT_URL_VISITS_START:
|
|
|
|
return {
|
|
|
|
...state,
|
2018-08-26 00:39:27 +03:00
|
|
|
loading: true,
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
|
|
|
case GET_SHORT_URL_VISITS_ERROR:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
loading: false,
|
2018-08-26 00:39:27 +03:00
|
|
|
error: true,
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
|
|
|
case GET_SHORT_URL_VISITS:
|
|
|
|
return {
|
2018-07-30 22:34:06 +03:00
|
|
|
shortUrl: action.shortUrl,
|
2018-07-29 20:25:22 +03:00
|
|
|
visits: action.visits,
|
|
|
|
loading: false,
|
2018-08-26 00:39:27 +03:00
|
|
|
error: false,
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 00:39:27 +03:00
|
|
|
export const _getShortUrlVisits = (shlinkApiClient, shortCode, dates) => (dispatch) => {
|
2018-07-29 20:25:22 +03:00
|
|
|
dispatch({ type: GET_SHORT_URL_VISITS_START });
|
|
|
|
|
2018-07-30 22:34:06 +03:00
|
|
|
Promise.all([
|
2018-08-26 00:39:27 +03:00
|
|
|
shlinkApiClient.getShortUrlVisits(shortCode, dates),
|
|
|
|
shlinkApiClient.getShortUrl(shortCode),
|
2018-07-30 22:34:06 +03:00
|
|
|
])
|
2018-08-26 00:39:27 +03:00
|
|
|
.then(([ visits, shortUrl ]) => dispatch({ visits, shortUrl, type: GET_SHORT_URL_VISITS }))
|
2018-07-30 22:34:06 +03:00
|
|
|
.catch(() => dispatch({ type: GET_SHORT_URL_VISITS_ERROR }));
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
2018-08-26 00:39:27 +03:00
|
|
|
|
|
|
|
export const getShortUrlVisits = curry(_getShortUrlVisits)(shlinkApiClient);
|