@@ -3,24 +3,24 @@ import '../styles/Modal.css';
33import { CuteButton } from './CuteButton' ;
44import { Chapter , UserModule } from "../dtos/ModuleDto" ;
55import axiosInstance from "../AxiosConfig" ;
6- import { saveModuleProgress } from "../api/ModuleApi " ;
6+ import { saveModuleProgress } from "../api/UserApi " ;
77
88interface ModalProps {
9- isOpen : boolean ;
109 onClose : ( ) => void ;
1110 module : UserModule ;
11+ allUserModules : UserModule [ ] ;
1212}
1313
14- const ModuleProgressSettings : React . FC < ModalProps > = ( { onClose, module} ) => {
14+ const ModuleProgressSettings : React . FC < ModalProps > = ( { onClose, module, allUserModules } ) => {
1515 const [ chapters , setChapters ] = useState < Chapter [ ] > ( module . chapter ) ;
1616
17- const toggleCheckbox = ( chapterId : string , checkboxId : string ) => {
17+ const toggleCheckbox = ( chapterId : number , checkboxId : number ) => {
1818 setChapters ( ( prevChapters ) =>
1919 prevChapters . map ( ( chapter ) =>
2020 chapter . id === chapterId
2121 ? {
2222 ...chapter ,
23- checkboxes : chapter . checkboxes . map ( ( checkbox ) =>
23+ checkbox : chapter . checkbox . map ( ( checkbox ) =>
2424 checkbox . id === checkboxId
2525 ? { ...checkbox , checked : ! checkbox . checked }
2626 : checkbox
@@ -31,29 +31,29 @@ const ModuleProgressSettings: React.FC<ModalProps> = ({onClose, module}) => {
3131 ) ;
3232 } ;
3333
34- const addCheckbox = ( chapterId : string ) => {
34+ const addCheckbox = ( chapterId : number ) => {
3535 const newCheckbox = {
36- id : Date . now ( ) . toString ( ) ,
37- text : `Checkbox ${ Math . random ( ) } ` ,
36+ id : Date . now ( ) + Math . floor ( Math . random ( ) * 1000 ) ,
37+ title : `Add Title ` ,
3838 checked : false ,
3939 } ;
4040
4141 setChapters ( ( prevChapters ) =>
4242 prevChapters . map ( ( chapter ) =>
4343 chapter . id === chapterId
44- ? { ...chapter , checkboxes : [ ...chapter . checkboxes , newCheckbox ] }
44+ ? { ...chapter , checkbox : [ ...chapter . checkbox , newCheckbox ] }
4545 : chapter
4646 )
4747 ) ;
4848 } ;
4949
50- const deleteCheckbox = ( chapterId : string , checkboxId : string ) => {
50+ const deleteCheckbox = ( chapterId : number , checkboxId : number ) => {
5151 setChapters ( ( prevChapters ) =>
5252 prevChapters . map ( ( chapter ) =>
5353 chapter . id === chapterId
5454 ? {
5555 ...chapter ,
56- checkboxes : chapter . checkboxes . filter (
56+ checkbox : chapter . checkbox . filter (
5757 ( checkbox ) => checkbox . id !== checkboxId
5858 ) ,
5959 }
@@ -64,17 +64,21 @@ const ModuleProgressSettings: React.FC<ModalProps> = ({onClose, module}) => {
6464
6565 const addChapter = ( ) => {
6666 const newChapter = {
67- id : Date . now ( ) . toString ( ) ,
67+ id : Date . now ( ) + Math . floor ( Math . random ( ) * 1000 ) ,
6868 title : `Kapitel ${ chapters . length + 1 } ` ,
69- checkboxes : [ ] ,
69+ checkbox : [ ] ,
7070 } ;
7171 setChapters ( ( prev ) => [ ...prev , newChapter ] ) ;
7272 } ;
7373
7474 const saveProgress = ( ) => {
7575 try {
7676 module . chapter = chapters ;
77- saveModuleProgress ( axiosInstance , module ) ;
77+ const updatedModules = allUserModules . map ( userModule =>
78+ userModule . name === module . name ? module : userModule
79+ ) ;
80+ saveModuleProgress ( axiosInstance , updatedModules ) ;
81+ onClose ( ) ;
7882 } catch ( error ) {
7983 console . error ( "Error saving progress:" + error ) ;
8084 }
@@ -102,7 +106,7 @@ const ModuleProgressSettings: React.FC<ModalProps> = ({onClose, module}) => {
102106 </ button >
103107 </ div >
104108 < div className = "checkbox-group sm:ml-8 mb-4" >
105- { chapter . checkboxes . map ( ( checkbox ) => (
109+ { chapter . checkbox . map ( ( checkbox ) => (
106110 < div key = { checkbox . id } className = "checkbox-item w-[65%]" >
107111 < input
108112 type = "checkbox"
@@ -113,17 +117,17 @@ const ModuleProgressSettings: React.FC<ModalProps> = ({onClose, module}) => {
113117 < input
114118 type = "text"
115119 className = { "flex-grow px-2" }
116- value = { checkbox . text }
120+ value = { checkbox . title }
117121 onChange = { ( e ) => {
118122 const newText = e . target . value ;
119123 setChapters ( ( prevChapters ) =>
120124 prevChapters . map ( ( ch ) =>
121125 ch . id === chapter . id
122126 ? {
123127 ...ch ,
124- checkboxes : ch . checkboxes . map ( ( c ) =>
128+ checkbox : ch . checkbox . map ( ( c ) =>
125129 c . id === checkbox . id
126- ? { ...c , text : newText }
130+ ? { ...c , title : newText }
127131 : c
128132 ) ,
129133 }
@@ -151,14 +155,17 @@ const ModuleProgressSettings: React.FC<ModalProps> = ({onClose, module}) => {
151155 </ button >
152156 < label className = "text-white" > Checkbox </ label >
153157
154- < button className = "add-chapter-btn" onClick = { addChapter } >
155- +
156- </ button >
157- < label className = "text-white" > Kapitel </ label >
158158 </ div >
159159 </ div >
160160 ) ) }
161161
162+ < div className = "add-checkbox-container sm:ml-8" >
163+ < button className = "add-chapter-btn" onClick = { addChapter } >
164+ +
165+ </ button >
166+ < label className = "text-white" > Kapitel </ label >
167+ </ div >
168+
162169 < div className = "mt-auto flex justify-end items-center gap-4" >
163170 < CuteButton
164171 onClick = { onClose }
0 commit comments