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

63 lines
1.8 KiB
JavaScript
Raw Normal View History

import { curry } from 'ramda';
import PropTypes from 'prop-types';
import shlinkApiClient from '../../api/ShlinkApiClient';
import { shortUrlType } from './shortUrlsList';
2018-07-29 20:25:22 +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';
/* eslint-enable padding-line-between-statements, newline-after-var */
2018-07-29 20:25:22 +03:00
export const shortUrlVisitsType = {
shortUrl: shortUrlType,
visits: PropTypes.array,
loading: PropTypes.bool,
error: PropTypes.bool,
};
2018-07-29 20:25:22 +03:00
const initialState = {
shortUrl: {},
2018-07-29 20:25:22 +03:00
visits: [],
loading: false,
error: false,
2018-07-29 20:25:22 +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,
loading: true,
2018-07-29 20:25:22 +03:00
};
case GET_SHORT_URL_VISITS_ERROR:
return {
...state,
loading: false,
error: true,
2018-07-29 20:25:22 +03:00
};
case GET_SHORT_URL_VISITS:
return {
shortUrl: action.shortUrl,
2018-07-29 20:25:22 +03:00
visits: action.visits,
loading: false,
error: false,
2018-07-29 20:25:22 +03:00
};
default:
return state;
}
}
export const _getShortUrlVisits = (shlinkApiClient, shortCode, dates) => (dispatch) => {
2018-07-29 20:25:22 +03:00
dispatch({ type: GET_SHORT_URL_VISITS_START });
Promise.all([
shlinkApiClient.getShortUrlVisits(shortCode, dates),
shlinkApiClient.getShortUrl(shortCode),
])
.then(([ visits, shortUrl ]) => dispatch({ visits, shortUrl, type: GET_SHORT_URL_VISITS }))
.catch(() => dispatch({ type: GET_SHORT_URL_VISITS_ERROR }));
2018-07-29 20:25:22 +03:00
};
export const getShortUrlVisits = curry(_getShortUrlVisits)(shlinkApiClient);