Skip to content

Commit

Permalink
fix(web): recommendation crashing website (#648)
Browse files Browse the repository at this point in the history
  • Loading branch information
leomotors authored Oct 4, 2023
1 parent ea961bb commit ee1ff5f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-phones-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'web': patch
---

fix: recommendation crashing website (unexpected behavior of useEffect)
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { useEffect, useMemo, useState } from 'react'
import { useEffect, useMemo } from 'react'

import { Link, Typography, styled, useTheme } from '@mui/material'
import { observer } from 'mobx-react'

import { Analytics } from '@web/common/context/Analytics/components/Analytics'
import { useCourseGroup } from '@web/common/hooks/useCourseGroup'
import { useCourseSearchProvider } from '@web/modules/CourseSearch/context/CourseSearch/hooks/useCourseSearchProvider'
import { useSearchCourseQueryParams } from '@web/modules/CourseSearch/hooks/useSearchCourseQueryParams'
import { collectLogEvent } from '@web/services/logging'
import { courseCartStore } from '@web/store'

import { SearchCourseQueryVariables, useRecommendCourseTextLazyQuery } from '@cgr/codegen'
import { useRecommendCourseTextQuery } from '@cgr/codegen'

import { SelectedCourse } from './types'

Expand All @@ -30,13 +29,17 @@ const RecommendationText: React.FC<RecommendationTextProps> = (props: Recommenda
const selectedCourses = props.selectedCourses
const courseGroup = useCourseGroup()

const { courseSearchQuery } = useCourseSearchProvider()
const { setFilter } = useSearchCourseQueryParams()
const [lastSearchQuery, setLastSearchQuery] = useState<SearchCourseQueryVariables | undefined>(
undefined
)

const [fetchRecommendation, { data }] = useRecommendCourseTextLazyQuery()
const { data } = useRecommendCourseTextQuery({
variables: {
req: {
variant,
semesterKey: courseGroup,
selectedCourses: selectedCourses,
},
},
})

const visibleRecommendation = useMemo(() => data?.recommend?.courses?.slice(0, 6) ?? [], [data])
const theme = useTheme()
Expand All @@ -54,27 +57,6 @@ const RecommendationText: React.FC<RecommendationTextProps> = (props: Recommenda
})
}, [data, variant])

useEffect(() => {
if (JSON.stringify(courseSearchQuery.variables) === JSON.stringify(lastSearchQuery)) return
fetchRecommendation({
variables: {
req: {
variant,
semesterKey: courseGroup,
selectedCourses: selectedCourses,
},
},
})
setLastSearchQuery(courseSearchQuery.variables)
}, [
courseSearchQuery.variables,
selectedCourses,
courseGroup,
fetchRecommendation,
lastSearchQuery,
variant,
])

if (!visibleRecommendation || visibleRecommendation.length === 0) return null

return (
Expand Down

0 comments on commit ee1ff5f

Please sign in to comment.