insomnia/app/reducers/requestgroups.js
Gregory Schier 70e92c4c78 Some updates
2016-03-23 22:43:27 -07:00

58 lines
1.7 KiB
JavaScript

import * as types from "../constants/actionTypes";
const initialState = {
all: [],
collapsed: []
};
function requestGroupsReducer (state = [], action) {
switch (action.type) {
case types.REQUEST_GROUP_ADD:
// Change name if there is a duplicate
const requestGroup = action.requestGroup;
for (let i = 0; ; i++) {
let name = i === 0 ? requestGroup.name : requestGroup.name + ` (${i})`;
if (!state.find(r => r.name === name)) {
requestGroup.name = name;
break;
}
}
return [requestGroup, ...state];
case types.REQUEST_GROUP_UPDATE:
return state.map(requestGroup => {
if (requestGroup.id === action.patch.id) {
return Object.assign({}, requestGroup, action.patch);
} else {
return requestGroup;
}
});
default:
return state;
}
}
export default function (state = initialState, action) {
let all, collapsed;
switch (action.type) {
case types.REQUEST_GROUP_ADD:
all = requestGroupsReducer(state.all, action);
return Object.assign({}, state, {all});
case types.REQUEST_GROUP_DELETE:
// TODO: Remove from collapsed as well
all = state.all.filter(rg => rg.id !== action.id);
return Object.assign({}, state, {all});
case types.REQUEST_GROUP_TOGGLE:
if (state.collapsed.indexOf(action.id) >= 0) {
collapsed = state.collapsed.filter(id => id !== action.id);
} else {
collapsed = [...state.collapsed, action.id]
}
return Object.assign({}, state, {collapsed});
case types.REQUEST_GROUP_UPDATE:
all = requestGroupsReducer(state.all, action);
return Object.assign({}, state, {all});
default:
return state
}
}