@@ -10,9 +10,9 @@ import { BookProps, getPublicCollection } from "@/api/book";
1010import { isAdminFor } from "@/api/user" ;
1111
1212import { logger } from "@/utils/logger" ;
13- import { getT , IntlContextProvider } from "@/i18n" ;
13+ import { getT , IntlContextProvider , useIntl } from "@/i18n" ;
1414import { UserContext } from "@/context/UserContextProvider" ;
15- import { AnswerState , QuizContextProvider } from "@/context/QuizContextProvider" ;
15+ import { AnswerState , QuizContext , QuizContextProvider } from "@/context/QuizContextProvider" ;
1616import Layout from "../Layout/Layout" ;
1717
1818import Login from "../Login" ;
@@ -22,9 +22,51 @@ import { Chapter } from "./Chapter";
2222import { SidenoteContext } from "@/components/Book/Sidenote" ;
2323import { useHasMounted } from "@/hooks/useHasMounted" ;
2424import { usePublicProvider } from "@/hooks/usePublicProvider" ;
25- import { LinkDesc } from "@/types" ;
25+ import { ChapterDef , LinkDesc , UnlockChaptersOnAnswersType } from "@/types" ;
2626
2727
28+ const Chapters = ( { chapters : allChapters , bookId, chapterNumbers, unlockChaptersOnAnswers, setIsChapterIndexVisible, allAnswers} :
29+ { chapters : ChapterDef [ ] ,
30+ bookId : number ,
31+ chapterNumbers : Record < number , number > ,
32+ unlockChaptersOnAnswers : UnlockChaptersOnAnswersType ,
33+ setIsChapterIndexVisible : React . Dispatch < React . SetStateAction < Record < number , boolean > > > ,
34+ allAnswers ?: AnswersInBook }
35+ ) => {
36+ const { chapterStats} = useContext ( QuizContext ) ;
37+ const { t} = useIntl ( ) ;
38+ const chapters = [ ] ;
39+ for ( let index = 0 ; index < allChapters . length ; index ++ ) {
40+ chapters . push ( allChapters [ index ] ) ;
41+ const stats = chapterStats ( index ) ;
42+ if ( unlockChaptersOnAnswers !== "none"
43+ && stats
44+ && stats . nQuestions != ( unlockChaptersOnAnswers === "attempt" ? stats . answered : stats . correct ) ) {
45+ break ;
46+ }
47+ }
48+ return < >
49+ { chapters . map ( ( chapterDef , index ) => (
50+ < Chapter
51+ { ...chapterDef }
52+ bookId = { bookId }
53+ chapterId = { chapterDef . chapterId }
54+ key = { chapterDef . chapterPath }
55+ index = { index }
56+ setIsChapterIndexVisible = { setIsChapterIndexVisible }
57+ chapterNumber = { chapterNumbers [ index ] }
58+ allAnswers = { allAnswers }
59+ />
60+ ) ) }
61+ { allChapters . length !== chapters . length &&
62+ < div className = "chapter locked" >
63+ < h2 > { t ( "book.locked-chapter" ) } </ h2 >
64+ < p > { t ( `book.locked-chapter-msg-${ unlockChaptersOnAnswers } ` ) } </ p >
65+ </ div >
66+ }
67+ </ >
68+ }
69+
2870export const Book = (
2971 { frontmatter, content, chapters, slug, bookId, previous, next } : BookProps
3072) => {
@@ -274,18 +316,14 @@ export const Book = (
274316 />
275317 }
276318
277- { chapters . map ( ( chapterDef , index ) => (
278- < Chapter
279- { ...chapterDef }
280- bookId = { bookId }
281- chapterId = { chapterDef . chapterId }
282- key = { chapterDef . chapterPath }
283- index = { index }
284- setIsChapterIndexVisible = { setIsChapterIndexVisible }
285- chapterNumber = { chapterNumbers [ index ] }
286- allAnswers = { showAnswers && allAnswers || undefined }
319+ < Chapters
320+ chapters = { chapters }
321+ bookId = { bookId }
322+ chapterNumbers = { chapterNumbers }
323+ unlockChaptersOnAnswers = { frontmatter . unlockChaptersOnAnswers }
324+ setIsChapterIndexVisible = { setIsChapterIndexVisible }
325+ allAnswers = { showAnswers && allAnswers || undefined }
287326 />
288- ) ) }
289327 { ! ! next && < a href = { next . href } className = "next-book-link" >
290328 { t ( "book.next-in-collection" ) } { next . title } </ a >
291329 }
0 commit comments