insomnia/app/reducers/requests.js

37 lines
1.0 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:
return state.map(request => request.id === action.request.id ? action.request : request);
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);
active = state.active.id === action.request.id ? action.request : state.active;
return Object.assign({}, state, {all, active});
2016-03-20 04:47:43 +00:00
case types.REQUEST_ACTIVATE:
active = action.request;
return Object.assign({}, state, {active});
2016-03-20 04:00:40 +00:00
default:
return state
}
2016-03-16 05:49:42 +00:00
}