Migrate servers reducer to typescript

This commit is contained in:
Alejandro Celaya
2020-08-23 10:20:31 +02:00
parent 87e64e5899
commit dc78138066
4 changed files with 59 additions and 43 deletions

View File

@@ -1,9 +1,10 @@
import { MercureInfo } from '../mercure/reducers/mercureInfo'; import { MercureInfo } from '../mercure/reducers/mercureInfo';
import { ServersMap } from '../servers/reducers/servers';
export type ConnectDecorator = (props: string[], actions?: string[]) => any; export type ConnectDecorator = (props: string[], actions?: string[]) => any;
export interface ShlinkState { export interface ShlinkState {
servers: any; servers: ServersMap;
selectedServer: any; selectedServer: any;
shortUrlsList: any; shortUrlsList: any;
shortUrlsListParams: any; shortUrlsListParams: any;

View File

@@ -1,35 +0,0 @@
import { handleActions } from 'redux-actions';
import { pipe, assoc, map, reduce, dissoc } from 'ramda';
import { v4 as uuid } from 'uuid';
/* eslint-disable padding-line-between-statements */
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 = {};
const assocId = (server) => assoc('id', server.id || uuid(), server);
export default handleActions({
[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 createServer = (server) => createServers([ server ]);
const serversListToMap = reduce((acc, server) => assoc(server.id, server, acc), {});
export const createServers = pipe(
map(assocId),
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 });

View File

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

View File

@@ -1,4 +1,5 @@
import { values } from 'ramda'; import { values } from 'ramda';
import { Mock } from 'ts-mockery';
import reducer, { import reducer, {
createServer, createServer,
deleteServer, deleteServer,
@@ -8,11 +9,12 @@ import reducer, {
DELETE_SERVER, DELETE_SERVER,
CREATE_SERVERS, CREATE_SERVERS,
} from '../../../src/servers/reducers/servers'; } from '../../../src/servers/reducers/servers';
import { RegularServer } from '../../../src/servers/data';
describe('serverReducer', () => { describe('serverReducer', () => {
const list = { const list = {
abc123: { id: 'abc123' }, abc123: Mock.of<RegularServer>({ id: 'abc123' }),
def456: { id: 'def456' }, def456: Mock.of<RegularServer>({ id: 'def456' }),
}; };
afterEach(jest.clearAllMocks); afterEach(jest.clearAllMocks);
@@ -21,14 +23,14 @@ describe('serverReducer', () => {
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, serverId: 'abc123', serverData: { foo: 'foo' } }, { type: EDIT_SERVER, serverId: 'abc123', serverData: { foo: 'foo' } } as any,
)).toEqual({ )).toEqual({
abc123: { id: 'abc123', foo: 'foo' }, abc123: { id: 'abc123', foo: 'foo' },
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' })).toEqual({ expect(reducer(list, { type: DELETE_SERVER, serverId: 'abc123' } as any)).toEqual({
def456: { id: 'def456' }, def456: { id: 'def456' },
})); }));
@@ -38,7 +40,7 @@ describe('serverReducer', () => {
newServers: { newServers: {
ghi789: { id: 'ghi789' }, ghi789: { id: 'ghi789' },
}, },
})).toEqual({ } as any)).toEqual({
abc123: { id: 'abc123' }, abc123: { id: 'abc123' },
def456: { id: 'def456' }, def456: { id: 'def456' },
ghi789: { id: 'ghi789' }, ghi789: { id: 'ghi789' },
@@ -48,7 +50,7 @@ describe('serverReducer', () => {
describe('action creators', () => { describe('action creators', () => {
describe('createServer', () => { describe('createServer', () => {
it('returns expected action', () => { it('returns expected action', () => {
const serverToCreate = { id: 'abc123' }; const serverToCreate = Mock.of<RegularServer>({ id: 'abc123' });
const result = createServer(serverToCreate); const result = createServer(serverToCreate);
expect(result).toEqual(expect.objectContaining({ type: CREATE_SERVERS })); expect(result).toEqual(expect.objectContaining({ type: CREATE_SERVERS }));
@@ -66,7 +68,7 @@ describe('serverReducer', () => {
describe('deleteServer', () => { describe('deleteServer', () => {
it('returns expected action', () => { it('returns expected action', () => {
const serverToDelete = { 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, serverId: 'abc123' });