Some minor refactorings

This commit is contained in:
Alejandro Celaya 2020-04-27 10:52:19 +02:00
parent 277b5e43f8
commit 8b2cbf7aea
4 changed files with 12 additions and 12 deletions

View file

@ -1,5 +1,5 @@
import { combineReducers } from 'redux';
import serversReducer from '../servers/reducers/server';
import serversReducer from '../servers/reducers/servers';
import selectedServerReducer from '../servers/reducers/selectedServer';
import shortUrlsListReducer from '../short-urls/reducers/shortUrlsList';
import shortUrlsListParamsReducer from '../short-urls/reducers/shortUrlsListParams';

View file

@ -3,21 +3,21 @@ import { pipe, isEmpty, assoc, map, prop } from 'ramda';
import { v4 as uuid } from 'uuid';
import { homepage } from '../../../package.json';
export const FETCH_SERVERS = 'shlink/servers/FETCH_SERVERS';
export const LIST_SERVERS = 'shlink/servers/LIST_SERVERS';
const initialState = {};
const assocId = (server) => assoc('id', server.id || uuid(), server);
export default handleActions({
[FETCH_SERVERS]: (state, { list }) => list,
[LIST_SERVERS]: (state, { list }) => list,
}, initialState);
export const listServers = ({ listServers, createServers }, { get }) => () => async (dispatch) => {
const localList = listServers();
if (!isEmpty(localList)) {
dispatch({ type: FETCH_SERVERS, list: localList });
dispatch({ type: LIST_SERVERS, list: localList });
return;
}
@ -39,7 +39,7 @@ export const listServers = ({ listServers, createServers }, { get }) => () => as
.catch(() => []);
createServers(remoteList);
dispatch({ type: FETCH_SERVERS, list: remoteList.reduce((map, server) => ({ ...map, [server.id]: server }), {}) });
dispatch({ type: LIST_SERVERS, list: remoteList.reduce((map, server) => ({ ...map, [server.id]: server }), {}) });
};
export const createServer = ({ createServer }, listServersAction) => pipe(createServer, listServersAction);

View file

@ -6,7 +6,7 @@ import DeleteServerButton from '../DeleteServerButton';
import { EditServer } from '../EditServer';
import ImportServersBtn from '../helpers/ImportServersBtn';
import { resetSelectedServer, selectServer } from '../reducers/selectedServer';
import { createServer, createServers, deleteServer, editServer, listServers } from '../reducers/server';
import { createServer, createServers, deleteServer, editServer, listServers } from '../reducers/servers';
import ForServerVersion from '../helpers/ForServerVersion';
import { ServerError } from '../helpers/ServerError';
import ServersImporter from './ServersImporter';

View file

@ -5,15 +5,15 @@ import reducer, {
listServers,
createServers,
editServer,
FETCH_SERVERS,
} from '../../../src/servers/reducers/server';
LIST_SERVERS,
} from '../../../src/servers/reducers/servers';
describe('serverReducer', () => {
const list = {
abc123: { id: 'abc123' },
def456: { id: 'def456' },
};
const expectedFetchServersResult = { type: FETCH_SERVERS, list };
const expectedFetchServersResult = { type: LIST_SERVERS, list };
const ServersServiceMock = {
listServers: jest.fn(() => list),
createServer: jest.fn(),
@ -25,8 +25,8 @@ describe('serverReducer', () => {
afterEach(jest.clearAllMocks);
describe('reducer', () => {
it('returns servers when action is FETCH_SERVERS', () =>
expect(reducer({}, { type: FETCH_SERVERS, list })).toEqual(list));
it('returns servers when action is LIST_SERVERS', () =>
expect(reducer({}, { type: LIST_SERVERS, list })).toEqual(list));
});
describe('action creators', () => {
@ -89,7 +89,7 @@ describe('serverReducer', () => {
await listServers(NoListServersServiceMock, axios)()(dispatch);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: FETCH_SERVERS, list: expectedList });
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_SERVERS, list: expectedList });
expect(NoListServersServiceMock.listServers).toHaveBeenCalledTimes(1);
expect(NoListServersServiceMock.createServer).not.toHaveBeenCalled();
expect(NoListServersServiceMock.editServer).not.toHaveBeenCalled();