Converted server handling actions into regular actions

This commit is contained in:
Alejandro Celaya
2020-04-27 11:30:51 +02:00
parent 8b2cbf7aea
commit bcf5dcf180
5 changed files with 70 additions and 56 deletions

View File

@@ -1,9 +1,14 @@
import { handleActions } from 'redux-actions';
import { pipe, isEmpty, assoc, map, prop } from 'ramda';
import { pipe, isEmpty, assoc, map, prop, reduce, dissoc } from 'ramda';
import { v4 as uuid } from 'uuid';
import { homepage } from '../../../package.json';
/* eslint-disable padding-line-between-statements */
export const LIST_SERVERS = 'shlink/servers/LIST_SERVERS';
export const EDIT_SERVER = 'shlink/servers/EDIT_SERVER';
export const DELETE_SERVER = 'shlink/servers/DELETE_SERVER';
export const CREATE_SERVERS = 'shlink/servers/CREATE_SERVERS';
/* eslint-enable padding-line-between-statements */
const initialState = {};
@@ -11,6 +16,11 @@ const assocId = (server) => assoc('id', server.id || uuid(), server);
export default handleActions({
[LIST_SERVERS]: (state, { list }) => list,
[CREATE_SERVERS]: (state, { newServers }) => ({ ...state, ...newServers }),
[DELETE_SERVER]: (state, { serverId }) => dissoc(serverId, state),
[EDIT_SERVER]: (state, { serverId, serverData }) => !state[serverId]
? state
: assoc(serverId, { ...state[serverId], ...serverData }, state),
}, initialState);
export const listServers = ({ listServers, createServers }, { get }) => () => async (dispatch) => {
@@ -42,14 +52,16 @@ export const listServers = ({ listServers, createServers }, { get }) => () => as
dispatch({ type: LIST_SERVERS, list: remoteList.reduce((map, server) => ({ ...map, [server.id]: server }), {}) });
};
export const createServer = ({ createServer }, listServersAction) => pipe(createServer, listServersAction);
export const createServer = (server) => createServers([ server ]);
export const editServer = ({ editServer }, listServersAction) => pipe(editServer, listServersAction);
const serversListToMap = reduce((acc, server) => assoc(server.id, server, acc), {});
export const deleteServer = ({ deleteServer }, listServersAction) => pipe(deleteServer, listServersAction);
export const createServers = ({ createServers }, listServersAction) => pipe(
export const createServers = pipe(
map(assocId),
createServers,
listServersAction
serversListToMap,
(newServers) => ({ type: CREATE_SERVERS, newServers })
);
export const editServer = (serverId, serverData) => ({ type: EDIT_SERVER, serverId, serverData });
export const deleteServer = ({ id }) => ({ type: DELETE_SERVER, serverId: id });