Skip to content

Commit b193763

Browse files
author
Alexandra Zwinger
committed
Add new Chapter structure (doesn't work yet, because backend is slacking)
1 parent 27e6830 commit b193763

File tree

4 files changed

+62
-41
lines changed

4 files changed

+62
-41
lines changed

src/components/ModuleProgressSettings.tsx

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
11
import React, {useState} from 'react';
22
import '../styles/Modal.css';
33
import {CuteButton} from './CuteButton';
4+
import {Chapter, UserModule} from "../dtos/ModuleDto";
45

56
interface ModalProps {
67
isOpen: boolean;
78
onClose: () => void;
9+
module: UserModule;
810
}
911

10-
interface Checkbox {
11-
id: number;
12-
text: string;
13-
checked: boolean;
14-
}
15-
16-
interface Chapter {
17-
id: number;
18-
title: string;
19-
checkboxes: Checkbox[];
20-
}
12+
const ModuleProgressSettings: React.FC<ModalProps> = ({onClose, module}) => {
13+
const [chapters, setChapters] = useState<Chapter[]>(module.chapter);
2114

22-
const ModuleProgressSettings: React.FC<ModalProps> = ({isOpen, onClose}) => {
23-
const [chapters, setChapters] = useState<Chapter[]>([
24-
{
25-
id: 1,
26-
title: 'Kapitel 1',
27-
checkboxes: [
28-
{id: 1, text: 'Checkbox 1', checked: false},
29-
{id: 2, text: 'Checkbox 2', checked: false},
30-
{id: 3, text: 'Checkbox 3', checked: false},
31-
],
32-
},
33-
]);
34-
35-
const toggleCheckbox = (chapterId: number, checkboxId: number) => {
15+
const toggleCheckbox = (chapterId: string, checkboxId: string) => {
3616
setChapters((prevChapters) =>
3717
prevChapters.map((chapter) =>
3818
chapter.id === chapterId
@@ -49,9 +29,9 @@ const ModuleProgressSettings: React.FC<ModalProps> = ({isOpen, onClose}) => {
4929
);
5030
};
5131

52-
const addCheckbox = (chapterId: number) => {
32+
const addCheckbox = (chapterId: string) => {
5333
const newCheckbox = {
54-
id: Date.now(),
34+
id: Date.now().toString(),
5535
text: `Checkbox ${Math.random()}`,
5636
checked: false,
5737
};
@@ -65,7 +45,7 @@ const ModuleProgressSettings: React.FC<ModalProps> = ({isOpen, onClose}) => {
6545
);
6646
};
6747

68-
const deleteCheckbox = (chapterId: number, checkboxId: number) => {
48+
const deleteCheckbox = (chapterId: string, checkboxId: string) => {
6949
setChapters((prevChapters) =>
7050
prevChapters.map((chapter) =>
7151
chapter.id === chapterId
@@ -82,13 +62,17 @@ const ModuleProgressSettings: React.FC<ModalProps> = ({isOpen, onClose}) => {
8262

8363
const addChapter = () => {
8464
const newChapter = {
85-
id: Date.now(),
65+
id: Date.now().toString(),
8666
title: `Kapitel ${chapters.length + 1}`,
8767
checkboxes: [],
8868
};
8969
setChapters((prev) => [...prev, newChapter]);
9070
};
9171

72+
const saveProgress = () => {
73+
//TODO
74+
}
75+
9276
return (
9377
<div className="modal-overlay" onClick={onClose}>
9478
<div className="modal-content p-8 min-w-[300px] w-[650px] overflow-y-scroll max-h-[80%] max-w-[90%]"
@@ -179,6 +163,7 @@ const ModuleProgressSettings: React.FC<ModalProps> = ({isOpen, onClose}) => {
179163
text={'Speichern'}
180164
bgColor={"#56A095"} textColor={"#e8fcf6"}
181165
classname={'sm:text-2xl text-lg'}
166+
onClick={saveProgress}
182167
/>
183168
</div>
184169

src/components/yourStudies/UserInfo.tsx

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import {getMeetingsOfWeek} from "../../api/MeetingApi";
44
import axiosInstance from "../../AxiosConfig";
55
import {MeetingDto} from "../../dtos/MeetingDto";
66
import ModuleProgressSettings from "../ModuleProgressSettings";
7-
import {ModuleDto} from "../../dtos/ModuleDto";
7+
import {Chapter, Checkbox, UserModule} from "../../dtos/ModuleDto";
88
import {getUser} from "../../api/UserApi";
99

1010
export default function UserInfo() {
1111
const [weeklyMeetings, setWeeklyMeetings] = useState<MeetingDto[]>([]);
1212
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
13-
const [modules, setModules] = useState<ModuleDto[]>([]);
13+
const [modules, setModules] = useState<UserModule[]>([]);
14+
const [activeModule, setActiveModule] = useState<UserModule | undefined>();
1415

1516
const filterMeetingsForCurrentWeek = (meetings: MeetingDto[]) => {
1617
const currentDate = new Date();
@@ -45,22 +46,37 @@ export default function UserInfo() {
4546
fetchUserInfo();
4647
}, []);
4748

48-
const openModal = () => {
49+
const openModal = (moduleName: string) => {
50+
setActiveModule(modules.find((m) => m.name === moduleName));
4951
setIsModalOpen(true);
5052
};
5153

5254
const closeModal = () => {
5355
setIsModalOpen(false);
5456
};
5557

58+
const calculateProgress = (module: UserModule): number => {
59+
if (!module.chapter?.length) return 0;
60+
61+
let checked = 0;
62+
let total = 0;
63+
64+
module.chapter.forEach((chapter: Chapter) => {
65+
total += chapter.checkboxes.length;
66+
checked += chapter.checkboxes.filter((box: Checkbox) => box.checked).length;
67+
});
68+
69+
return total === 0 ? 0 : checked / total;
70+
}
71+
5672
return (
5773
<div className="lg:w-[60%] w-[80%] sm:px-8 mb-16 justify-center mt-8">
5874
<div className="w-full md:px-16">
5975
<p className="text-2xl font-bold text-white text-left lg:mt-4 mt-16 mb-7">Prüfungstermine</p>
6076

6177
<table className="w-full border-collapse hidden md:table">
6278
<tbody>
63-
{modules.map((subject, index) => (
79+
{modules && modules.map((subject, index) => (
6480
<tr key={index}>
6581
<td className="px-1 py-1 text-[#9B9B9B]">{subject.name}</td>
6682
<td className="px-1 py-1 text-[#2AB19D]">DATE</td>
@@ -71,7 +87,7 @@ export default function UserInfo() {
7187
</tbody>
7288
</table>
7389
<div className="md:hidden space-y-4">
74-
{modules.map((subject, index) => (
90+
{modules && modules.map((subject, index) => (
7591
<div key={index} className="p-3 shadow-sm">
7692
<p className="text-[#9B9B9B]">
7793
{subject.name}
@@ -128,17 +144,20 @@ export default function UserInfo() {
128144
</div>
129145

130146
<p className="text-2xl font-bold text-white text-left mt-9 mb-6">Lernfortschritt</p>
131-
{modules.map((subject, index) => (
147+
{modules && modules.map((subject, index) => (
132148
<div key={index} className="mb-6">
133149
<p className="text-m text-[#9B9B9B]">{subject.name}</p>
134-
<div onClick={openModal}>
135-
<ProgressBar progress={0.5}/>
150+
<div onClick={() => {
151+
openModal(subject.name)
152+
}}>
153+
<ProgressBar progress={calculateProgress(subject)}/>
136154
</div>
137155
</div>
138156
))}
139157
</div>
140158
{
141-
isModalOpen && <ModuleProgressSettings isOpen={isModalOpen} onClose={closeModal}/>
159+
isModalOpen && activeModule &&
160+
<ModuleProgressSettings isOpen={isModalOpen} onClose={closeModal} module={activeModule}/>
142161
}
143162
</div>
144163
);

src/dtos/ModuleDto.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
export type ModuleDto = {
22
name: string;
3-
}
3+
}
4+
5+
export type UserModule = {
6+
name: string;
7+
chapter: Chapter[];
8+
}
9+
10+
export type Checkbox = {
11+
id: string;
12+
text: string;
13+
checked: boolean;
14+
}
15+
16+
export type Chapter = {
17+
id: string;
18+
title: string;
19+
checkboxes: Checkbox[];
20+
}

src/dtos/UserDto.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {ModuleDto} from "./ModuleDto";
1+
import {UserModule} from "./ModuleDto";
22

33
export type UserDto = {
44
username: string;
55
uuid: string;
6-
modules: ModuleDto[];
6+
modules: UserModule[];
77
}

0 commit comments

Comments
 (0)