Tweaked server types and data

This commit is contained in:
Alejandro Celaya
2020-08-23 10:51:42 +02:00
parent dc78138066
commit 1b7e1e2b5b
8 changed files with 64 additions and 38 deletions

View File

@@ -1,7 +1,7 @@
import { handleActions } from 'redux-actions';
import { pipe, assoc, map, reduce, dissoc } from 'ramda';
import { v4 as uuid } from 'uuid';
import { RegularServer, NewServerData } from '../data';
import { NewServerData, ServerWithId } from '../data';
/* eslint-disable padding-line-between-statements */
export const EDIT_SERVER = 'shlink/servers/EDIT_SERVER';
@@ -9,13 +9,13 @@ export const DELETE_SERVER = 'shlink/servers/DELETE_SERVER';
export const CREATE_SERVERS = 'shlink/servers/CREATE_SERVERS';
/* eslint-enable padding-line-between-statements */
export type ServersMap = Record<string, RegularServer>;
export type ServersMap = Record<string, ServerWithId>;
const initialState: ServersMap = {};
const serverWithId = (server: RegularServer | NewServerData): RegularServer => {
if ((server as RegularServer).id) {
return server as RegularServer;
const serverWithId = (server: ServerWithId | NewServerData): ServerWithId => {
if ((server as ServerWithId).id) {
return server as ServerWithId;
}
return assoc('id', uuid(), server);
@@ -29,7 +29,7 @@ export default handleActions<ServersMap, any>({
: assoc(serverId, { ...state[serverId], ...serverData }, state),
}, initialState);
const serversListToMap = reduce<RegularServer, ServersMap>((acc, server) => assoc(server.id, server, acc), {});
const serversListToMap = reduce<ServerWithId, ServersMap>((acc, server) => assoc(server.id, server, acc), {});
export const createServers = pipe(
map(serverWithId),
@@ -37,7 +37,7 @@ export const createServers = pipe(
(newServers: ServersMap) => ({ type: CREATE_SERVERS, newServers }),
);
export const createServer = (server: RegularServer) => createServers([ server ]);
export const createServer = (server: ServerWithId) => createServers([ server ]);
export const editServer = (serverId: string, serverData: Partial<NewServerData>) => ({
type: EDIT_SERVER,
@@ -45,4 +45,4 @@ export const editServer = (serverId: string, serverData: Partial<NewServerData>)
serverData,
});
export const deleteServer = ({ id }: RegularServer) => ({ type: DELETE_SERVER, serverId: id });
export const deleteServer = ({ id }: ServerWithId) => ({ type: DELETE_SERVER, serverId: id });