insomnia/app/reducers/requests.js

43 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: [],
active: null
};
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 => {
if (request.id === action.request.id) {
return Object.assign({}, request, action.request);
} 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) {
let all, active;
switch (action.type) {
case types.REQUEST_ADD:
all = requestsReducer(state.all, action);
active = state.active || action.request;
return Object.assign({}, state, {all, active});
case types.REQUEST_UPDATE:
all = requestsReducer(state.all, action);
2016-03-20 20:42:27 +00:00
active = state.active;
active = active && active.id === action.request.id ? action.request : active;
2016-03-20 04:00:40 +00:00
return Object.assign({}, state, {all, active});
2016-03-20 04:47:43 +00:00
case types.REQUEST_ACTIVATE:
active = action.request;
2016-03-20 20:42:27 +00:00
return active ? Object.assign({}, state, {active}) : state;
2016-03-20 04:00:40 +00:00
default:
return state
}
2016-03-16 05:49:42 +00:00
}