From 349ea8518f68cc1b09f0c74dfd1b2be43f280563 Mon Sep 17 00:00:00 2001 From: Brett Sun Date: Tue, 8 Dec 2015 14:55:04 +0100 Subject: [PATCH] Destructure params for fetchEditionList --- js/actions/edition_list_actions.js | 5 ++--- .../accordion_list_item_edition_widget.js | 11 +++++----- .../accordion_list_item_table_editions.js | 20 ++++++++++++----- .../ascribe_buttons/create_editions_button.js | 22 ++++++++++--------- js/fetchers/edition_list_fetcher.js | 2 +- js/stores/edition_list_store.js | 17 +++++++++----- 6 files changed, 46 insertions(+), 31 deletions(-) diff --git a/js/actions/edition_list_actions.js b/js/actions/edition_list_actions.js index 6f9881ee..1ea76eb1 100644 --- a/js/actions/edition_list_actions.js +++ b/js/actions/edition_list_actions.js @@ -17,7 +17,7 @@ class EditionListActions { ); } - fetchEditionList(pieceId, page, pageSize, orderBy, orderAsc, filterBy) { + fetchEditionList({pieceId, page, pageSize, orderBy, orderAsc, filterBy}) { if((!orderBy && typeof orderAsc === 'undefined') || !orderAsc) { orderBy = 'edition_number'; orderAsc = true; @@ -31,7 +31,7 @@ class EditionListActions { return Q.Promise((resolve, reject) => { EditionListFetcher - .fetch(pieceId, page, pageSize, orderBy, orderAsc, filterBy) + .fetch({pieceId, page, pageSize, orderBy, orderAsc, filterBy}) .then((res) => { if(res && !res.editions) { throw new Error('Piece has no editions to fetch.'); @@ -54,7 +54,6 @@ class EditionListActions { reject(err); }); }); - } } diff --git a/js/components/ascribe_accordion_list/accordion_list_item_edition_widget.js b/js/components/ascribe_accordion_list/accordion_list_item_edition_widget.js index 8033f239..31ae1a6e 100644 --- a/js/components/ascribe_accordion_list/accordion_list_item_edition_widget.js +++ b/js/components/ascribe_accordion_list/accordion_list_item_edition_widget.js @@ -50,14 +50,15 @@ let AccordionListItemEditionWidget = React.createClass({ * Calls the store to either show or hide the editionListTable */ toggleTable() { - let pieceId = this.props.piece.id; - let isEditionListOpen = this.state.isEditionListOpenForPieceId[pieceId] ? this.state.isEditionListOpenForPieceId[pieceId].show : false; - + const { piece: { id: pieceId } } = this.props; + const { filterBy, isEditionListOpenForPieceId } = this.state; + const isEditionListOpen = isEditionListOpenForPieceId[pieceId] ? isEditionListOpenForPieceId[pieceId].show : false; + if(isEditionListOpen) { EditionListActions.toggleEditionList(pieceId); } else { EditionListActions.toggleEditionList(pieceId); - EditionListActions.fetchEditionList(pieceId, null, null, null, null, this.state.filterBy); + EditionListActions.fetchEditionList({pieceId, filterBy}); } }, @@ -68,7 +69,7 @@ let AccordionListItemEditionWidget = React.createClass({ getGlyphicon() { let pieceId = this.props.piece.id; let isEditionListOpen = this.state.isEditionListOpenForPieceId[pieceId] ? this.state.isEditionListOpenForPieceId[pieceId].show : false; - + if(isEditionListOpen) { // this is the loading feedback for the editions // button. diff --git a/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js b/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js index 23cfb239..036cc143 100644 --- a/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js +++ b/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js @@ -66,20 +66,28 @@ let AccordionListItemTableEditions = React.createClass({ }, filterSelectedEditions() { - let selectedEditions = this.state.editionList[this.props.parentId] - .filter((edition) => edition.selected); - return selectedEditions; + return this.state + .editionList[this.props.parentId] + .filter((edition) => edition.selected); }, loadFurtherEditions() { + const { parentId: pieceId } = this.props; + const { page, pageSize, orderBy, orderAsc, filterBy } = this.state.editionList[pieceId]; + // trigger loading animation this.setState({ showMoreLoading: true }); - let editionList = this.state.editionList[this.props.parentId]; - EditionListActions.fetchEditionList(this.props.parentId, editionList.page + 1, editionList.pageSize, - editionList.orderBy, editionList.orderAsc, editionList.filterBy); + EditionListActions.fetchEditionList({ + pieceId, + page: page + 1, + pageSize, + orderBy, + orderAsc, + filterBy + }); }, render() { let selectedEditionsCount = 0; diff --git a/js/components/ascribe_buttons/create_editions_button.js b/js/components/ascribe_buttons/create_editions_button.js index 8a0da40b..c78b0d63 100644 --- a/js/components/ascribe_buttons/create_editions_button.js +++ b/js/components/ascribe_buttons/create_editions_button.js @@ -49,16 +49,18 @@ let CreateEditionsButton = React.createClass({ // requests, will try to merge the filterBy parameter with other parameters (mergeOptions). // Therefore it can't but null but instead has to be an empty object - EditionListActions.fetchEditionList(this.props.piece.id, null, null, null, null, {}) - .then((res) => { - - clearInterval(this.state.pollingIntervalIndex); - this.props.onPollingSuccess(this.props.piece.id, res.editions[0].num_editions); - - }) - .catch((err) => { - /* Ignore and keep going */ - }); + EditionListActions + .fetchEditionList({ + pieceId: this.props.piece.id, + filterBy: {} + }) + .then((res) => { + clearInterval(this.state.pollingIntervalIndex); + this.props.onPollingSuccess(this.props.piece.id, res.editions[0].num_editions); + }) + .catch((err) => { + /* Ignore and keep going */ + }); }, 5000); this.setState({ diff --git a/js/fetchers/edition_list_fetcher.js b/js/fetchers/edition_list_fetcher.js index 93e4553d..342efbff 100644 --- a/js/fetchers/edition_list_fetcher.js +++ b/js/fetchers/edition_list_fetcher.js @@ -9,7 +9,7 @@ let EditionListFetcher = { /** * Fetches a list of editions from the API. */ - fetch(pieceId, page, pageSize, orderBy, orderAsc, filterBy) { + fetch({pieceId, page, pageSize, orderBy, orderAsc, filterBy}) { let ordering = generateOrderingQueryParams(orderBy, orderAsc); let queryParams = mergeOptions( diff --git a/js/stores/edition_list_store.js b/js/stores/edition_list_store.js index 107f9af4..dcc49213 100644 --- a/js/stores/edition_list_store.js +++ b/js/stores/edition_list_store.js @@ -32,7 +32,7 @@ class EditionListStore { // page let storeEditionIndex = (page - 1) * pageSize + i; let editionsForPieces = this.editionList[pieceId]; - + // if edition already exists, just merge if(editionsForPieces[storeEditionIndex]) { editionsForPieces[storeEditionIndex] = React.addons.update(editionsForPieces[storeEditionIndex], {$merge: editionListOfPiece[i]}); @@ -88,10 +88,15 @@ class EditionListStore { this.editionList[pieceId].length = 0; // refetch editions with adjusted page size - EditionsListActions.fetchEditionList(pieceId, 1, prevEditionListLength, - this.editionList[pieceId].orderBy, - this.editionList[pieceId].orderAsc, - filterBy) + EditionsListActions + .fetchEditionList({ + pieceId, + page: 1, + pageSize: prevEditionListLength, + orderBy: this.editionList[pieceId].orderBy, + orderAsc: this.editionList[pieceId].orderAsc, + filterBy + }) .then(() => { // reset back to the normal pageSize and page this.editionList[pieceId].page = prevEditionListPage; @@ -144,7 +149,7 @@ class EditionListStore { if(!this.isEditionListOpenForPieceId[pieceId].show) { // to clear an array, david walsh recommends to just set it's length to zero // http://davidwalsh.name/empty-array - + this.editionList[pieceId].length = 0; } }