Skip to content

Commit

Permalink
LearningPathTopic has ignoreCache variable. Other components can set …
Browse files Browse the repository at this point in the history
…ignoreCache to trigger a fetch
  • Loading branch information
DimitriB01 committed Aug 30, 2024
1 parent 0944374 commit 9e12e4b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import log from 'loglevel'
import { useCallback, useContext, useEffect, useMemo, useState } from 'react'
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useNavigate } from 'react-router-dom'
import { LearningPathElementStatus, LearningPathLearningElement, Topic, User } from '@core'
Expand Down Expand Up @@ -49,6 +49,9 @@ export const useLearningPathTopicProgress = (
const getLearningPathElementStatus = usePersistedStore((state) => state.getLearningPathElementStatus)
const getLearningPathTopic = useStore((state) => state.getLearningPathTopic)

// Trigger Reload of Fetches
const ignoreLearningPathTopicCache = useStore((state) => state.ignoreLearningPathTopicCache)

// Function
const getTopicProgress = useCallback(
(
Expand Down Expand Up @@ -138,7 +141,7 @@ export const useLearningPathTopicProgress = (
log.error(t('error.fetchUser') + ' ' + error)
})
}
}, [isAuth, navigate, getLearningPathTopic, getAllTopicProgress])
}, [isAuth, navigate, getLearningPathTopic, getAllTopicProgress, ignoreLearningPathTopicCache])

return useMemo(() => ({ topicProgress, isLoading, topics }), [topicProgress, isLoading, topics])
}
4 changes: 3 additions & 1 deletion src/pages/Course/Course.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import AddCircleIcon from '@mui/icons-material/AddCircle'
import { useContext, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useParams } from 'react-router-dom'
import { Box, Button, Card, CardContent, Grid, Stack } from '@common/components'
import { useLearningPathTopicProgress, useMediaQuery, useTheme } from '@common/hooks'
import { CreateTopicModal, SkeletonList, TopicCard } from '@components'
import { RoleContext } from '@services'
import { useStore } from '@store'

/**
* # Course Page
Expand All @@ -24,11 +24,13 @@ const Course = () => {
const { isCourseCreatorRole } = useContext(RoleContext)
const { courseId } = useParams<{ courseId: string }>()
const { topicProgress, isLoading, topics } = useLearningPathTopicProgress({ courseId })
const triggerLearningPathTopicReload = useStore((state) => state.triggerLearningPathTopicReload)

const [createTopicModalOpen, setCreateTopicModalOpen] = useState<boolean>(false)
const [successTopicCreated, setSuccessTopicCreated] = useState<boolean>(false)

const handleCloseTopicModal = () => {
triggerLearningPathTopicReload(true)
setCreateTopicModalOpen(false)
}

Expand Down
12 changes: 9 additions & 3 deletions src/store/Slices/LearningPathTopicSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,34 @@ import { resetters } from '../Zustand/Store'

export default interface LearningPathTopicSlice {
_cache_learningPathTopic_record: Record<string, LearningPathTopic | undefined>
ignoreLearningPathTopicCache: boolean
getLearningPathTopic: LearningPathTopicReturn
triggerLearningPathTopicReload: (reloadState: boolean) => void
}

export const createLearningPathTopicSlice: StateCreator<StoreState, [], [], LearningPathTopicSlice> = (set, get) => {
resetters.push(() => set({ _cache_learningPathTopic_record: {} }))
return {
_cache_learningPathTopic_record: {},
ignoreLearningPathTopicCache: false,
getLearningPathTopic: async (...arg) => {
const [userId, lmsUserId, studentId, courseId] = arg

const { ignoreLearningPathTopicCache } = get()
const cached = get()._cache_learningPathTopic_record[`${courseId}`]

if (!cached) {
if (!cached || ignoreLearningPathTopicCache) {
const learningPathTopic_response = await fetchLearningPathTopic(userId, lmsUserId, studentId, courseId)
set({
_cache_learningPathTopic_record: {
...get()._cache_learningPathTopic_record,
[`${courseId}`]: learningPathTopic_response
}
},
ignoreLearningPathTopicCache: false
})
return learningPathTopic_response
} else return cached
}
},
triggerLearningPathTopicReload: (reloadState: boolean) => set({ ignoreLearningPathTopicCache: reloadState })
}
}

0 comments on commit 9e12e4b

Please sign in to comment.