mirror of
https://github.com/Kong/insomnia
synced 2024-11-07 22:30:15 +00:00
Append number to duplicate names
This commit is contained in:
parent
9882f7f0db
commit
d354436462
@ -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});
|
||||
|
@ -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,
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user