Append number to duplicate names

This commit is contained in:
Gregory Schier 2016-03-20 16:39:46 -07:00
parent 9882f7f0db
commit d354436462
3 changed files with 31 additions and 2 deletions

View File

@ -37,7 +37,7 @@ function buildRequest (request) {
} }
export function addRequest (name = 'My Request') { export function addRequest (name = 'My Request') {
return (dispatch) => { return (dispatch, getState) => {
dispatch(loadStart()); dispatch(loadStart());
const request = buildRequest({name}); const request = buildRequest({name});
dispatch({type: types.REQUEST_ADD, request}); dispatch({type: types.REQUEST_ADD, request});

View File

@ -53,6 +53,24 @@ describe('Requests Reducer', () => {
}); });
}); });
it('should add request of same name', () => {
initialState.all.push(request);
const newRequest = Object.assign({}, request);
expect(
reducer(initialState, {
type: types.REQUEST_ADD,
request: newRequest
})
).toEqual({
all: [
request,
Object.assign(newRequest, {name: `${request.name} (1)`})
],
active: request.id
});
});
it('should update request', () => { it('should update request', () => {
const state = reducer(undefined, { const state = reducer(undefined, {
type: types.REQUEST_ADD, type: types.REQUEST_ADD,

View File

@ -8,7 +8,18 @@ const initialState = {
function requestsReducer (state = [], action) { function requestsReducer (state = [], action) {
switch (action.type) { switch (action.type) {
case types.REQUEST_ADD: case types.REQUEST_ADD:
return [...state, action.request];
// Change name if there is a duplicate
const request = action.request;
for (let i = 0; ; i++) {
let name = i === 0 ? request.name : request.name + ` (${i})`;
if (!state.find(r => r.name === name)) {
request.name = name;
break;
}
}
return [...state, request];
case types.REQUEST_UPDATE: case types.REQUEST_UPDATE:
return state.map(request => { return state.map(request => {
if (request.id === action.patch.id) { if (request.id === action.patch.id) {