Extrait IdentityBlock réutilisable, aligne les modales parent/enfant sur le style validation, corrige le parsing parentChildren et les URLs /uploads en Flutter web. Co-authored-by: Cursor <cursoragent@cursor.com>
109 lines
3.1 KiB
Dart
109 lines
3.1 KiB
Dart
import 'package:p_tits_pas/models/user.dart';
|
|
|
|
/// Enfant tel que renvoyé par `GET /enfants` (dashboard admin).
|
|
class EnfantAdminModel {
|
|
final String id;
|
|
final String? firstName;
|
|
final String? lastName;
|
|
final String? gender;
|
|
final String? birthDate;
|
|
final String? dueDate;
|
|
final String status;
|
|
final String? photoUrl;
|
|
final bool consentPhoto;
|
|
final bool isMultiple;
|
|
final List<EnfantParentLink> parentLinks;
|
|
|
|
EnfantAdminModel({
|
|
required this.id,
|
|
this.firstName,
|
|
this.lastName,
|
|
this.gender,
|
|
this.birthDate,
|
|
this.dueDate,
|
|
required this.status,
|
|
this.photoUrl,
|
|
this.consentPhoto = false,
|
|
this.isMultiple = false,
|
|
this.parentLinks = const [],
|
|
});
|
|
|
|
String get fullName {
|
|
final fn = (firstName ?? '').trim();
|
|
final ln = (lastName ?? '').trim();
|
|
if (fn.isEmpty && ln.isEmpty) return 'Enfant';
|
|
if (ln.isEmpty) return fn;
|
|
return '$fn $ln';
|
|
}
|
|
|
|
factory EnfantAdminModel.fromJson(Map<String, dynamic> json) {
|
|
final linksRaw = json['parentLinks'] as List?;
|
|
final links = <EnfantParentLink>[];
|
|
if (linksRaw != null) {
|
|
for (final item in linksRaw) {
|
|
if (item is Map<String, dynamic>) {
|
|
links.add(EnfantParentLink.fromJson(item));
|
|
}
|
|
}
|
|
}
|
|
|
|
return EnfantAdminModel(
|
|
id: (json['id'] ?? '').toString(),
|
|
firstName: json['first_name'] as String?,
|
|
lastName: json['last_name'] as String?,
|
|
gender: json['gender'] as String?,
|
|
birthDate: _dateString(json['birth_date']),
|
|
dueDate: _dateString(json['due_date']),
|
|
status: (json['status'] ?? '').toString(),
|
|
photoUrl: json['photo_url'] as String?,
|
|
consentPhoto: json['consent_photo'] == true,
|
|
isMultiple: json['is_multiple'] == true,
|
|
parentLinks: links,
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toUpdateJson() {
|
|
return {
|
|
if (firstName != null) 'first_name': firstName,
|
|
if (lastName != null) 'last_name': lastName,
|
|
if (gender != null && gender!.isNotEmpty) 'gender': gender,
|
|
'status': status,
|
|
if (birthDate != null && birthDate!.isNotEmpty) 'birth_date': birthDate,
|
|
if (dueDate != null && dueDate!.isNotEmpty) 'due_date': dueDate,
|
|
'consent_photo': consentPhoto,
|
|
'is_multiple': isMultiple,
|
|
};
|
|
}
|
|
|
|
static String? _dateString(dynamic v) {
|
|
if (v == null) return null;
|
|
if (v is String) return v.split('T').first;
|
|
return v.toString().split('T').first;
|
|
}
|
|
}
|
|
|
|
class EnfantParentLink {
|
|
final String parentId;
|
|
final String? parentName;
|
|
|
|
EnfantParentLink({required this.parentId, this.parentName});
|
|
|
|
factory EnfantParentLink.fromJson(Map<String, dynamic> json) {
|
|
final parentId =
|
|
(json['parentId'] ?? json['id_parent'] ?? '').toString();
|
|
String? name;
|
|
final parent = json['parent'];
|
|
if (parent is Map<String, dynamic>) {
|
|
final user = parent['user'];
|
|
if (user is Map<String, dynamic>) {
|
|
final u = AppUser.fromJson(user);
|
|
name = u.fullName.isNotEmpty ? u.fullName : u.email;
|
|
}
|
|
}
|
|
return EnfantParentLink(
|
|
parentId: parentId,
|
|
parentName: name,
|
|
);
|
|
}
|
|
}
|