diff --git a/src/servers/reducers/selectedServer.js b/src/servers/reducers/selectedServer.js index b05443f6..c2ff3ca0 100644 --- a/src/servers/reducers/selectedServer.js +++ b/src/servers/reducers/selectedServer.js @@ -21,9 +21,7 @@ export const selectServer = (serversService) => (serverId) => (dispatch) => { }); }; -const reducer = handleActions({ +export default handleActions({ [RESET_SELECTED_SERVER]: () => defaultState, [SELECT_SERVER]: (state, { selectedServer }) => selectedServer, }, defaultState); - -export default reducer; diff --git a/src/servers/reducers/server.js b/src/servers/reducers/server.js index 44837d15..1a3955f6 100644 --- a/src/servers/reducers/server.js +++ b/src/servers/reducers/server.js @@ -1,33 +1,16 @@ +import { createAction, handleActions } from 'redux-actions'; +import { pipe } from 'ramda'; + export const FETCH_SERVERS = 'shlink/servers/FETCH_SERVERS'; -export default function reducer(state = {}, action) { - switch (action.type) { - case FETCH_SERVERS: - return action.servers; - default: - return state; - } -} +export const listServers = ({ listServers }) => createAction(FETCH_SERVERS, () => listServers()); -export const listServers = (serversService) => () => ({ - type: FETCH_SERVERS, - servers: serversService.listServers(), -}); +export const createServer = ({ createServer }, listServers) => pipe(createServer, listServers); -export const createServer = (serversService, listServers) => (server) => { - serversService.createServer(server); +export const deleteServer = ({ deleteServer }, listServers) => pipe(deleteServer, listServers); - return listServers(); -}; +export const createServers = ({ createServers }, listServers) => pipe(createServers, listServers); -export const deleteServer = (serversService, listServers) => (server) => { - serversService.deleteServer(server); - - return listServers(); -}; - -export const createServers = (serversService, listServers) => (servers) => { - serversService.createServers(servers); - - return listServers(); -}; +export default handleActions({ + [FETCH_SERVERS]: (state, { payload }) => payload, +}, {}); diff --git a/test/servers/reducers/server.test.js b/test/servers/reducers/server.test.js index 99cf30b7..55844f0a 100644 --- a/test/servers/reducers/server.test.js +++ b/test/servers/reducers/server.test.js @@ -9,13 +9,13 @@ import reducer, { } from '../../../src/servers/reducers/server'; describe('serverReducer', () => { - const servers = { + const payload = { abc123: { id: 'abc123' }, def456: { id: 'def456' }, }; - const expectedFetchServersResult = { type: FETCH_SERVERS, servers }; + const expectedFetchServersResult = { type: FETCH_SERVERS, payload }; const ServersServiceMock = { - listServers: sinon.fake.returns(servers), + listServers: sinon.fake.returns(payload), createServer: sinon.fake(), deleteServer: sinon.fake(), createServers: sinon.fake(), @@ -23,7 +23,7 @@ describe('serverReducer', () => { describe('reducer', () => { it('returns servers when action is FETCH_SERVERS', () => - expect(reducer({}, { type: FETCH_SERVERS, servers })).toEqual(servers)); + expect(reducer({}, { type: FETCH_SERVERS, payload })).toEqual(payload)); it('returns default when action is unknown', () => expect(reducer({}, { type: 'unknown' })).toEqual({})); @@ -79,7 +79,7 @@ describe('serverReducer', () => { describe('createServer', () => { it('creates multiple servers and then fetches servers again', () => { - const serversToCreate = values(servers); + const serversToCreate = values(payload); const result = createServers(ServersServiceMock, () => expectedFetchServersResult)(serversToCreate); expect(result).toEqual(expectedFetchServersResult);