From cc414c9b579b2f38a2777cfd899207588a74983b Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 25 Aug 2017 19:59:53 -0400 Subject: [PATCH] Implement select all/none --- src/actions/selection.js | 13 ++++++------- src/reducers/selection.js | 10 ++++------ src/ui/torrent_details.js | 6 +++--- src/ui/torrent_table.js | 18 +++++++++++++++--- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/actions/selection.js b/src/actions/selection.js index 09e88a1..65766ee 100644 --- a/src/actions/selection.js +++ b/src/actions/selection.js @@ -5,12 +5,11 @@ import { push } from 'react-router-redux'; export const UNION = 'UNION'; export const SUBTRACT = 'SUBTRACT'; export const EXCLUSIVE = 'EXCLUSIVE'; -export const NONE = 'NONE'; -export default function selectTorrent(id, action) { +export default function selectTorrent(ids, action) { return (dispatch, getState) => { const previous = new Set(getState().selection); - dispatch({ type: action, id }); + dispatch({ type: action, ids }); const state = getState(); const next = new Set(state.selection); @@ -23,7 +22,7 @@ export default function selectTorrent(id, action) { added.forEach(t => { const criteria = [ - { field: "torrent_id", op: "==", value: id } + { field: "torrent_id", op: "==", value: t } ]; dispatch(filter_subscribe("peer", criteria)); dispatch(filter_subscribe("file", criteria)); @@ -38,7 +37,7 @@ export default function selectTorrent(id, action) { .map(sub => sub.serial); serials.forEach(serial => dispatch(filter_unsubscribe(serial))); /* Remove resource subscriptions */ - const ids = [ + const _ids = [ ...Object.values(files) .filter(file => file.torrent_id === t) .map(file => file.id) @@ -52,8 +51,8 @@ export default function selectTorrent(id, action) { // .filter(piece => piece.torrent_id === t) // .map(piece => piece.id), ]; - if (ids.length > 0) { - dispatch(unsubscribe(...ids)); + if (_ids.length > 0) { + dispatch(unsubscribe(..._ids)); } }); diff --git a/src/reducers/selection.js b/src/reducers/selection.js index cc16cd6..e12abd4 100644 --- a/src/reducers/selection.js +++ b/src/reducers/selection.js @@ -1,16 +1,14 @@ import { UNION, SUBTRACT, EXCLUSIVE, NONE } from '../actions/selection'; export default function selection(state = [], action) { - const { id } = action; + const { ids } = action; switch (action.type) { case UNION: - return [id, ...state.filter(t => t !== id)]; + return [...ids, ...state.filter(id => ids.indexOf(id) === -1)]; case SUBTRACT: - return state.filter(t => t !== id); + return state.filter(id => ids.indexOf(id) === -1); case EXCLUSIVE: - return [id]; - case NONE: - return []; + return [...ids]; } return state; } diff --git a/src/ui/torrent_details.js b/src/ui/torrent_details.js index 95fbb1e..5b48ece 100644 --- a/src/ui/torrent_details.js +++ b/src/ui/torrent_details.js @@ -13,7 +13,7 @@ import { Progress } from 'reactstrap'; import ws_send from '../socket'; -import selectTorrent, { UNION, NONE } from '../actions/selection'; +import selectTorrent, { EXCLUSIVE, UNION, NONE } from '../actions/selection'; function File({ file }) { // TODO: show progress bar @@ -153,12 +153,12 @@ class TorrentDetails extends Component { const { dispatch } = this.props; const { ids } = this.props.match.params; const _ids = ids.split(","); - _ids.forEach(id => dispatch(selectTorrent(id, UNION))); + dispatch(selectTorrent(_ids, UNION)); } componentWillUnmount() { const { dispatch } = this.props; - dispatch(selectTorrent(null, NONE)); + dispatch(selectTorrent([], EXCLUSIVE)); } renderHeader(selection) { diff --git a/src/ui/torrent_table.js b/src/ui/torrent_table.js index aae4d16..7dbd51e 100644 --- a/src/ui/torrent_table.js +++ b/src/ui/torrent_table.js @@ -10,7 +10,19 @@ class TorrentTable extends Component { - + @@ -35,7 +47,7 @@ class TorrentTable extends Component { type="checkbox" checked={selection.indexOf(t.id) !== -1} onChange={e => - dispatch(selectTorrent(t.id, e.target.checked ? UNION : SUBTRACT)) + dispatch(selectTorrent([t.id], e.target.checked ? UNION : SUBTRACT)) } /> @@ -44,7 +56,7 @@ class TorrentTable extends Component { href={`/torrents/${t.id}`} onClick={e => { e.preventDefault(); - dispatch(selectTorrent(t.id, EXCLUSIVE)); + dispatch(selectTorrent([t.id], EXCLUSIVE)); }} >{t.name}
+ { + if (selection.length > 0) { + dispatch(selectTorrent([], EXCLUSIVE)); + } else { + dispatch(selectTorrent(Object.keys(torrents), EXCLUSIVE)); + } + }} + /> + name up down