Flesh out reducers for remaining resources

This commit is contained in:
Drew DeVault 2017-08-26 10:04:28 -04:00
parent 0050f12c65
commit 59c57f55db
9 changed files with 50 additions and 48 deletions

View File

@ -1,24 +1,3 @@
import { import reducer from './resource';
UPDATE_RESOURCES, const files = reducer("file");
RESOURCES_REMOVED export default files;
} from '../actions/resources';
export default function files(state = {}, action) {
switch (action.type) {
case UPDATE_RESOURCES:
return {
...state,
...action.resources
.filter(r => r.type === "file")
.reduce((s, r) => ({
...s,
[r.id]: { ...state[r.id], ...r }
}), {})
};
case RESOURCES_REMOVED:
return Object.values(state)
.filter(r => action.ids.indexOf(r.id) === -1)
.reduce((s, r) => ({ ...s, [r.id]: r }), {});
}
return state;
}

View File

@ -6,6 +6,9 @@ import filter_subscribe from './filter_subscribe';
import server from './server'; import server from './server';
import torrents from './torrents'; import torrents from './torrents';
import files from './files'; import files from './files';
import peers from './peers';
import trackers from './trackers';
import pieces from './pieces';
const root = combineReducers({ const root = combineReducers({
selection, selection,
@ -14,6 +17,9 @@ const root = combineReducers({
server, server,
torrents, torrents,
files, files,
peers,
trackers,
pieces,
router: routerReducer router: routerReducer
}); });

3
src/reducers/peers.js Normal file
View File

@ -0,0 +1,3 @@
import reducer from './resource';
const peers = reducer("peer");
export default peers;

3
src/reducers/pieces.js Normal file
View File

@ -0,0 +1,3 @@
import reducer from './resource';
const pieces = reducer("piece");
export default pieces;

23
src/reducers/resource.js Normal file
View File

@ -0,0 +1,23 @@
import { UPDATE_RESOURCES, RESOURCES_REMOVED } from '../actions/resources';
export default function resourceReducer(type) {
return (state = {}, action) => {
switch (action.type) {
case UPDATE_RESOURCES:
return {
...state,
...action.resources
.filter(r => r.type === type)
.reduce((s, r) => ({
...s,
[r.id]: { ...state[r.id], ...r }
}), {})
};
case RESOURCES_REMOVED:
return Object.values(state)
.filter(r => action.ids.indexOf(r.id) === -1)
.reduce((s, r) => ({ ...s, [r.id]: r }), {});
}
return state;
};
}

View File

@ -1,4 +1,5 @@
import { UNION, SUBTRACT, EXCLUSIVE, NONE } from '../actions/selection'; import { UNION, SUBTRACT, EXCLUSIVE, NONE } from '../actions/selection';
import { RESOURCES_REMOVED } from '../actions/resources';
export default function selection(state = [], action) { export default function selection(state = [], action) {
const { ids } = action; const { ids } = action;
@ -9,6 +10,8 @@ export default function selection(state = [], action) {
return state.filter(id => ids.indexOf(id) === -1); return state.filter(id => ids.indexOf(id) === -1);
case EXCLUSIVE: case EXCLUSIVE:
return [...ids]; return [...ids];
case RESOURCES_REMOVED:
return state.filter(id => ids.indexOf(id) === -1);
} }
return state; return state;
} }

View File

@ -1,4 +1,5 @@
import { SUBSCRIBE, UNSUBSCRIBE } from '../actions/subscribe'; import { SUBSCRIBE, UNSUBSCRIBE } from '../actions/subscribe';
import { RESOURCES_REMOVED } from '../actions/resources';
export default function subscribe(state = [], action) { export default function subscribe(state = [], action) {
switch (action.type) { switch (action.type) {
@ -10,6 +11,8 @@ export default function subscribe(state = [], action) {
const { ids } = action; const { ids } = action;
return state.filter(sub => ids.indexOf(sub.id) === -1); return state.filter(sub => ids.indexOf(sub.id) === -1);
} }
case RESOURCES_REMOVED:
return state.filter(sub => ids.indexOf(sub.id) === -1);
} }
return state; return state;
} }

View File

@ -1,24 +1,3 @@
import { import reducer from './resource';
UPDATE_RESOURCES, const torrents = reducer("torrent");
RESOURCES_REMOVED export default torrents;
} from '../actions/resources';
export default function torrents(state = {}, action) {
switch (action.type) {
case UPDATE_RESOURCES:
return {
...state,
...action.resources
.filter(r => r.type === "torrent")
.reduce((s, r) => ({
...s,
[r.id]: { ...state[r.id], ...r }
}), {})
};
case RESOURCES_REMOVED:
return Object.values(state)
.filter(r => action.ids.indexOf(r.id) === -1)
.reduce((s, r) => ({ ...s, [r.id]: r }), {});
}
return state;
}

3
src/reducers/trackers.js Normal file
View File

@ -0,0 +1,3 @@
import reducer from './resource';
const trackers = reducer("tracker");
export default trackers;