Skip to content

Commit

Permalink
progress update works
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurou committed May 2, 2021
1 parent 3a31a25 commit 9f86f33
Showing 1 changed file with 18 additions and 43 deletions.
61 changes: 18 additions & 43 deletions packages/moocfi-quizzes/src/CourseStatusProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from "react"
import { useEffect, useRef, useState } from "react"
import React, { useContext, useEffect, useRef, useState } from "react"
import {
CourseProgressProviderInterface,
ProgressData,
Expand All @@ -9,9 +8,8 @@ import {
ProgressResponse,
ExerciseCompletionsBySection,
CourseResponse,
CourseProgressProvider,
useCourseProgressState,
} from "../contexes/courseProgressProviderContext"
import { CourseProgressProviderContext } from "../contexes/courseProgressProviderContext"
import { PointsByGroup } from "../modelTypes"
import { languageOptions } from "../utils/languages"
import { ToastContainer, toast, TypeOptions } from "react-toastify"
Expand Down Expand Up @@ -69,7 +67,6 @@ export const CourseStatusProvider: React.FunctionComponent<CourseStatusProviderP
const fetchProgressData = async () => {
try {
const progressData = await getUserCourseData(courseId, accessToken)

const data = transformData(progressData.currentUser, progressData.course)
setData(data)
setLoading(false)
Expand All @@ -85,6 +82,12 @@ export const CourseStatusProvider: React.FunctionComponent<CourseStatusProviderP
}
}

const progress: CourseProgressProviderInterface = {
error,
loading,
courseProgressData: data,
}

const logout = () => {
setLoading(true)
setError(false)
Expand All @@ -107,21 +110,15 @@ export const CourseStatusProvider: React.FunctionComponent<CourseStatusProviderP
setUpdateQuiz({ ...updateQuiz, [id]: false })
}

const progress: CourseProgressProviderInterface = {
error,
loading,
courseProgressData: data,
courseId,
accessToken,
}

const status: CourseStatusProviderInterface = {
updateQuiz,
quizUpdated,
}

return (
<CourseProgressProvider courseProgress={progress}>
<CourseProgressProviderContext.Provider
value={{ progress, fetchProgressData }}
>
<CourseStatusProviderContext.Provider value={status}>
<ToastContainer
enableMultiContainer
Expand All @@ -140,7 +137,7 @@ export const CourseStatusProvider: React.FunctionComponent<CourseStatusProviderP
/>
{children}
</CourseStatusProviderContext.Provider>
</CourseProgressProvider>
</CourseProgressProviderContext.Provider>
)
}

Expand All @@ -150,34 +147,18 @@ export const injectCourseProgress = <P extends CourseProgressProviderInterface>(
props: P,
) => {
// initial course progress
const { state } = useCourseProgressState()
// course progress with updates
const { courseId, accessToken, ...rest } = state
const [injectProps, setInjectProps] = useState(rest)
const { progress: injectProps, fetchProgressData } = useContext(
CourseProgressProviderContext,
)

// Ref for the wrapped element
const ref: any = useRef<HTMLElement>()
const rootMargin = "0px"
const outOfViewThreshold = 10

const refetchData = async () => {
if (courseId && accessToken) {
// fetch data
setInjectProps({ ...injectProps, loading: true })
const progressData = await getUserCourseData(courseId, accessToken)
const data = transformData(progressData.currentUser, progressData.course)
setInjectProps({
...injectProps,
courseProgressData: data,
loading: false,
})
}
}
const outOfViewThreshold = 4

/**
* Triggers a refetch of progress after given time interval
* */

useEffect(() => {
const observer = new IntersectionObserver(
async ([entry]) => {
Expand All @@ -193,7 +174,7 @@ export const injectCourseProgress = <P extends CourseProgressProviderInterface>(
1000

if (secondsOutOfView >= outOfViewThreshold) {
await refetchData()
await fetchProgressData()
}

// reset off view counter
Expand All @@ -220,13 +201,7 @@ export const injectCourseProgress = <P extends CourseProgressProviderInterface>(
}
}, [])

const isLoggedInAndLoading = accessToken && state.loading

return (
<div ref={ref}>
{!isLoggedInAndLoading && <Component {...props} {...injectProps} />}
</div>
)
return <div ref={ref}>{<Component {...props} {...injectProps} />}</div>
}

const transformData = (
Expand Down

0 comments on commit 9f86f33

Please sign in to comment.