2018-06-15 22:49:25 +03:00
|
|
|
import ShlinkApiClient from '../../api/ShlinkApiClient';
|
|
|
|
|
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';
|
2018-07-15 11:28:39 +03:00
|
|
|
export const LIST_SHORT_URLS = 'shlink/shortUrlsList/LIST_SHORT_URLS';
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
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-07-15 11:39:05 +03:00
|
|
|
shortUrls: action.shortUrls
|
|
|
|
};
|
2018-07-24 20:17:01 +03:00
|
|
|
case LIST_SHORT_URLS_ERROR:
|
|
|
|
return {
|
|
|
|
loading: false,
|
|
|
|
error: true,
|
|
|
|
shortUrls: []
|
|
|
|
};
|
2018-06-15 22:49:25 +03:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 19:49:57 +03:00
|
|
|
export const _listShortUrls = (ShlinkApiClient, params = {}) => async dispatch => {
|
2018-07-28 11:41:05 +03:00
|
|
|
dispatch({ type: LIST_SHORT_URLS_START });
|
2018-07-15 11:28:39 +03:00
|
|
|
|
2018-07-28 11:41:05 +03:00
|
|
|
try {
|
|
|
|
const shortUrls = await ShlinkApiClient.listShortUrls(params);
|
|
|
|
dispatch({ type: LIST_SHORT_URLS, shortUrls, params });
|
|
|
|
} catch (e) {
|
|
|
|
dispatch({ type: LIST_SHORT_URLS_ERROR, params });
|
|
|
|
}
|
2018-06-17 18:12:16 +03:00
|
|
|
};
|
2018-08-12 20:07:42 +03:00
|
|
|
export const listShortUrls = (params = {}) => _listShortUrls(ShlinkApiClient, params);
|
2018-08-15 20:10:35 +03:00
|
|
|
|
|
|
|
export const _refreshShortUrls = ShlinkApiClient => async (dispatch, getState) => {
|
|
|
|
const { shortUrlsListParams } = getState();
|
|
|
|
await _listShortUrls(ShlinkApiClient, shortUrlsListParams)(dispatch);
|
|
|
|
};
|
|
|
|
export const refreshShortUrls = () => _refreshShortUrls(ShlinkApiClient);
|