mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-10 02:07:26 +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,
|
[RESET_SELECTED_SERVER]: () => defaultState,
|
||||||
[SELECT_SERVER]: (state, { selectedServer }) => selectedServer,
|
[SELECT_SERVER]: (state, { selectedServer }) => selectedServer,
|
||||||
}, defaultState);
|
}, 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 const FETCH_SERVERS = 'shlink/servers/FETCH_SERVERS';
|
||||||
|
|
||||||
export default function reducer(state = {}, action) {
|
export const listServers = ({ listServers }) => createAction(FETCH_SERVERS, () => listServers());
|
||||||
switch (action.type) {
|
|
||||||
case FETCH_SERVERS:
|
|
||||||
return action.servers;
|
|
||||||
default:
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const listServers = (serversService) => () => ({
|
export const createServer = ({ createServer }, listServers) => pipe(createServer, listServers);
|
||||||
type: FETCH_SERVERS,
|
|
||||||
servers: serversService.listServers(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const createServer = (serversService, listServers) => (server) => {
|
export const deleteServer = ({ deleteServer }, listServers) => pipe(deleteServer, listServers);
|
||||||
serversService.createServer(server);
|
|
||||||
|
|
||||||
return listServers();
|
export const createServers = ({ createServers }, listServers) => pipe(createServers, listServers);
|
||||||
};
|
|
||||||
|
|
||||||
export const deleteServer = (serversService, listServers) => (server) => {
|
export default handleActions({
|
||||||
serversService.deleteServer(server);
|
[FETCH_SERVERS]: (state, { payload }) => payload,
|
||||||
|
}, {});
|
||||||
return listServers();
|
|
||||||
};
|
|
||||||
|
|
||||||
export const createServers = (serversService, listServers) => (servers) => {
|
|
||||||
serversService.createServers(servers);
|
|
||||||
|
|
||||||
return listServers();
|
|
||||||
};
|
|
||||||
|
|
|
@ -9,13 +9,13 @@ import reducer, {
|
||||||
} from '../../../src/servers/reducers/server';
|
} from '../../../src/servers/reducers/server';
|
||||||
|
|
||||||
describe('serverReducer', () => {
|
describe('serverReducer', () => {
|
||||||
const servers = {
|
const payload = {
|
||||||
abc123: { id: 'abc123' },
|
abc123: { id: 'abc123' },
|
||||||
def456: { id: 'def456' },
|
def456: { id: 'def456' },
|
||||||
};
|
};
|
||||||
const expectedFetchServersResult = { type: FETCH_SERVERS, servers };
|
const expectedFetchServersResult = { type: FETCH_SERVERS, payload };
|
||||||
const ServersServiceMock = {
|
const ServersServiceMock = {
|
||||||
listServers: sinon.fake.returns(servers),
|
listServers: sinon.fake.returns(payload),
|
||||||
createServer: sinon.fake(),
|
createServer: sinon.fake(),
|
||||||
deleteServer: sinon.fake(),
|
deleteServer: sinon.fake(),
|
||||||
createServers: sinon.fake(),
|
createServers: sinon.fake(),
|
||||||
|
@ -23,7 +23,7 @@ describe('serverReducer', () => {
|
||||||
|
|
||||||
describe('reducer', () => {
|
describe('reducer', () => {
|
||||||
it('returns servers when action is FETCH_SERVERS', () =>
|
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', () =>
|
it('returns default when action is unknown', () =>
|
||||||
expect(reducer({}, { type: 'unknown' })).toEqual({}));
|
expect(reducer({}, { type: 'unknown' })).toEqual({}));
|
||||||
|
@ -79,7 +79,7 @@ describe('serverReducer', () => {
|
||||||
|
|
||||||
describe('createServer', () => {
|
describe('createServer', () => {
|
||||||
it('creates multiple servers and then fetches servers again', () => {
|
it('creates multiple servers and then fetches servers again', () => {
|
||||||
const serversToCreate = values(servers);
|
const serversToCreate = values(payload);
|
||||||
const result = createServers(ServersServiceMock, () => expectedFetchServersResult)(serversToCreate);
|
const result = createServers(ServersServiceMock, () => expectedFetchServersResult)(serversToCreate);
|
||||||
|
|
||||||
expect(result).toEqual(expectedFetchServersResult);
|
expect(result).toEqual(expectedFetchServersResult);
|
||||||
|
|
Loading…
Reference in a new issue