2018-07-29 20:25:22 +03:00
|
|
|
import ShlinkApiClient from '../../api/ShlinkApiClient';
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
const initialState = {
|
2018-07-30 22:34:06 +03:00
|
|
|
shortUrl: {},
|
2018-07-29 20:25:22 +03:00
|
|
|
visits: [],
|
|
|
|
loading: false,
|
|
|
|
error: false
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function dispatch (state = initialState, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case GET_SHORT_URL_VISITS_START:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
loading: true
|
|
|
|
};
|
|
|
|
case GET_SHORT_URL_VISITS_ERROR:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
loading: false,
|
|
|
|
error: true
|
|
|
|
};
|
|
|
|
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,
|
|
|
|
error: false
|
|
|
|
};
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 22:34:06 +03:00
|
|
|
export const getShortUrlVisits = (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([
|
|
|
|
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
|
|
|
};
|