From 5a75a326aa4e0c0dbe476af5e1dad425a531ab5e Mon Sep 17 00:00:00 2001 From: Tony Chen Date: Wed, 5 Jun 2019 18:12:01 -0700 Subject: [PATCH] Prevent crashes on directly navigating to classifcations and results pages. --- src/SetList/SetList.js | 20 ++++++++++++-------- src/SetList/SetResults.js | 26 +++++++++++++++----------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/SetList/SetList.js b/src/SetList/SetList.js index 2027bab..32956b3 100644 --- a/src/SetList/SetList.js +++ b/src/SetList/SetList.js @@ -22,14 +22,18 @@ export default class SetList extends Component { } getClassificationResults = () => { - const data = this.props.location.state.results; // classification results passed in from Classify view - let categories = Object.keys(data); - const results = categories.map(category => { - return {category: category, data: data[category]}; - }); - this.setState({ - results: results - }); + if (this.props.location.state) { + const data = this.props.location.state.results; // classification results passed in from Classify view + let categories = Object.keys(data); + const results = categories.map(category => { + return {category: category, data: data[category]}; + }); + this.setState({ + results: results + }); + } else { + this.props.history.push('/'); + } }; renderResultsView = (index) => { diff --git a/src/SetList/SetResults.js b/src/SetList/SetResults.js index 7dc0085..deabc58 100644 --- a/src/SetList/SetResults.js +++ b/src/SetList/SetResults.js @@ -19,16 +19,20 @@ export default class SetResult extends Component { } componentWillMount() { - const jsonData = this.props.location.state.data; - const download = { - filename: this.generateDownloadFilename(jsonData), - rhef: this.generateDownloadHref(jsonData) - }; - this.setState({ - title: this.props.location.state.data.category, - results: this.props.location.state.data.data, - download: download - }); + if (this.props.location.state) { + const jsonData = this.props.location.state.data; + const download = { + filename: this.generateDownloadFilename(jsonData), + rhef: this.generateDownloadHref(jsonData) + }; + this.setState({ + title: this.props.location.state.data.category, + results: this.props.location.state.data.data, + download: download + }); + } else { + this.props.history.push('/'); + } } generateDownloadFilename = (jsonData) => { @@ -104,7 +108,7 @@ export default class SetResult extends Component { - {this.renderClassifications(this.state.results)} + {this.state.results ? this.renderClassifications(this.state.results) : null}