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