Skip to content

Commit 3c4dfc6

Browse files
committed
add checkbox progress bar logic
1 parent 782f887 commit 3c4dfc6

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/components/ModuleProgressSettings.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useState} from 'react';
1+
import React, {useState, useEffect} from 'react';
22
import '../styles/Modal.css';
33
import {CuteButton} from './CuteButton';
44
import {Chapter, UserModule} from "../dtos/ModuleDto";
@@ -13,6 +13,16 @@ interface ModalProps {
1313

1414
const ModuleProgressSettings: React.FC<ModalProps> = ({onClose, module, allUserModules}) => {
1515
const [chapters, setChapters] = useState<Chapter[]>(module.chapter);
16+
const [progress, setProgress] = useState<number>(0);
17+
18+
// Berechne den Fortschritt (in %) bei jedem Ändern der Checkboxen
19+
useEffect(() => {
20+
const allBoxes = chapters.flatMap(ch => ch.checkbox);
21+
const total = allBoxes.length;
22+
const checkedCount = allBoxes.filter(box => box.checked).length;
23+
const percent = total > 0 ? Math.round((checkedCount / total) * 100) : 0;
24+
setProgress(percent);
25+
}, [chapters]);
1626

1727
const toggleCheckbox = (chapterId: number, checkboxId: number) => {
1828
setChapters((prevChapters) =>
@@ -91,6 +101,13 @@ const ModuleProgressSettings: React.FC<ModalProps> = ({onClose, module, allUserM
91101
<p className="text-2xl font-bold text-white text-left sm:ml-3">
92102
Lernfortschritt
93103
</p>
104+
105+
{/* Progress-Bar */}
106+
<div className="progress-container sm:ml-3 mb-6">
107+
<div className="progress-bar" style={{ width: `${progress}%` }} />
108+
<span className="text-white text-sm ml-2">{progress}%</span>
109+
</div>
110+
94111
{chapters.map((chapter) => (
95112
<div key={chapter.id}>
96113
<div className="flex gap-4 items-center mt-2 sm:ml-3 mb-4">
@@ -99,8 +116,7 @@ const ModuleProgressSettings: React.FC<ModalProps> = ({onClose, module, allUserM
99116
</p>
100117
<button
101118
className="delete-btn"
102-
onClick={() => {
103-
}}
119+
onClick={() => deleteCheckbox(chapter.id, -1)}
104120
>
105121
x
106122
</button>

0 commit comments

Comments
 (0)