Skip to content

Commit

Permalink
Merge pull request #252 from thinc-org/fix/sync-regressions
Browse files Browse the repository at this point in the history
Fix sync regressions from migrating to backend sync
  • Loading branch information
suphon-t authored Nov 5, 2021
2 parents 318a376 + f8663d5 commit d3061d9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/services/apollo/query/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const GET_COURSE_CART = gql`
courseNo
semester
selectedSectionNo
isHidden
}
}
`
26 changes: 9 additions & 17 deletions src/store/courseCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { userStore } from '@/store/userStore'

export interface CourseCartItem extends Course {
selectedSectionNo: string
isSelected: boolean
isHidden: boolean
}

Expand All @@ -39,6 +38,7 @@ interface CourseCartStoreItem {
courseNo: string
semester: string
selectedSectionNo: string
isHidden: boolean
}

export interface CourseCartStore {
Expand Down Expand Up @@ -145,9 +145,9 @@ export class CourseCart implements CourseCartProps {
})
try {
const courses = await this.source.syncFromStore()
const fullCourses: (Course & { selectedSectionNo: string })[] = []
const fullCourses: CourseCartItem[] = []
for (const course of courses) {
let detail
let detail: CourseCartItem
try {
const { data } = await client.query<GetCourseResponse, GetCourseVars>({
query: GET_COURSE,
Expand All @@ -160,11 +160,12 @@ export class CourseCart implements CourseCartProps {
},
},
})
detail = { ...data.course, selectedSectionNo: course.selectedSectionNo }
detail = { ...data.course, selectedSectionNo: course.selectedSectionNo, isHidden: course.isHidden }
} catch (e) {
detail = {
...unknownCourse,
selectedSectionNo: course.selectedSectionNo,
isHidden: course.isHidden,
studyProgram: course.studyProgram as StudyProgram,
semester: course.semester as Semester,
academicYear: course.academicYear,
Expand All @@ -174,7 +175,7 @@ export class CourseCart implements CourseCartProps {
fullCourses.push(detail)
}
runInAction(() => {
this.shopItems = fullCourses.map((course) => ({ ...course, isSelected: false, isHidden: false }))
this.shopItems = fullCourses.map((course) => ({ ...course }))
})
setTimeout(
action('Delayed sync icon', () => {
Expand Down Expand Up @@ -203,6 +204,7 @@ export class CourseCart implements CourseCartProps {
semester: item.semester,
courseNo: item.courseNo,
selectedSectionNo: item.selectedSectionNo,
isHidden: item.isHidden,
}))
)
setTimeout(
Expand Down Expand Up @@ -261,7 +263,7 @@ export class CourseCart implements CourseCartProps {
})

if (!selectedSectionNo) selectedSectionNo = this.findFirstSectionNo(course)
const newItem: CourseCartItem = { ...course, selectedSectionNo, isSelected: false, isHidden: false }
const newItem: CourseCartItem = { ...course, selectedSectionNo, isHidden: false }
const foundIndex = this.shopItems.findIndex((item) => isSameKey(item, newItem))
if (foundIndex != -1) this.shopItems[foundIndex] = newItem
else this.shopItems.push(newItem)
Expand All @@ -286,17 +288,6 @@ export class CourseCart implements CourseCartProps {
const foundIndex = this.shopItems.findIndex((item) => isSameKey(item, course))
if (foundIndex == -1) return
this.shopItems[foundIndex].isHidden = !this.shopItems[foundIndex].isHidden
}

/**
* Use to remove all selected items
*/
@action
removeItems(): void {
if (this.state === 'default') return
this.shopItems = this.shopItems.filter((item) => item.isSelected === false)
this.state = 'default'

this.onChange()
}

Expand All @@ -307,6 +298,7 @@ export class CourseCart implements CourseCartProps {
const [removed] = result.splice(from, 1)
result.splice(to, 0, removed)
this.shopItems = result
this.onChange()
}

/**
Expand Down

0 comments on commit d3061d9

Please sign in to comment.