Migrated server list actions to use payload prop

This commit is contained in:
Alejandro Celaya 2022-11-05 09:24:12 +01:00
parent 62ab86aefa
commit 90ef41b419
3 changed files with 55 additions and 43 deletions

View file

@ -1,6 +1,6 @@
import { PayloadAction } from '@reduxjs/toolkit';
import { assoc, dissoc, fromPairs, map, pipe, reduce, toPairs } from 'ramda'; import { assoc, dissoc, fromPairs, map, pipe, reduce, toPairs } from 'ramda';
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
import { Action } from 'redux';
import { ServerData, ServersMap, ServerWithId } from '../data'; import { ServerData, ServersMap, ServerWithId } from '../data';
import { buildReducer } from '../../utils/helpers/redux'; import { buildReducer } from '../../utils/helpers/redux';
@ -9,18 +9,16 @@ export const DELETE_SERVER = 'shlink/servers/DELETE_SERVER';
export const CREATE_SERVERS = 'shlink/servers/CREATE_SERVERS'; export const CREATE_SERVERS = 'shlink/servers/CREATE_SERVERS';
export const SET_AUTO_CONNECT = 'shlink/servers/SET_AUTO_CONNECT'; export const SET_AUTO_CONNECT = 'shlink/servers/SET_AUTO_CONNECT';
export interface CreateServersAction extends Action<string> { export type CreateServersAction = PayloadAction<ServersMap>;
newServers: ServersMap;
}
interface DeleteServerAction extends Action<string> { type DeleteServerAction = PayloadAction<{
serverId: string; serverId: string;
} }>;
interface SetAutoConnectAction extends Action<string> { type SetAutoConnectAction = PayloadAction<{
serverId: string; serverId: string;
autoConnect: boolean; autoConnect: boolean;
} }>;
const initialState: ServersMap = {}; const initialState: ServersMap = {};
@ -33,12 +31,16 @@ const serverWithId = (server: ServerWithId | ServerData): ServerWithId => {
}; };
export default buildReducer<ServersMap, CreateServersAction & DeleteServerAction & SetAutoConnectAction>({ export default buildReducer<ServersMap, CreateServersAction & DeleteServerAction & SetAutoConnectAction>({
[CREATE_SERVERS]: (state, { newServers }) => ({ ...state, ...newServers }), [CREATE_SERVERS]: (state, { payload: newServers }) => ({ ...state, ...newServers }),
[DELETE_SERVER]: (state, { serverId }) => dissoc(serverId, state), [DELETE_SERVER]: (state, { payload }) => dissoc(payload.serverId, state),
[EDIT_SERVER]: (state, { serverId, serverData }: any) => ( [EDIT_SERVER]: (state, { payload }: any) => {
const { serverId, serverData } = payload;
return (
!state[serverId] ? state : assoc(serverId, { ...state[serverId], ...serverData }, state) !state[serverId] ? state : assoc(serverId, { ...state[serverId], ...serverData }, state)
), );
[SET_AUTO_CONNECT]: (state, { serverId, autoConnect }) => { },
[SET_AUTO_CONNECT]: (state, { payload }) => {
const { serverId, autoConnect } = payload;
if (!state[serverId]) { if (!state[serverId]) {
return state; return state;
} }
@ -61,21 +63,22 @@ const serversListToMap = reduce<ServerWithId, ServersMap>((acc, server) => assoc
export const createServers = pipe( export const createServers = pipe(
map(serverWithId), map(serverWithId),
serversListToMap, serversListToMap,
(newServers: ServersMap) => ({ type: CREATE_SERVERS, newServers }), (newServers: ServersMap) => ({ type: CREATE_SERVERS, payload: newServers }),
); );
export const createServer = (server: ServerWithId) => createServers([server]); export const createServer = (server: ServerWithId) => createServers([server]);
export const editServer = (serverId: string, serverData: Partial<ServerData>) => ({ export const editServer = (serverId: string, serverData: Partial<ServerData>) => ({
type: EDIT_SERVER, type: EDIT_SERVER,
serverId, payload: { serverId, serverData },
serverData,
}); });
export const deleteServer = ({ id }: ServerWithId): DeleteServerAction => ({ type: DELETE_SERVER, serverId: id }); export const deleteServer = ({ id }: ServerWithId): DeleteServerAction => ({
type: DELETE_SERVER,
payload: { serverId: id },
});
export const setAutoConnect = ({ id }: ServerWithId, autoConnect: boolean): SetAutoConnectAction => ({ export const setAutoConnect = ({ id }: ServerWithId, autoConnect: boolean): SetAutoConnectAction => ({
type: SET_AUTO_CONNECT, type: SET_AUTO_CONNECT,
serverId: id, payload: { serverId: id, autoConnect },
autoConnect,
}); });

View file

@ -87,7 +87,7 @@ describe('remoteServersReducer', () => {
await fetchServers(axios)()(dispatch); await fetchServers(axios)()(dispatch);
expect(dispatch).toHaveBeenCalledWith({ type: CREATE_SERVERS, newServers: expectedNewServers }); expect(dispatch).toHaveBeenCalledWith({ type: CREATE_SERVERS, payload: expectedNewServers });
expect(get).toHaveBeenCalledTimes(1); expect(get).toHaveBeenCalledTimes(1);
}); });
}); });

View file

@ -13,7 +13,7 @@ import reducer, {
} from '../../../src/servers/reducers/servers'; } from '../../../src/servers/reducers/servers';
import { RegularServer } from '../../../src/servers/data'; import { RegularServer } from '../../../src/servers/data';
describe('serverReducer', () => { describe('serversReducer', () => {
const list = { const list = {
abc123: Mock.of<RegularServer>({ id: 'abc123' }), abc123: Mock.of<RegularServer>({ id: 'abc123' }),
def456: Mock.of<RegularServer>({ id: 'def456' }), def456: Mock.of<RegularServer>({ id: 'def456' }),
@ -23,32 +23,35 @@ describe('serverReducer', () => {
describe('reducer', () => { describe('reducer', () => {
it('returns edited server when action is EDIT_SERVER', () => it('returns edited server when action is EDIT_SERVER', () =>
expect(reducer( expect(reducer(list, {
list, type: EDIT_SERVER,
{ type: EDIT_SERVER, serverId: 'abc123', serverData: { foo: 'foo' } } as any, payload: { serverId: 'abc123', serverData: { foo: 'foo' } },
)).toEqual({ } as any)).toEqual({
abc123: { id: 'abc123', foo: 'foo' }, abc123: { id: 'abc123', foo: 'foo' },
def456: { id: 'def456' }, def456: { id: 'def456' },
})); }));
it('returns as it is when action is EDIT_SERVER and server does not exist', () => it('returns as it is when action is EDIT_SERVER and server does not exist', () =>
expect(reducer( expect(reducer(list, {
list, type: EDIT_SERVER,
{ type: EDIT_SERVER, serverId: 'invalid', serverData: { foo: 'foo' } } as any, payload: { serverId: 'invalid', serverData: { foo: 'foo' } },
)).toEqual({ } as any)).toEqual({
abc123: { id: 'abc123' }, abc123: { id: 'abc123' },
def456: { id: 'def456' }, def456: { id: 'def456' },
})); }));
it('removes server when action is DELETE_SERVER', () => it('removes server when action is DELETE_SERVER', () =>
expect(reducer(list, { type: DELETE_SERVER, serverId: 'abc123' } as any)).toEqual({ expect(reducer(list, {
type: DELETE_SERVER,
payload: { serverId: 'abc123' },
} as any)).toEqual({
def456: { id: 'def456' }, def456: { id: 'def456' },
})); }));
it('appends server when action is CREATE_SERVERS', () => it('appends server when action is CREATE_SERVERS', () =>
expect(reducer(list, { expect(reducer(list, {
type: CREATE_SERVERS, type: CREATE_SERVERS,
newServers: { payload: {
ghi789: { id: 'ghi789' }, ghi789: { id: 'ghi789' },
}, },
} as any)).toEqual({ } as any)).toEqual({
@ -63,8 +66,7 @@ describe('serverReducer', () => {
])('returns state as it is when trying to set auto-connect on invalid server', (autoConnect) => ])('returns state as it is when trying to set auto-connect on invalid server', (autoConnect) =>
expect(reducer(list, { expect(reducer(list, {
type: SET_AUTO_CONNECT, type: SET_AUTO_CONNECT,
serverId: 'invalid', payload: { serverId: 'invalid', autoConnect },
autoConnect,
} as any)).toEqual({ } as any)).toEqual({
abc123: { id: 'abc123' }, abc123: { id: 'abc123' },
def456: { id: 'def456' }, def456: { id: 'def456' },
@ -78,8 +80,7 @@ describe('serverReducer', () => {
expect(reducer(listWithDisabledAutoConnect, { expect(reducer(listWithDisabledAutoConnect, {
type: SET_AUTO_CONNECT, type: SET_AUTO_CONNECT,
serverId: 'abc123', payload: { serverId: 'abc123', autoConnect: false },
autoConnect: false,
} as any)).toEqual({ } as any)).toEqual({
abc123: { id: 'abc123', autoConnect: false }, abc123: { id: 'abc123', autoConnect: false },
def456: { id: 'def456' }, def456: { id: 'def456' },
@ -94,8 +95,7 @@ describe('serverReducer', () => {
expect(reducer(listWithEnabledAutoConnect, { expect(reducer(listWithEnabledAutoConnect, {
type: SET_AUTO_CONNECT, type: SET_AUTO_CONNECT,
serverId: 'def456', payload: { serverId: 'def456', autoConnect: true },
autoConnect: true,
} as any)).toEqual({ } as any)).toEqual({
abc123: { id: 'abc123', autoConnect: false }, abc123: { id: 'abc123', autoConnect: false },
def456: { id: 'def456', autoConnect: true }, def456: { id: 'def456', autoConnect: true },
@ -118,7 +118,10 @@ describe('serverReducer', () => {
const serverData = { name: 'edited' }; const serverData = { name: 'edited' };
const result = editServer('123', serverData); const result = editServer('123', serverData);
expect(result).toEqual({ type: EDIT_SERVER, serverId: '123', serverData }); expect(result).toEqual({
type: EDIT_SERVER,
payload: { serverId: '123', serverData },
});
}); });
}); });
@ -127,7 +130,10 @@ describe('serverReducer', () => {
const serverToDelete = Mock.of<RegularServer>({ id: 'abc123' }); const serverToDelete = Mock.of<RegularServer>({ id: 'abc123' });
const result = deleteServer(serverToDelete); const result = deleteServer(serverToDelete);
expect(result).toEqual({ type: DELETE_SERVER, serverId: 'abc123' }); expect(result).toEqual({
type: DELETE_SERVER,
payload: { serverId: 'abc123' },
});
}); });
}); });
@ -141,9 +147,9 @@ describe('serverReducer', () => {
it('generates an id for every provided server if they do not have it', () => { it('generates an id for every provided server if they do not have it', () => {
const servers = values(list).map(dissoc('id')); const servers = values(list).map(dissoc('id'));
const { newServers } = createServers(servers); const { payload } = createServers(servers);
expect(values(newServers).every(({ id }) => !!id)).toEqual(true); expect(values(payload).every(({ id }) => !!id)).toEqual(true);
}); });
}); });
@ -155,7 +161,10 @@ describe('serverReducer', () => {
const serverToEdit = Mock.of<RegularServer>({ id: 'abc123' }); const serverToEdit = Mock.of<RegularServer>({ id: 'abc123' });
const result = setAutoConnect(serverToEdit, autoConnect); const result = setAutoConnect(serverToEdit, autoConnect);
expect(result).toEqual({ type: SET_AUTO_CONNECT, serverId: 'abc123', autoConnect }); expect(result).toEqual({
type: SET_AUTO_CONNECT,
payload: { serverId: 'abc123', autoConnect },
});
}); });
}); });
}); });