Updated list servers action so that it tries to fetch servers from the servers.json file when no local servers are found

This commit is contained in:
Alejandro Celaya
2019-04-28 12:07:09 +02:00
parent 502c8a7e02
commit 20820c47d4
9 changed files with 74 additions and 48 deletions

View File

@@ -10,10 +10,10 @@ const initialState = null;
export const resetSelectedServer = createAction(RESET_SELECTED_SERVER);
export const selectServer = (serversService) => (serverId) => (dispatch) => {
export const selectServer = ({ findServerById }) => (serverId) => (dispatch) => {
dispatch(resetShortUrlParams());
const selectedServer = serversService.findServerById(serverId);
const selectedServer = findServerById(serverId);
dispatch({
type: SELECT_SERVER,

View File

@@ -1,16 +1,51 @@
import { createAction, handleActions } from 'redux-actions';
import { pipe } from 'ramda';
import { handleActions } from 'redux-actions';
import { pipe, isEmpty, assoc, map } from 'ramda';
import { v4 as uuid } from 'uuid';
import { homepage } from '../../../package.json';
/* eslint-disable padding-line-between-statements */
export const FETCH_SERVERS_START = 'shlink/servers/FETCH_SERVERS_START';
export const FETCH_SERVERS = 'shlink/servers/FETCH_SERVERS';
/* eslint-enable padding-line-between-statements */
export const listServers = ({ listServers }) => createAction(FETCH_SERVERS, () => listServers());
const initialState = {
list: {},
loading: false,
};
export const createServer = ({ createServer }, listServers) => pipe(createServer, listServers);
export const deleteServer = ({ deleteServer }, listServers) => pipe(deleteServer, listServers);
export const createServers = ({ createServers }, listServers) => pipe(createServers, listServers);
const assocId = (server) => assoc('id', uuid(), server);
export default handleActions({
[FETCH_SERVERS]: (state, { payload }) => payload,
}, {});
[FETCH_SERVERS_START]: (state) => ({ ...state, loading: true }),
[FETCH_SERVERS]: (state, { list }) => ({ list, loading: false }),
}, initialState);
export const listServers = ({ listServers, createServers }, { get }) => () => async (dispatch) => {
dispatch({ type: FETCH_SERVERS_START });
// Fetch list from local storage.
const localList = listServers();
if (!isEmpty(localList)) {
dispatch({ type: FETCH_SERVERS, list: localList });
return;
}
// If local list is empty, try to fetch it remotely, calculate IDs for every server, and use it
const { data: remoteList } = await get(`${homepage}/servers.json`);
const listWithIds = map(assocId, remoteList);
createServers(listWithIds);
dispatch({ type: FETCH_SERVERS, list: listWithIds });
};
export const createServer = ({ createServer }, listServersAction) => pipe(createServer, listServersAction);
export const deleteServer = ({ deleteServer }, listServersAction) => pipe(deleteServer, listServersAction);
export const createServers = ({ createServers }, listServersAction) => pipe(
map(assocId),
createServers,
listServersAction
);