Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Promise#finally from course search #2643

Merged
merged 4 commits into from
Jun 10, 2018
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion source/lib/course-search/update-course-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function areAnyTermsCached(): Promise<boolean> {
export async function updateStoredCourses(): Promise<boolean> {
const outdatedTerms: Array<TermType> = await determineOutdatedTerms()
await Promise.all(outdatedTerms.map(term => storeTermCoursesFromServer(term)))
// returns ``true`` if any terms were updated
// returns `true` if any terms were updated
return outdatedTerms.length === 0 ? false : true
}

Expand Down
37 changes: 14 additions & 23 deletions source/views/sis/course-search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,7 @@ class CourseSearchView extends React.PureComponent<Props, State> {
}

componentDidMount() {
areAnyTermsCached().then(anyTermsCached => {
if (anyTermsCached) {
this.loadData()
} else {
this.setState(() => ({dataLoading: false}))
}
})
this.loadData()
this.updateFilters(this.props)
}

Expand All @@ -212,32 +206,29 @@ class CourseSearchView extends React.PureComponent<Props, State> {
searchBarTop = new Animated.Value(this.animations.searchBarTop.start)
containerHeight = new Animated.Value(this.animations.containerHeight.start)

loadData = () => {
loadData = async () => {
this.setState(() => ({dataLoading: true}))

// If the data has not been loaded into Redux State:
if (this.props.courseDataState !== 'ready') {
// If the data has not been loaded into Redux State:
// 1. load the cached courses
await this.props.loadCourseDataIntoMemory()

// 2. if any courses are cached, hide the spinner
if (await areAnyTermsCached()) {
this.setState(() => ({dataLoading: false}))
}

// 3. either way, start updating courses in the background
// 4. when everything is done, make sure the spinner is hidden
return this.props
.loadCourseDataIntoMemory()
.then(() => areAnyTermsCached())
.then(anyTermsCached => {
if (anyTermsCached) {
this.doneLoading()
}
return this.props.updateCourseData()
})
.finally(() => this.doneLoading())
await this.props.updateCourseData()
} else {
// If the course data is already in Redux State, check for update
return this.props.updateCourseData().then(() => this.doneLoading())
await this.props.updateCourseData()
}
}

doneLoading = () => this.setState(() => ({dataLoading: false}))
// 4. when everything is done, make sure the spinner is hidden
this.setState(() => ({dataLoading: false}))
}

onSearchButtonPress = text => {
if (Platform.OS === 'ios') {
Expand Down