diff --git a/app/actions/requests.js b/app/actions/requests.js index 6d93685e2..a60d1e8d0 100644 --- a/app/actions/requests.js +++ b/app/actions/requests.js @@ -37,7 +37,7 @@ function buildRequest (request) { } export function addRequest (name = 'My Request') { - return (dispatch) => { + return (dispatch, getState) => { dispatch(loadStart()); const request = buildRequest({name}); dispatch({type: types.REQUEST_ADD, request}); diff --git a/app/reducers/__tests__/requests.test.js b/app/reducers/__tests__/requests.test.js index 446aa2297..64afee75c 100644 --- a/app/reducers/__tests__/requests.test.js +++ b/app/reducers/__tests__/requests.test.js @@ -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', () => { const state = reducer(undefined, { type: types.REQUEST_ADD, diff --git a/app/reducers/requests.js b/app/reducers/requests.js index 3c486c65c..4e879672c 100644 --- a/app/reducers/requests.js +++ b/app/reducers/requests.js @@ -8,7 +8,18 @@ const initialState = { function requestsReducer (state = [], action) { switch (action.type) { 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: return state.map(request => { if (request.id === action.patch.id) {