-
Notifications
You must be signed in to change notification settings - Fork 60
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
Feat: Add fetch courses-user endpoints #1303
base: develop
Are you sure you want to change the base?
Changes from all commits
ea16a52
ba729d1
d835025
8eb4da6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import Column from '@/components/common/Column'; | |
import LoadingContainer from '@/components/common/LoadingContainer'; | ||
import Row from '@/components/common/Row'; | ||
import Header from '@/components/layout/Header'; | ||
import { useGetUserCoursesQuery } from '@/lib/api'; | ||
import { | ||
EMAIL_REMINDERS_FREQUENCY, | ||
PROGRESS_STATUS, | ||
|
@@ -56,7 +57,6 @@ export default function CoursesPage({ courseStories, conversations, shorts }: Pr | |
const entryPartnerReferral = useTypedSelector((state) => state.user.entryPartnerReferral); | ||
const partnerAccesses = useTypedSelector((state) => state.partnerAccesses); | ||
const partnerAdmin = useTypedSelector((state) => state.partnerAdmin); | ||
const courses = useTypedSelector((state) => state.courses); | ||
const searchParams = useSearchParams(); | ||
const sectionQueryParam = searchParams.get('section'); | ||
const isSmallScreen = useMediaQuery(theme.breakpoints.down('md')); | ||
|
@@ -71,6 +71,10 @@ export default function CoursesPage({ courseStories, conversations, shorts }: Pr | |
imageAlt: 'alt.personSitting', | ||
}; | ||
|
||
const { data: userCourses } = useGetUserCoursesQuery(undefined, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would put this query inside a useEffect otherwise it will run on component refresh. https://react.dev/reference/react/useEffect. |
||
skip: !userId, | ||
}); | ||
|
||
useEffect(() => { | ||
logEvent(COURSE_LIST_VIEWED); | ||
}, []); | ||
|
@@ -132,10 +136,10 @@ export default function CoursesPage({ courseStories, conversations, shorts }: Pr | |
setLoadedCourses(coursesWithAccess); | ||
setLoadedShorts(shortsWithAccess); | ||
|
||
if (courses) { | ||
if (userCourses) { | ||
let courseCoursesStarted: Array<number> = []; | ||
let courseCoursesCompleted: Array<number> = []; | ||
courses.map((course) => { | ||
userCourses.map((course) => { | ||
if (course.completed) { | ||
courseCoursesCompleted.push(course.storyblokId); | ||
} else { | ||
|
@@ -145,7 +149,15 @@ export default function CoursesPage({ courseStories, conversations, shorts }: Pr | |
setCoursesStarted(courseCoursesStarted); | ||
setCoursesCompleted(courseCoursesCompleted); | ||
} | ||
}, [partnerAccesses, partnerAdmin, courseStories, courses, shorts, entryPartnerReferral, userId]); | ||
}, [ | ||
partnerAccesses, | ||
partnerAdmin, | ||
courseStories, | ||
userCourses, | ||
shorts, | ||
entryPartnerReferral, | ||
userId, | ||
]); | ||
|
||
const getCourseProgress = (courseId: number) => { | ||
return coursesStarted.includes(courseId) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
'use client'; | ||
|
||
import { useGetUserCoursesQuery } from '@/lib/api'; | ||
import { PROGRESS_STATUS } from '@/lib/constants/enums'; | ||
import { useTypedSelector } from '@/lib/hooks/store'; | ||
import { Course } from '@/lib/store/coursesSlice'; | ||
import CheckCircleIcon from '@mui/icons-material/CheckCircle'; | ||
import DonutLargeIcon from '@mui/icons-material/DonutLarge'; | ||
|
@@ -20,10 +20,10 @@ export const SessionProgressDisplay = (props: SessionProgressDisplayProps) => { | |
PROGRESS_STATUS.NOT_STARTED, | ||
); | ||
|
||
const courses = useTypedSelector((state) => state.courses); | ||
const { data: courses } = useGetUserCoursesQuery(undefined); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above! |
||
|
||
useEffect(() => { | ||
const userCourse = courses.find((course: Course) => course.storyblokId === storyblokCourseId); | ||
const userCourse = courses?.find((course: Course) => course.storyblokId === storyblokCourseId); | ||
|
||
if (userCourse) { | ||
const matchingSession = userCourse.sessions?.find( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import { ContentUnavailable } from '@/components/common/ContentUnavailable'; | |
import CourseHeader from '@/components/course/CourseHeader'; | ||
import CourseIntroduction from '@/components/course/CourseIntroduction'; | ||
import { Link as i18nLink } from '@/i18n/routing'; | ||
import { useGetUserCoursesQuery } from '@/lib/api'; | ||
import { PROGRESS_STATUS } from '@/lib/constants/enums'; | ||
import { COURSE_OVERVIEW_VIEWED } from '@/lib/constants/events'; | ||
import { useTypedSelector } from '@/lib/hooks/store'; | ||
|
@@ -69,12 +70,14 @@ const StoryblokCoursePage = (props: StoryblokCoursePageProps) => { | |
const entryPartnerReferral = useTypedSelector((state) => state.user.entryPartnerReferral); | ||
const partnerAccesses = useTypedSelector((state) => state.partnerAccesses); | ||
const partnerAdmin = useTypedSelector((state) => state.partnerAdmin); | ||
const courses = useTypedSelector((state) => state.courses); | ||
const isLoggedIn = useTypedSelector((state) => Boolean(state.user.id)); | ||
const [userAccess, setUserAccess] = useState<boolean>(); | ||
const [courseProgress, setCourseProgress] = useState<PROGRESS_STATUS>( | ||
PROGRESS_STATUS.NOT_STARTED, | ||
); | ||
const { data: courses } = useGetUserCoursesQuery(undefined, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above! |
||
skip: !isLoggedIn, | ||
}); | ||
|
||
useEffect(() => { | ||
const storyPartners = included_for_partners; | ||
|
@@ -91,7 +94,7 @@ const StoryblokCoursePage = (props: StoryblokCoursePageProps) => { | |
}, [partnerAccesses, partnerAdmin, included_for_partners, entryPartnerReferral, isLoggedIn]); | ||
|
||
useEffect(() => { | ||
setCourseProgress(determineCourseProgress(courses, storyId)); | ||
setCourseProgress(determineCourseProgress(courses || [], storyId)); | ||
}, [courses, storyId]); | ||
|
||
useEffect(() => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,9 @@ const slice = createSlice({ | |
builder.addMatcher(api.endpoints.startSession.matchFulfilled, (state, { payload }) => { | ||
return mergeUpdatedCourse(state, payload); | ||
}); | ||
builder.addMatcher(api.endpoints.getUserCourses.matchFulfilled, (state, { payload }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This matcher populates state when the api endpoint is called. This means you don't need ot use the return value of the api call above. Hope that makes sense! |
||
return payload; | ||
}); | ||
}, | ||
}); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would continue using the state rather than using the result of the query useGetUserQuery below.