insomnia/app/reducers/requests.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-03-20 04:00:40 +00:00
import * as types from "../constants/actionTypes";
2016-03-16 05:49:42 +00:00
2016-03-20 04:00:40 +00:00
const initialState = {
all: [],
2016-03-20 23:20:00 +00:00
activeId: null
2016-03-20 04:00:40 +00:00
};
2016-03-16 05:49:42 +00:00
2016-03-20 04:00:40 +00:00
function requestsReducer (state = [], action) {
switch (action.type) {
case types.REQUEST_ADD:
return [...state, action.request];
case types.REQUEST_UPDATE:
2016-03-20 20:42:27 +00:00
return state.map(request => {
2016-03-20 23:20:00 +00:00
if (request.id === action.patch.id) {
return Object.assign({}, request, action.patch);
2016-03-20 20:42:27 +00:00
} else {
return request;
}});
2016-03-20 04:00:40 +00:00
default:
return state;
}
2016-03-16 05:49:42 +00:00
}
2016-03-20 04:00:40 +00:00
export default function (state = initialState, action) {
2016-03-20 23:20:00 +00:00
let all, activeId;
2016-03-20 04:00:40 +00:00
switch (action.type) {
case types.REQUEST_ADD:
all = requestsReducer(state.all, action);
2016-03-20 23:20:00 +00:00
activeId = state.activeId || action.request.id;
return Object.assign({}, state, {all, activeId});
2016-03-20 04:00:40 +00:00
case types.REQUEST_UPDATE:
all = requestsReducer(state.all, action);
2016-03-20 23:20:00 +00:00
return Object.assign({}, state, {all});
2016-03-20 04:47:43 +00:00
case types.REQUEST_ACTIVATE:
2016-03-20 23:20:00 +00:00
if (!state.all.find(r => r.id === action.id)) {
// Don't set if the request doesn't exist
return state;
} else {
return Object.assign({}, state, {activeId: action.id});
}
2016-03-20 04:00:40 +00:00
default:
return state
}
2016-03-16 05:49:42 +00:00
}