receptor/src/reducers/resource.js

33 lines
949 B
JavaScript
Raw Normal View History

import { UPDATE_RESOURCES, RESOURCES_REMOVED } from '../actions/resources';
2017-09-08 06:55:47 +02:00
import { SOCKET_STATE, SOCKET_UPDATE } from '../actions/socket';
2017-12-29 20:56:41 +01:00
function hack(old, _new) {
if (old && old.type == "torrent") {
if (old.progress != 1 && _new.progress == 1) {
Notification && new Notification("Download complete: " + _new.name);
}
}
return _new;
}
export default function resourceReducer(type) {
return (state = {}, action) => {
let ns = {...state};
switch (action.type) {
case UPDATE_RESOURCES:
action.resources
2018-03-10 15:48:10 +01:00
.filter(r => r.type === type)
.map(r => ns[r.id] = hack(state[r.id], { ...state[r.id], ...r }));
return ns;
case RESOURCES_REMOVED:
action.ids
.map(id => delete ns[id]);
return ns;
2017-09-08 06:55:47 +02:00
case SOCKET_UPDATE:
const _state = action.state;
return _state === SOCKET_STATE.CONNECTING ? {} : state;
}
return state;
};
}