@@ -14,7 +14,7 @@ import { initChallengeNavigationMenu } from "@/store/feature/communities/navigat
14
14
import { setColors } from "@/store/feature/ui.slice" ;
15
15
import useNavigation from "@/hooks/useNavigation" ;
16
16
import { GetServerSideProps } from "next" ;
17
- import { wrapper } from "@/store" ;
17
+ import { IRootState , wrapper } from "@/store" ;
18
18
import { fetchCurrentCommunity } from "@/store/services/community.service" ;
19
19
import { serverSideTranslations } from "next-i18next/serverSideTranslations" ;
20
20
import ChallengeOverviewCard from "@/components/cards/challenge/Overview" ;
@@ -23,6 +23,10 @@ import { fetchChallenge } from "@/store/services/communities/challenges";
23
23
import PageNavigation from "@/components/sections/courses/PageNavigation" ;
24
24
import ChallengeCard from "@/components/cards/challenge/Challenge" ;
25
25
import { useTranslation } from "next-i18next" ;
26
+ import { useMultiSelector } from "@/hooks/useTypedSelector" ;
27
+ import Section from "@/components/ui/Section" ;
28
+ import Loader from "@/components/ui/Loader" ;
29
+ import { useRouter } from "next/router" ;
26
30
27
31
/**
28
32
* Learning module page props interfae
@@ -39,6 +43,11 @@ interface LearningModulePageProps {
39
43
} ;
40
44
}
41
45
46
+ interface LearningModuleMultiselector {
47
+ learningModule : LearningModule ,
48
+ loading : boolean
49
+ }
50
+
42
51
/**
43
52
* Learning module module view page
44
53
* @date 4/24/2023 - 8:35:52 PM
@@ -48,27 +57,45 @@ interface LearningModulePageProps {
48
57
* @returns
49
58
*/
50
59
export default function LearningModulePage ( props : LearningModulePageProps ) {
51
- const { community, learningModule, challenge } = props . pageProps ;
60
+ const { community, challenge } = props . pageProps ;
61
+ const { learningModule, loading } = useMultiSelector < unknown , LearningModuleMultiselector > ( {
62
+ learningModule : ( state : IRootState ) => state . learningModules . current ,
63
+ loading : ( state : IRootState ) => state . learningModules . loading
64
+ } )
52
65
const dispatch = useDispatch ( ) ;
53
66
const { t } = useTranslation ( ) ;
54
67
const navigation = useNavigation ( ) ;
68
+ const router = useRouter ( )
69
+ const { query, locale } = router
55
70
56
71
useEffect ( ( ) => {
57
72
dispatch ( setCurrentCommunity ( community ) ) ;
58
73
dispatch ( setCurrentLearningModule ( learningModule ) ) ;
59
74
dispatch ( setColors ( community . colors ) ) ;
60
75
dispatch ( initChallengeNavigationMenu ( navigation . community ) ) ;
61
- } , [ community , learningModule , navigation . community ] ) ;
76
+ dispatch ( findLearningModule ( { id : query ?. id as string , locale } ) )
77
+ } , [ community ?. colors , locale ] ) ;
78
+
62
79
63
80
const title = getMetadataTitle ( learningModule ?. title ) ;
64
81
const descriptions = getMetadataDescription ( learningModule ?. description ) ;
65
82
66
- const paths = useMemo ( ( ) => [ challenge . name , learningModule ?. title ] , [ challenge . name , learningModule ?. title ] ) ;
83
+ const paths = useMemo ( ( ) => [ challenge . name , learningModule ?. title ] , [ challenge . name , learningModule ] ) ;
67
84
68
85
const isLastLearningModule = useMemo ( ( ) => {
86
+ if ( ! learningModule ) return false
69
87
if ( ! challenge . learningModules || ! challenge . learningModules . length ) return false ;
70
88
return learningModule . id === challenge . learningModules [ challenge . learningModules . length - 1 ] . id ;
71
- } , [ learningModule . id , challenge . learningModules ] ) ;
89
+ } , [ learningModule , challenge . learningModules ] ) ;
90
+
91
+ if ( loading )
92
+
93
+ return (
94
+ < Section className = "h-[50vh] flex items-center justify-center" >
95
+ < Loader />
96
+ </ Section >
97
+ ) ;
98
+
72
99
return (
73
100
< >
74
101
< Head >
@@ -109,22 +136,19 @@ export const getServerSideProps: GetServerSideProps = wrapper.getServerSideProps
109
136
try {
110
137
const communitySlug = params ?. slug as string ;
111
138
const challenge_id = params ?. challenge_id as string ;
112
- const learningModule_id = params ?. id as string ;
113
139
114
- const [ { data : community } , { data : challenge } , { data : learningModule } , translations ] = await Promise . all ( [
140
+ const [ { data : community } , { data : challenge } , translations ] = await Promise . all ( [
115
141
store . dispatch ( fetchCurrentCommunity ( { slug : communitySlug , locale } ) ) ,
116
142
store . dispatch ( fetchChallenge ( { id : challenge_id , relations : [ "learning-modules" , "rubric" ] , locale } ) ) ,
117
- store . dispatch ( findLearningModule ( { id : learningModule_id , locale } ) ) ,
118
143
serverSideTranslations ( locale as string ) ,
119
144
] ) ;
120
145
121
- if ( Object . entries ( community ) . length === 0 || Object . entries ( challenge ) . length === 0 || Object . entries ( learningModule ) . length === 0 )
146
+ if ( Object . entries ( community ) . length === 0 || Object . entries ( challenge ) . length === 0 )
122
147
throw new Error ( "Failed to fetch learning module" ) ;
123
148
124
149
return {
125
150
props : {
126
151
community,
127
- learningModule,
128
152
challenge,
129
153
...translations ,
130
154
} ,
0 commit comments