From 4c4e5e0004d1273121c84ad1e40633031095f26b Mon Sep 17 00:00:00 2001 From: Brett Sun Date: Fri, 15 Jan 2016 17:18:05 +0100 Subject: [PATCH] Load piece when necessary in MarketRegisterPiece --- .../market/market_register_piece.js | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/js/components/whitelabel/wallet/components/market/market_register_piece.js b/js/components/whitelabel/wallet/components/market/market_register_piece.js index 858fb469..bd4a8824 100644 --- a/js/components/whitelabel/wallet/components/market/market_register_piece.js +++ b/js/components/whitelabel/wallet/components/market/market_register_piece.js @@ -6,11 +6,15 @@ import { History } from 'react-router'; import Col from 'react-bootstrap/lib/Col'; import Row from 'react-bootstrap/lib/Row'; +import PieceStore from '../../../../../stores/piece_store'; import PieceActions from '../../../../../actions/piece_actions'; + import PieceListStore from '../../../../../stores/piece_list_store'; import PieceListActions from '../../../../../actions/piece_list_actions'; + import UserStore from '../../../../../stores/user_store'; import UserActions from '../../../../../actions/user_actions'; + import WhitelabelActions from '../../../../../actions/whitelabel_actions'; import WhitelabelStore from '../../../../../stores/whitelabel_store'; @@ -35,6 +39,7 @@ let MarketRegisterPiece = React.createClass({ getInitialState(){ return mergeOptions( PieceListStore.getState(), + PieceStore.getInitialState(), UserStore.getState(), WhitelabelStore.getState(), { @@ -44,19 +49,27 @@ let MarketRegisterPiece = React.createClass({ componentDidMount() { PieceListStore.listen(this.onChange); + PieceStore.listen(this.onChange); UserStore.listen(this.onChange); WhitelabelStore.listen(this.onChange); UserActions.fetchCurrentUser(); WhitelabelActions.fetchWhitelabel(); - // Reset the piece store to make sure that we don't display old data - // if the user repeatedly registers works - PieceActions.updatePiece({}); + const queryParams = this.props.location.query; + + // Load the correct piece if the user loads the second step directly + // by pressing on the back button or using the url + // We're using 'in' here as we want to know if 'piece_id' is present in the url, + // we don't care about the value. + if ('piece_id' in queryParams) { + PieceActions.fetchPiece(queryParams.piece_id); + } }, componentWillUnmount() { PieceListStore.unlisten(this.onChange); + PieceStore.unlisten(this.onChange); UserStore.unlisten(this.onChange); WhitelabelStore.unlisten(this.onChange); }, @@ -68,15 +81,12 @@ let MarketRegisterPiece = React.createClass({ handleRegisterSuccess(response) { this.refreshPieceList(); - // Use the response's piece for the next step if available - let pieceId = null; + // Also load the newly registered piece for the next step if (response && response.piece) { - pieceId = response.piece.id; PieceActions.updatePiece(response.piece); } - this.incrementStep(); - this.refs.slidesContainer.nextSlide({ piece_id: pieceId }); + this.nextSlide({ piece_id: response.piece.id }); }, handleAdditionalDataSuccess() { @@ -85,21 +95,13 @@ let MarketRegisterPiece = React.createClass({ this.history.push('/collection'); }, - // We need to increase the step to lock the forms that are already filled out - incrementStep() { + nextSlide(queryParams) { + // We need to increase the step to lock the forms that are already filled out this.setState({ step: this.state.step + 1 }); - }, - getPieceFromQueryParam() { - const queryParams = this.props.location.query; - - // Since every step of this register process is atomic, - // we may need to enter the process at step 1 or 2. - // If this is the case, we'll need the piece number to complete submission. - // It is encoded in the URL as a queryParam and we're checking for it here. - return queryParams && queryParams.piece_id; + this.refs.slidesContainer.nextSlide(queryParams); }, refreshPieceList() { @@ -114,7 +116,9 @@ let MarketRegisterPiece = React.createClass({ }, render() { + const { location } = this.props; const { + piece, step, whitelabel: { name: whitelabelName = 'Market' @@ -130,7 +134,7 @@ let MarketRegisterPiece = React.createClass({ pending: 'glyphicon glyphicon-chevron-right', completed: 'glyphicon glyphicon-lock' }} - location={this.props.location}> + location={location}>
@@ -142,7 +146,7 @@ let MarketRegisterPiece = React.createClass({ isFineUploaderActive={true} enableSeparateThumbnail={false} handleSuccess={this.handleRegisterSuccess} - location={this.props.location}> + location={location}> @@ -160,8 +164,10 @@ let MarketRegisterPiece = React.createClass({