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

47 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-06-15 22:49:25 +03:00
import ServersService from '../../servers/services';
import ShlinkApiClient from '../../api/ShlinkApiClient';
export const LIST_SHORT_URLS_START = 'shlink/shortUrlsList/LIST_SHORT_URLS_START';
export const LIST_SHORT_URLS = 'shlink/shortUrlsList/LIST_SHORT_URLS';
export const UPDATE_SHORT_URLS_LIST = 'shlink/shortUrlsList/UPDATE_SHORT_URLS_LIST';
const initialState = {
shortUrls: [],
loading: true,
};
export default function reducer(state = initialState, action) {
2018-06-15 22:49:25 +03:00
switch (action.type) {
case LIST_SHORT_URLS_START:
return { ...state, loading: true };
2018-06-15 22:49:25 +03:00
case LIST_SHORT_URLS:
case UPDATE_SHORT_URLS_LIST:
return {
loading: false,
shortUrls: action.shortUrls
};
2018-06-15 22:49:25 +03:00
default:
return state;
}
}
export const listShortUrls = (serverId, params = {}) => {
2018-06-15 22:49:25 +03:00
return async dispatch => {
2018-07-21 11:38:54 +03:00
dispatch({ type: LIST_SHORT_URLS_START });
2018-06-15 22:49:25 +03:00
const selectedServer = ServersService.findServerById(serverId);
ShlinkApiClient.setConfig(selectedServer);
const shortUrls = await ShlinkApiClient.listShortUrls(params);
dispatch({ type: LIST_SHORT_URLS, shortUrls, selectedServer, params });
2018-06-15 22:49:25 +03:00
};
};
export const updateShortUrlsList = (params = {}) => {
return async dispatch => {
dispatch({ type: LIST_SHORT_URLS_START });
const shortUrls = await ShlinkApiClient.listShortUrls(params);
dispatch({ type: UPDATE_SHORT_URLS_LIST, shortUrls, params });
};
};