From 757ee40e4eb874a300f9e442cb50b47f1bf6b515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Mon, 19 Oct 2015 15:58:05 +0200 Subject: [PATCH] add 404 routine to PieceContainer --- js/actions/piece_actions.js | 4 ++- .../ascribe_detail/piece_container.js | 33 +++++++++++++------ js/stores/piece_store.js | 6 ++++ 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/js/actions/piece_actions.js b/js/actions/piece_actions.js index 7aed13fc..64dcb2e2 100644 --- a/js/actions/piece_actions.js +++ b/js/actions/piece_actions.js @@ -8,7 +8,8 @@ class PieceActions { constructor() { this.generateActions( 'updatePiece', - 'updateProperty' + 'updateProperty', + 'pieceFailed' ); } @@ -18,6 +19,7 @@ class PieceActions { this.actions.updatePiece(res.piece); }) .catch((err) => { + this.actions.pieceFailed(err.json); console.logGlobal(err); }); } diff --git a/js/components/ascribe_detail/piece_container.js b/js/components/ascribe_detail/piece_container.js index e2ebaa43..175008b4 100644 --- a/js/components/ascribe_detail/piece_container.js +++ b/js/components/ascribe_detail/piece_container.js @@ -44,7 +44,8 @@ import { getLangText } from '../../utils/lang_utils'; */ let PieceContainer = React.createClass({ propTypes: { - location: React.PropTypes.object + location: React.PropTypes.object, + params: React.PropTypes.object }, mixins: [History], @@ -61,19 +62,31 @@ let PieceContainer = React.createClass({ }, componentDidMount() { - UserStore.listen(this.onChange); - PieceListStore.listen(this.onChange); - UserActions.fetchCurrentUser(); - PieceStore.listen(this.onChange); - PieceActions.fetchOne(this.props.params.pieceId); - }, - - componentWillUnmount() { - // Every time we're leaving the piece detail page, + // Every time we're entering the piece detail page, // just reset the piece that is saved in the piece store // as it will otherwise display wrong/old data once the user loads // the piece detail a second time PieceActions.updatePiece({}); + + UserStore.listen(this.onChange); + PieceListStore.listen(this.onChange); + PieceStore.listen(this.onChange); + + UserActions.fetchCurrentUser(); + PieceActions.fetchOne(this.props.params.pieceId); + }, + + componentDidUpdate() { + const { pieceError } = this.state; + + if(pieceError && pieceError.status === 404) { + // Even though this path doesn't exist we can redirect + // to it as it catches all unknown paths + this.history.pushState(null, '/404'); + } + }, + + componentWillUnmount() { PieceStore.unlisten(this.onChange); UserStore.unlisten(this.onChange); PieceListStore.unlisten(this.onChange); diff --git a/js/stores/piece_store.js b/js/stores/piece_store.js index ccef50b1..3b04736b 100644 --- a/js/stores/piece_store.js +++ b/js/stores/piece_store.js @@ -7,11 +7,13 @@ import PieceActions from '../actions/piece_actions'; class PieceStore { constructor() { this.piece = {}; + this.pieceError = null; this.bindActions(PieceActions); } onUpdatePiece(piece) { this.piece = piece; + this.pieceError = null; } onUpdateProperty({key, value}) { @@ -21,6 +23,10 @@ class PieceStore { throw new Error('There is no piece defined in PieceStore or the piece object does not have the property you\'re looking for.'); } } + + onPieceFailed(err) { + this.pieceError = err; + } } export default alt.createStore(PieceStore, 'PieceStore');