feat(frontend): connect return flow to api
This commit is contained in:
@@ -3,6 +3,7 @@ import "package:google_fonts/google_fonts.dart";
|
||||
import "../theme/ensup_colors.dart";
|
||||
import "../models/emprunt_item.dart";
|
||||
import "../models/anomalie_retour.dart";
|
||||
import "../services/emprunt_service.dart";
|
||||
import "../widgets/ensup_top_bar.dart";
|
||||
import "../widgets/brand_corners.dart";
|
||||
import "../widgets/etat_checklist.dart";
|
||||
@@ -20,27 +21,83 @@ class ChecklistRetourScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _ChecklistRetourScreenState extends State<ChecklistRetourScreen> {
|
||||
late final List<String> _elements = widget.emprunt.materiel.accessoires.isEmpty
|
||||
? <String>[widget.emprunt.materiel.nom]
|
||||
: widget.emprunt.materiel.accessoires;
|
||||
late final List<EtatChecklist> _etats = List<EtatChecklist>.filled(
|
||||
widget.emprunt.materiel.accessoires.length,
|
||||
_elements.length,
|
||||
EtatChecklist.present,
|
||||
);
|
||||
final TextEditingController _commentaireController = TextEditingController();
|
||||
bool _envoiEnCours = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_commentaireController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _valider() async {
|
||||
setState(() => _envoiEnCours = true);
|
||||
|
||||
void _valider() {
|
||||
final accessoires = widget.emprunt.materiel.accessoires;
|
||||
final anomalies = <AnomalieRetour>[];
|
||||
for (var i = 0; i < _etats.length; i++) {
|
||||
if (_etats[i] == EtatChecklist.absent) {
|
||||
anomalies.add(AnomalieRetour(element: accessoires[i], etatRetour: "Absent au retour"));
|
||||
anomalies.add(AnomalieRetour(element: _elements[i], etatRetour: "Absent au retour"));
|
||||
} else if (_etats[i] == EtatChecklist.deteriore) {
|
||||
anomalies.add(AnomalieRetour(element: accessoires[i], etatRetour: "Détérioré au retour"));
|
||||
anomalies.add(AnomalieRetour(element: _elements[i], etatRetour: "Détérioré au retour"));
|
||||
}
|
||||
}
|
||||
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ResultatRetourScreen(emprunt: widget.emprunt, anomalies: anomalies),
|
||||
),
|
||||
);
|
||||
try {
|
||||
final resultat = await EmpruntService.restituerEmprunt(
|
||||
empruntId: widget.emprunt.id,
|
||||
commentaireRetour: _commentaireController.text,
|
||||
elements: _elements.asMap().entries.map((entry) {
|
||||
return {
|
||||
"nomElement": entry.value,
|
||||
"etat": _etatApi(_etats[entry.key]),
|
||||
"quantiteConstatee": _etats[entry.key] == EtatChecklist.absent ? 0 : 1,
|
||||
};
|
||||
}).toList(),
|
||||
);
|
||||
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
final anomaliesAffichees = resultat.statut == "CLOTURE" ? <AnomalieRetour>[] : anomalies;
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ResultatRetourScreen(
|
||||
emprunt: widget.emprunt.copyWith(statut: resultat.statut),
|
||||
anomalies: anomaliesAffichees,
|
||||
),
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(error.toString())),
|
||||
);
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _envoiEnCours = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _etatApi(EtatChecklist etat) {
|
||||
switch (etat) {
|
||||
case EtatChecklist.present:
|
||||
return "PRESENT";
|
||||
case EtatChecklist.absent:
|
||||
return "ABSENT";
|
||||
case EtatChecklist.deteriore:
|
||||
return "DETERIORE";
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -144,7 +201,7 @@ class _ChecklistRetourScreenState extends State<ChecklistRetourScreen> {
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ChecklistTableau(
|
||||
accessoires: materiel.accessoires,
|
||||
accessoires: _elements,
|
||||
etats: _etats,
|
||||
onChanged: (index, etat) => setState(() => _etats[index] = etat),
|
||||
),
|
||||
@@ -163,15 +220,24 @@ class _ChecklistRetourScreenState extends State<ChecklistRetourScreen> {
|
||||
"Indiquez leur état au retour ; toute différence sera signalée.",
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
const ChecklistCommentaire(hint: "Commentaire optionnel..."),
|
||||
ChecklistCommentaire(
|
||||
controller: _commentaireController,
|
||||
hint: "Commentaire optionnel...",
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: FilledButton.icon(
|
||||
onPressed: _valider,
|
||||
icon: const Icon(Icons.check, size: 18),
|
||||
onPressed: _envoiEnCours ? null : _valider,
|
||||
icon: _envoiEnCours
|
||||
? const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
|
||||
)
|
||||
: const Icon(Icons.check, size: 18),
|
||||
label: Text(
|
||||
"Valider le retour",
|
||||
_envoiEnCours ? "Enregistrement..." : "Valider le retour",
|
||||
style: GoogleFonts.darkerGrotesque(fontWeight: FontWeight.w700, fontSize: 16),
|
||||
),
|
||||
style: FilledButton.styleFrom(
|
||||
|
||||
Reference in New Issue
Block a user