feat(frontend): connect return flow to api
This commit is contained in:
@@ -1,38 +1,59 @@
|
||||
import "package:flutter/material.dart";
|
||||
import "package:google_fonts/google_fonts.dart";
|
||||
import "../theme/ensup_colors.dart";
|
||||
import "../models/materiel_item.dart";
|
||||
import "../models/emprunt_item.dart";
|
||||
import "../services/emprunt_service.dart";
|
||||
import "../services/materiel_service.dart";
|
||||
import "../widgets/ensup_top_bar.dart";
|
||||
import "../widgets/brand_corners.dart";
|
||||
import "checklist_retour_screen.dart";
|
||||
|
||||
const _pcDell = MaterielItem(
|
||||
id: 0,
|
||||
nom: "PC Dell Latitude 5420",
|
||||
categorie: "Ordinateur",
|
||||
marque: "Dell",
|
||||
modele: "Latitude 5420",
|
||||
reference: "ENS-PC-001",
|
||||
campus: "Paris",
|
||||
etatGeneral: "Bon état",
|
||||
disponible: false,
|
||||
accessoires: ["Chargeur 65W", "Souris sans fil", "Housse de protection", "Câble HDMI"],
|
||||
icon: Icons.laptop_mac,
|
||||
);
|
||||
|
||||
const _emprunts = <EmpruntItem>[
|
||||
EmpruntItem(materiel: _pcDell, dateEmprunt: "28/05/2026 à 09:15", retourPrevu: "29/05"),
|
||||
];
|
||||
|
||||
/// Sélection de l'emprunt en cours à restituer (RG15 : ses propres emprunts).
|
||||
class RestitutionSelectScreen extends StatelessWidget {
|
||||
class RestitutionSelectScreen extends StatefulWidget {
|
||||
const RestitutionSelectScreen({super.key});
|
||||
|
||||
void _restituer(BuildContext context, EmpruntItem emprunt) {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => ChecklistRetourScreen(emprunt: emprunt)),
|
||||
);
|
||||
@override
|
||||
State<RestitutionSelectScreen> createState() => _RestitutionSelectScreenState();
|
||||
}
|
||||
|
||||
class _RestitutionSelectScreenState extends State<RestitutionSelectScreen> {
|
||||
late Future<List<EmpruntItem>> _future;
|
||||
int? _chargementId;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_future = EmpruntService.getMesEmprunts();
|
||||
}
|
||||
|
||||
void _recharger() {
|
||||
setState(() => _future = EmpruntService.getMesEmprunts());
|
||||
}
|
||||
|
||||
Future<void> _restituer(EmpruntItem emprunt) async {
|
||||
setState(() => _chargementId = emprunt.id);
|
||||
try {
|
||||
final detail = await MaterielService.getDetail(emprunt.materiel.id);
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ChecklistRetourScreen(emprunt: emprunt.copyWith(materiel: detail)),
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(error.toString())),
|
||||
);
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _chargementId = null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -68,29 +89,23 @@ class RestitutionSelectScreen extends StatelessWidget {
|
||||
onBack: () => Navigator.of(context).pop(),
|
||||
),
|
||||
Expanded(
|
||||
child: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 28),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"Sélectionner le matériel à restituer",
|
||||
style: GoogleFonts.darkerGrotesque(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.w900,
|
||||
color: EnsupColors.text,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
for (final emprunt in _emprunts)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 10),
|
||||
child: _EmpruntCard(
|
||||
emprunt: emprunt,
|
||||
onRestituer: () => _restituer(context, emprunt),
|
||||
),
|
||||
),
|
||||
],
|
||||
child: FutureBuilder<List<EmpruntItem>>(
|
||||
future: _future,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
return _ErreurState(
|
||||
message: snapshot.error.toString(),
|
||||
onRetry: _recharger,
|
||||
);
|
||||
}
|
||||
|
||||
return _contenu(snapshot.data ?? const []);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -104,12 +119,50 @@ class RestitutionSelectScreen extends StatelessWidget {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _contenu(List<EmpruntItem> emprunts) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"Sélectionner le matériel à restituer",
|
||||
style: GoogleFonts.darkerGrotesque(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.w900,
|
||||
color: EnsupColors.text,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Expanded(
|
||||
child: emprunts.isEmpty
|
||||
? const _EmptyState()
|
||||
: ListView.separated(
|
||||
itemCount: emprunts.length,
|
||||
separatorBuilder: (_, _) => const SizedBox(height: 10),
|
||||
itemBuilder: (context, index) {
|
||||
final emprunt = emprunts[index];
|
||||
return _EmpruntCard(
|
||||
emprunt: emprunt,
|
||||
loading: _chargementId == emprunt.id,
|
||||
onRestituer: () => _restituer(emprunt),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _EmpruntCard extends StatelessWidget {
|
||||
const _EmpruntCard({required this.emprunt, required this.onRestituer});
|
||||
const _EmpruntCard({
|
||||
required this.emprunt,
|
||||
required this.loading,
|
||||
required this.onRestituer,
|
||||
});
|
||||
|
||||
final EmpruntItem emprunt;
|
||||
final bool loading;
|
||||
final VoidCallback onRestituer;
|
||||
|
||||
@override
|
||||
@@ -161,7 +214,7 @@ class _EmpruntCard extends StatelessWidget {
|
||||
borderRadius: BorderRadius.circular(999),
|
||||
),
|
||||
child: Text(
|
||||
"En cours",
|
||||
emprunt.statut == "EN_RETARD" ? "En retard" : "En cours",
|
||||
style: GoogleFonts.darkerGrotesque(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w700,
|
||||
@@ -171,14 +224,14 @@ class _EmpruntCard extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
FilledButton(
|
||||
onPressed: onRestituer,
|
||||
onPressed: loading ? null : onRestituer,
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor: EnsupColors.cyan,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
|
||||
),
|
||||
child: Text(
|
||||
"Restituer",
|
||||
loading ? "Chargement..." : "Restituer",
|
||||
style: GoogleFonts.darkerGrotesque(fontWeight: FontWeight.w700, fontSize: 14),
|
||||
),
|
||||
),
|
||||
@@ -187,3 +240,50 @@ class _EmpruntCard extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _EmptyState extends StatelessWidget {
|
||||
const _EmptyState();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Text(
|
||||
"Aucun emprunt en cours à restituer",
|
||||
style: GoogleFonts.titilliumWeb(fontSize: 14, color: EnsupColors.muted),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ErreurState extends StatelessWidget {
|
||||
const _ErreurState({required this.message, required this.onRetry});
|
||||
|
||||
final String message;
|
||||
final VoidCallback onRetry;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Icon(Icons.cloud_off_outlined, size: 40, color: EnsupColors.muted),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
message,
|
||||
textAlign: TextAlign.center,
|
||||
style: GoogleFonts.titilliumWeb(fontSize: 14, color: EnsupColors.text2),
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
OutlinedButton(
|
||||
onPressed: onRetry,
|
||||
child: Text(
|
||||
"Réessayer",
|
||||
style: GoogleFonts.darkerGrotesque(fontWeight: FontWeight.w700),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user