@@ -5,8 +5,10 @@ import ModuleProgressSettings from "../components/ModuleProgressSettings";
55import { getMeetingsOfWeek } from "../api/MeetingApi" ;
66import axiosInstance from "../AxiosConfig" ;
77import { MeetingDto } from "../dtos/MeetingDto" ;
8- import { getUser , updateUsername } from "../api/UserApi" ;
8+ import { getUser , updateUserModules , updateUsername } from "../api/UserApi" ;
99import { UserDto } from "../dtos/UserDto" ;
10+ import { ModuleDto } from "../dtos/ModuleDto" ;
11+ import { createModule , getModules } from "../api/ModuleApi" ;
1012
1113const subjects = [
1214 { name : "Algorithmen und Datenstrukturen" , date : "17.02.2025" , time : "10:30" , room : "HQ.120" , progress : 70 } ,
@@ -20,7 +22,11 @@ export default function YourStudies() {
2022 const [ isModalOpen , setIsModalOpen ] = useState < boolean > ( false ) ;
2123 const [ user , setUser ] = useState < UserDto | undefined > ( ) ;
2224 const [ editProfile , setEditProfile ] = useState ( false ) ;
25+ const [ editModule , setEditModule ] = useState ( false ) ;
2326 const [ profileName , setProfileName ] = useState ( user ?. username ) ;
27+ const [ ownModules , setownModules ] = useState < ModuleDto [ ] > ( [ ] ) ;
28+ const [ allModules , setAllModules ] = useState < ModuleDto [ ] > ( [ ] ) ;
29+ const [ module , setModule ] = useState < string > ( "" ) ;
2430
2531 const filterMeetingsForCurrentWeek = ( meetings : MeetingDto [ ] ) => {
2632 const currentDate = new Date ( ) ;
@@ -33,6 +39,15 @@ export default function YourStudies() {
3339 } ) ;
3440 } ;
3541
42+ const fetchAllModules = async ( ) => {
43+ try {
44+ const response = await getModules ( axiosInstance ) ;
45+ setAllModules ( response ) ;
46+ } catch ( error ) {
47+ alert ( "Error fetching user modules:" + error ) ;
48+ }
49+ }
50+
3651 const fetchMeetings = async ( ) => {
3752 try {
3853 const response = await getMeetingsOfWeek ( axiosInstance ) ;
@@ -53,6 +68,7 @@ export default function YourStudies() {
5368
5469 useEffect ( ( ) => {
5570 fetchMeetings ( ) ;
71+ fetchAllModules ( ) ;
5672 fetchUserInfo ( ) ;
5773 } , [ ] ) ;
5874
@@ -67,12 +83,46 @@ export default function YourStudies() {
6783 }
6884 updateUsername ( axiosInstance , profileName ) ;
6985 editProfileMode ( false ) ;
86+ fetchUserInfo ( ) ;
87+ }
88+
89+ const saveNewModule = async ( ) => {
90+ try {
91+ await createModule ( axiosInstance , module ) ;
92+ fetchAllModules ( ) ;
93+ } catch ( error ) {
94+ alert ( "Error fetching user modules:" + error ) ;
95+ }
96+ }
97+
98+ const saveModules = async ( ) => {
99+ try {
100+ await updateUserModules ( axiosInstance , ownModules ) ;
101+ fetchAllModules ( ) ;
102+ } catch ( error ) {
103+ alert ( "Error fetching user modules:" + error ) ;
104+ }
105+ }
106+
107+ const addModule = ( ) => {
108+ setownModules ( [ ...ownModules , { name : module } ] ) ;
109+ setModule ( "" ) ;
110+ if ( ! allModules . some ( m => m . name === module . toUpperCase ( ) ) )
111+ saveNewModule ( ) ;
112+ }
113+
114+ const deleteModule = ( moduleName : string ) => {
115+ setownModules ( ownModules . filter ( m => m . name !== moduleName ) ) ;
70116 }
71117
72118 const openModal = ( ) => {
73119 setIsModalOpen ( true ) ;
74120 } ;
75121
122+ const editModulesMode = ( mode : boolean ) => {
123+ setEditModule ( mode ) ;
124+ }
125+
76126 const closeModal = ( ) => {
77127 setIsModalOpen ( false ) ;
78128 } ;
@@ -119,17 +169,67 @@ export default function YourStudies() {
119169 < div className = "p-4 lg:mr-20 mr-8 mt-2" >
120170 < table className = "w-full border-collapse" >
121171 < tbody >
122- { subjects . map ( ( subject , index ) => (
123- < tr key = { index } >
124- < td className = "py-2 border-b border-[#4B708C] text-gray-300" > { subject . name } </ td >
125- </ tr >
126- ) ) }
172+ { editModule ? (
173+ < div className = { "flex flex-col gap-4" } >
174+ { ownModules . map ( ( subject ) => (
175+ < div
176+ className = { "flex flex-row justify-between pr-4 py-1 border-b border-[#4B708C] text-gray-300" } >
177+ < p > { subject . name } </ p >
178+ < button
179+ onClick = { ( ) => deleteModule ( subject . name ) }
180+ >
181+ x
182+ </ button >
183+ </ div >
184+ ) ) }
185+ < input
186+ id = "module"
187+ type = "text"
188+ placeholder = { "Modul" }
189+ className = "text-gray-300 block bg-[#333C4F] w-full px-10 py-2 border rounded-full shadow-sm border-[#333C4F] placeholder-gray-550 placeholder:text-xs"
190+ value = { module }
191+ onChange = { ( e ) => setModule ( e . target . value ) }
192+ />
193+
194+ < datalist id = "modules" >
195+ { allModules . map ( ( module , index ) => (
196+ < option key = { index } className = { "w-full" } value = { module . name } />
197+ ) ) }
198+ </ datalist >
199+ < div className = "flex flex-col w-full my-4" >
200+ < button
201+ className = "bg-[#2EF6D9] text-white cursor-pointer p-[10px] border-none w-[30px] h-[30px] rounded font-semibold text-[16px] inline-flex items-center"
202+ onClick = { ( ) => addModule ( ) } >
203+ +
204+ </ button >
205+ </ div >
206+
207+ </ div >
208+ ) : (
209+ ownModules . map ( ( subject , index ) => (
210+ < tr key = { index } >
211+ < td className = "py-2 border-b border-[#4B708C] text-gray-300" > { subject . name } </ td >
212+ </ tr >
213+ ) )
214+
215+ ) }
127216 </ tbody >
128217 </ table >
129218 </ div >
130219 < div className = "mt-3" >
131- < CuteButton bgColor = "#598BB1" classname = "lg:text-lg text-base" textColor = "#e6ebfc"
132- text = "Module verwalten" />
220+ { editModule ? (
221+ < div className = { "flex items-center w-full gap-2 lg:pr-20 pr-8" } >
222+ < CuteButton classname = "lg:text-base text-sm ml-auto" bgColor = { "#598BB1" }
223+ textColor = { "#e6ebfc" } onClick = { ( ) => editModulesMode ( false ) }
224+ text = "Abbrechen" />
225+ < CuteButton classname = "lg:text-lg text-base" bgColor = { "#56A095" } textColor = { "#e8fcf6" }
226+ onClick = { saveModules }
227+ text = "Speichern" />
228+ </ div >
229+ ) : (
230+ < CuteButton bgColor = "#598BB1" classname = "lg:text-lg text-base" textColor = "#e6ebfc"
231+ text = "Module verwalten" onClick = { ( ) => editModulesMode ( true ) } />
232+ ) }
133233 </ div >
134234 </ div >
135235 </ div >
0 commit comments