Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 106 additions & 89 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}

Expand All @@ -65,70 +67,87 @@ class App extends Component {

loadSermons() {
const church = JSON.parse(localStorage.getItem("church"));
if (!church) { 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();
}
);
});
if (!church) {
return;
}

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() {
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() {
Expand All @@ -139,7 +158,7 @@ class App extends Component {
this.setState({
loading: false,
promoted_guide: guides[0]
})
});
} else {
this.setState({ loading: false });
}
Expand Down Expand Up @@ -217,42 +236,40 @@ class App extends Component {
)}
/>

{guides &&
church && (
<Route
path="/guides/:guideSlug"
render={({ match }) => (
<ApiEndpoint.Consumer>
{endpoint => (
<Guide
church={this.state.church}
title={this.state.title}
slug={match.params.guideSlug}
studySlug={match.params.studySlug}
apiEndpoint={endpoint}
setTitle={this.setTitle}
setView={this.setView}
/>
)}
</ApiEndpoint.Consumer>
)}
/>
)}

{guides &&
church && (
<Route
exact
path="/guides"
render={({ match }) => (
<GuideList
{...this.state}
setTitle={this.setTitle}
setView={this.setView}
/>
)}
/>
)}
{guides && church && (
<Route
path="/guides/:guideSlug"
render={({ match }) => (
<ApiEndpoint.Consumer>
{endpoint => (
<Guide
church={this.state.church}
title={this.state.title}
slug={match.params.guideSlug}
studySlug={match.params.studySlug}
apiEndpoint={endpoint}
setTitle={this.setTitle}
setView={this.setView}
/>
)}
</ApiEndpoint.Consumer>
)}
/>
)}

{guides && church && (
<Route
exact
path="/guides"
render={({ match }) => (
<GuideList
{...this.state}
setTitle={this.setTitle}
setView={this.setView}
/>
)}
/>
)}

<Route
exact
Expand Down