2016-03-24 05:26:04 +00:00
|
|
|
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) {
|
2016-03-24 05:43:27 +00:00
|
|
|
let all, collapsed;
|
2016-03-24 05:26:04 +00:00
|
|
|
switch (action.type) {
|
|
|
|
case types.REQUEST_GROUP_ADD:
|
|
|
|
all = requestGroupsReducer(state.all, action);
|
2016-03-24 05:43:27 +00:00
|
|
|
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});
|
2016-03-24 05:26:04 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|