diff --git a/src/reducers/files.js b/src/reducers/files.js index 6b1b068..071695e 100644 --- a/src/reducers/files.js +++ b/src/reducers/files.js @@ -1,24 +1,3 @@ -import { - UPDATE_RESOURCES, - RESOURCES_REMOVED -} 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; -} +import reducer from './resource'; +const files = reducer("file"); +export default files; diff --git a/src/reducers/index.js b/src/reducers/index.js index 6fd06f2..2fed523 100644 --- a/src/reducers/index.js +++ b/src/reducers/index.js @@ -6,6 +6,9 @@ import filter_subscribe from './filter_subscribe'; import server from './server'; import torrents from './torrents'; import files from './files'; +import peers from './peers'; +import trackers from './trackers'; +import pieces from './pieces'; const root = combineReducers({ selection, @@ -14,6 +17,9 @@ const root = combineReducers({ server, torrents, files, + peers, + trackers, + pieces, router: routerReducer }); diff --git a/src/reducers/peers.js b/src/reducers/peers.js new file mode 100644 index 0000000..1c39569 --- /dev/null +++ b/src/reducers/peers.js @@ -0,0 +1,3 @@ +import reducer from './resource'; +const peers = reducer("peer"); +export default peers; diff --git a/src/reducers/pieces.js b/src/reducers/pieces.js new file mode 100644 index 0000000..a235164 --- /dev/null +++ b/src/reducers/pieces.js @@ -0,0 +1,3 @@ +import reducer from './resource'; +const pieces = reducer("piece"); +export default pieces; diff --git a/src/reducers/resource.js b/src/reducers/resource.js new file mode 100644 index 0000000..3b5a6b7 --- /dev/null +++ b/src/reducers/resource.js @@ -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; + }; +} diff --git a/src/reducers/selection.js b/src/reducers/selection.js index e12abd4..456cf94 100644 --- a/src/reducers/selection.js +++ b/src/reducers/selection.js @@ -1,4 +1,5 @@ import { UNION, SUBTRACT, EXCLUSIVE, NONE } from '../actions/selection'; +import { RESOURCES_REMOVED } from '../actions/resources'; export default function selection(state = [], action) { const { ids } = action; @@ -9,6 +10,8 @@ export default function selection(state = [], action) { return state.filter(id => ids.indexOf(id) === -1); case EXCLUSIVE: return [...ids]; + case RESOURCES_REMOVED: + return state.filter(id => ids.indexOf(id) === -1); } return state; } diff --git a/src/reducers/subscribe.js b/src/reducers/subscribe.js index af4c475..09e6194 100644 --- a/src/reducers/subscribe.js +++ b/src/reducers/subscribe.js @@ -1,4 +1,5 @@ import { SUBSCRIBE, UNSUBSCRIBE } from '../actions/subscribe'; +import { RESOURCES_REMOVED } from '../actions/resources'; export default function subscribe(state = [], action) { switch (action.type) { @@ -10,6 +11,8 @@ export default function subscribe(state = [], action) { const { ids } = action; return state.filter(sub => ids.indexOf(sub.id) === -1); } + case RESOURCES_REMOVED: + return state.filter(sub => ids.indexOf(sub.id) === -1); } return state; } diff --git a/src/reducers/torrents.js b/src/reducers/torrents.js index 8c8bcd2..8ecff2b 100644 --- a/src/reducers/torrents.js +++ b/src/reducers/torrents.js @@ -1,24 +1,3 @@ -import { - UPDATE_RESOURCES, - RESOURCES_REMOVED -} 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; -} +import reducer from './resource'; +const torrents = reducer("torrent"); +export default torrents; diff --git a/src/reducers/trackers.js b/src/reducers/trackers.js new file mode 100644 index 0000000..84b9e06 --- /dev/null +++ b/src/reducers/trackers.js @@ -0,0 +1,3 @@ +import reducer from './resource'; +const trackers = reducer("tracker"); +export default trackers;