mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-09 09:47:28 +03:00
Refactored server reducer, removing duplicated code and taking advantage of redux-actions
This commit is contained in:
parent
724c804971
commit
51b5f6264d
3 changed files with 16 additions and 35 deletions
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
}, {});
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue