1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00

add 404 routine to PieceContainer

This commit is contained in:
Tim Daubenschütz 2015-10-19 15:58:05 +02:00
parent 23b7ebd776
commit 757ee40e4e
3 changed files with 32 additions and 11 deletions

View File

@ -8,7 +8,8 @@ class PieceActions {
constructor() { constructor() {
this.generateActions( this.generateActions(
'updatePiece', 'updatePiece',
'updateProperty' 'updateProperty',
'pieceFailed'
); );
} }
@ -18,6 +19,7 @@ class PieceActions {
this.actions.updatePiece(res.piece); this.actions.updatePiece(res.piece);
}) })
.catch((err) => { .catch((err) => {
this.actions.pieceFailed(err.json);
console.logGlobal(err); console.logGlobal(err);
}); });
} }

View File

@ -44,7 +44,8 @@ import { getLangText } from '../../utils/lang_utils';
*/ */
let PieceContainer = React.createClass({ let PieceContainer = React.createClass({
propTypes: { propTypes: {
location: React.PropTypes.object location: React.PropTypes.object,
params: React.PropTypes.object
}, },
mixins: [History], mixins: [History],
@ -61,19 +62,31 @@ let PieceContainer = React.createClass({
}, },
componentDidMount() { componentDidMount() {
UserStore.listen(this.onChange); // Every time we're entering the piece detail page,
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,
// just reset the piece that is saved in the piece store // just reset the piece that is saved in the piece store
// as it will otherwise display wrong/old data once the user loads // as it will otherwise display wrong/old data once the user loads
// the piece detail a second time // the piece detail a second time
PieceActions.updatePiece({}); 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); PieceStore.unlisten(this.onChange);
UserStore.unlisten(this.onChange); UserStore.unlisten(this.onChange);
PieceListStore.unlisten(this.onChange); PieceListStore.unlisten(this.onChange);

View File

@ -7,11 +7,13 @@ import PieceActions from '../actions/piece_actions';
class PieceStore { class PieceStore {
constructor() { constructor() {
this.piece = {}; this.piece = {};
this.pieceError = null;
this.bindActions(PieceActions); this.bindActions(PieceActions);
} }
onUpdatePiece(piece) { onUpdatePiece(piece) {
this.piece = piece; this.piece = piece;
this.pieceError = null;
} }
onUpdateProperty({key, value}) { 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.'); 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'); export default alt.createStore(PieceStore, 'PieceStore');