Skip to content

Commit ee58be7

Browse files
authored
Merge pull request #46 from openpatch/copilot/add-delete-button-student
feat: add delete student action to edit sheet
2 parents 730e2c2 + 03647a4 commit ee58be7

4 files changed

Lines changed: 57 additions & 1 deletion

File tree

assets/translations/de.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"confirm_archive_group_body": "Die Gruppe verschwindet aus der aktiven Liste, alle Daten bleiben erhalten.",
7070
"confirm_delete": "{name} löschen?",
7171
"confirm_delete_group_body": "Schüler:innen, Noten, Listen und verknüpfte Notizen dieser Gruppe werden gelöscht.",
72+
"confirm_delete_student_body": "Alle Noten, Anwesenheiten, Materialien, Hausaufgaben, Notizen und Listeneinträge dieser Schüler:in werden gelöscht.",
7273
"confirm_passphrase": "Passphrase bestätigen",
7374
"create_database": "Bibliothek erstellen",
7475
"create_another_database": "Weitere Bibliothek erstellen",

assets/translations/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"confirm_archive_group_body": "The group will leave the active list, but its data stays available.",
7070
"confirm_delete": "Delete {name}?",
7171
"confirm_delete_group_body": "Students, grades, lists, and linked notes in this group will be deleted.",
72+
"confirm_delete_student_body": "All grades, attendance, material, homework, notes, and list entries for this student will be deleted.",
7273
"confirm_passphrase": "Confirm passphrase",
7374
"create_database": "Create library",
7475
"create_another_database": "Create another library",

lib/features/students/student_detail_screen.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,30 @@ class StudentDetailScreen extends ConsumerWidget {
187187
WidgetRef ref,
188188
Student student,
189189
) async {
190+
var deleted = false;
190191
final result = await showStudentFormSheet(
191192
context: context,
192193
initialFirstName: student.firstName,
193194
initialLastName: student.lastName,
194195
initialOriginNote: student.originNote,
195196
initialAvatarJson: student.avatarJson,
196197
title: 'edit'.tr(),
198+
onDelete: () async {
199+
await ref
200+
.read(studentRepositoryProvider)
201+
.deleteStudent(student.id);
202+
deleted = true;
203+
},
197204
);
205+
if (deleted) {
206+
if (!context.mounted) return;
207+
if (context.canPop()) {
208+
context.pop();
209+
} else {
210+
context.go('/groups/${student.groupId}');
211+
}
212+
return;
213+
}
198214
if (result == null) {
199215
return;
200216
}

lib/features/students/student_form.dart

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
55
import '../../core/database/app_database.dart';
66
import '../../core/providers/app_providers.dart';
77
import '../../shared/utils/formatting.dart';
8+
import '../../shared/widgets/confirm_dialog.dart';
89
import '../../shared/widgets/student_avatar.dart';
910
import '../avatar/avatar_editor_sheet.dart';
1011
import 'student_draft.dart';
@@ -18,6 +19,7 @@ Future<StudentFormResult?> showStudentFormSheet({
1819
String? initialOriginNote,
1920
String? initialAvatarJson,
2021
String? title,
22+
Future<void> Function()? onDelete,
2123
}) {
2224
return showModalBottomSheet<StudentFormResult>(
2325
context: context,
@@ -30,6 +32,7 @@ Future<StudentFormResult?> showStudentFormSheet({
3032
initialOriginNote: initialOriginNote,
3133
initialAvatarJson: initialAvatarJson,
3234
title: title,
35+
onDelete: onDelete,
3336
),
3437
);
3538
}
@@ -41,13 +44,15 @@ class _StudentFormSheet extends ConsumerStatefulWidget {
4144
this.initialOriginNote,
4245
this.initialAvatarJson,
4346
this.title,
47+
this.onDelete,
4448
});
4549

4650
final String? initialFirstName;
4751
final String? initialLastName;
4852
final String? initialOriginNote;
4953
final String? initialAvatarJson;
5054
final String? title;
55+
final Future<void> Function()? onDelete;
5156

5257
@override
5358
ConsumerState<_StudentFormSheet> createState() => _StudentFormSheetState();
@@ -165,8 +170,18 @@ class _StudentFormSheetState extends ConsumerState<_StudentFormSheet> {
165170
),
166171
const SizedBox(height: 24),
167172
Row(
168-
mainAxisAlignment: MainAxisAlignment.end,
169173
children: [
174+
if (widget.onDelete != null) ...[
175+
TextButton(
176+
onPressed: _delete,
177+
style: TextButton.styleFrom(
178+
foregroundColor:
179+
Theme.of(context).colorScheme.error,
180+
),
181+
child: Text('delete'.tr()),
182+
),
183+
],
184+
const Spacer(),
170185
TextButton(
171186
onPressed: () => Navigator.of(context).pop(),
172187
child: Text('cancel'.tr()),
@@ -222,4 +237,27 @@ class _StudentFormSheetState extends ConsumerState<_StudentFormSheet> {
222237
avatarJson: _avatarJson,
223238
));
224239
}
240+
241+
Future<void> _delete() async {
242+
final confirmed = await showConfirmDialog(
243+
context: context,
244+
title: 'confirm_delete'.tr(
245+
namedArgs: {
246+
'name': studentDisplayName(
247+
firstName: _firstNameController.text.trim(),
248+
lastName: _lastNameController.text.trim(),
249+
),
250+
},
251+
),
252+
body: 'confirm_delete_student_body'.tr(),
253+
);
254+
if (!confirmed || !mounted) {
255+
return;
256+
}
257+
await widget.onDelete!();
258+
if (!mounted) {
259+
return;
260+
}
261+
Navigator.of(context).pop();
262+
}
225263
}

0 commit comments

Comments
 (0)