From 2b5a7e2ba7207d8c6ec9fdce500041dc1c431b62 Mon Sep 17 00:00:00 2001 From: kbberker Date: Tue, 26 Mar 2019 12:41:14 +0000 Subject: [PATCH 1/2] Prettier --- src/App.js | 155 +++++++++++++++++++++++++++-------------------------- 1 file changed, 78 insertions(+), 77 deletions(-) diff --git a/src/App.js b/src/App.js index 40233a9..001863a 100644 --- a/src/App.js +++ b/src/App.js @@ -38,7 +38,9 @@ class App extends Component { .then(this.handleFetchErrors) .then(res => res.json()) .then(feed_json => resolve(feed_json)) - .catch(err => { reject(err); }); + .catch(err => { + reject(err); + }); }); } @@ -65,10 +67,12 @@ class App extends Component { loadSermons() { const church = JSON.parse(localStorage.getItem("church")); - if (!church) { return } + if (!church) { + return; + } - this.requestJSON(`${one21Api}church/${church.slug}/guides/sermons`) - .then(churchFeed => { + this.requestJSON(`${one21Api}church/${church.slug}/guides/sermons`).then( + churchFeed => { const sermons = churchFeed.studies; this.setState( @@ -80,55 +84,54 @@ class App extends Component { this.hasCompletedLoad(); } ); - }); + } + ); } loadContent() { const church = JSON.parse(localStorage.getItem("church")); - if (this.state.loading === true || !church) { return; } - - this.setState( - { loading: true }, - () => { + if (this.state.loading === true || !church) { + return; + } - this.setState({ - church: church, - sermons: null, - latest_sermon: null, - guides: null, - promoted_guide: null - }); + this.setState({ loading: true }, () => { + this.setState({ + church: church, + sermons: null, + latest_sermon: null, + guides: null, + promoted_guide: null + }); - const guidelist = `${one21Api}church/${church.slug}/guides`; - this.requestJSON(guidelist) - .then( - guides => { - this.setState( - { - guides: guides, - promoted_guide: guides.filter( - guide => guide.highlight_first === true - )[0] - }, - () => { - if (guides.filter(guide => guide.slug === 'sermons').length !== 0) { - this.loadSermons(); - } else { - this.hasCompletedLoad(); - } - } - ); - }) + const guidelist = `${one21Api}church/${church.slug}/guides`; + this.requestJSON(guidelist) + .then(guides => { + this.setState( + { + guides: guides, + promoted_guide: guides.filter( + guide => guide.highlight_first === true + )[0] + }, + () => { + if ( + guides.filter(guide => guide.slug === "sermons").length !== 0 + ) { + this.loadSermons(); + } else { + this.hasCompletedLoad(); + } + } + ); + }) .catch(() => { this.setState({ loading: false, emptyState: true }); }); - - } - ); + }); } hasCompletedLoad() { @@ -139,7 +142,7 @@ class App extends Component { this.setState({ loading: false, promoted_guide: guides[0] - }) + }); } else { this.setState({ loading: false }); } @@ -217,42 +220,40 @@ class App extends Component { )} /> - {guides && - church && ( - ( - - {endpoint => ( - - )} - - )} - /> - )} - - {guides && - church && ( - ( - - )} - /> - )} + {guides && church && ( + ( + + {endpoint => ( + + )} + + )} + /> + )} + + {guides && church && ( + ( + + )} + /> + )} Date: Tue, 26 Mar 2019 20:17:04 +0000 Subject: [PATCH 2/2] Load sermons from localstorage or from server --- src/App.js | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/src/App.js b/src/App.js index 001863a..14b53dc 100644 --- a/src/App.js +++ b/src/App.js @@ -71,21 +71,37 @@ class App extends Component { return; } - this.requestJSON(`${one21Api}church/${church.slug}/guides/sermons`).then( - churchFeed => { - const sermons = churchFeed.studies; - - this.setState( - { - sermons: sermons, - latest_sermon: sermons[0] - }, - () => { - this.hasCompletedLoad(); - } - ); - } - ); + const localSermons = JSON.parse(localStorage.getItem("sermons")); + + if (localSermons) { + this.setState( + { + sermons: localSermons, + latest_sermon: localSermons[0] + }, + () => { + this.hasCompletedLoad(); + } + ); + } else { + this.requestJSON(`${one21Api}church/${church.slug}/guides/sermons`).then( + churchFeed => { + const sermons = churchFeed.studies; + + localStorage.setItem("sermons", JSON.stringify(sermons)); + + this.setState( + { + sermons: sermons, + latest_sermon: sermons[0] + }, + () => { + this.hasCompletedLoad(); + } + ); + } + ); + } } loadContent() {