fix: some error managment and a whole feature missing
This commit is contained in:
parent
dad000a1b9
commit
6bcc3a7e88
5 changed files with 192 additions and 140 deletions
|
|
@ -7,13 +7,11 @@ class ConfirmationPopup extends StatefulWidget {
|
|||
const ConfirmationPopup({
|
||||
super.key,
|
||||
required this.exitPopup,
|
||||
required this.setPrice,
|
||||
required this.viewModel,
|
||||
required this.book,
|
||||
});
|
||||
|
||||
final Function(BuildContext) exitPopup;
|
||||
final Function(num) setPrice;
|
||||
final AddViewModel viewModel;
|
||||
final Book book;
|
||||
|
||||
|
|
@ -24,9 +22,11 @@ class ConfirmationPopup extends StatefulWidget {
|
|||
class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||
double price = 0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return AlertDialog(
|
||||
title: Text("Prix"),
|
||||
content: Form(
|
||||
|
|
@ -72,6 +72,7 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
|||
],
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 10),
|
||||
(widget.viewModel.askPrice)
|
||||
? TextFormField(
|
||||
|
|
@ -87,15 +88,21 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
|||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return "Indiquez un prix";
|
||||
} else if (num.tryParse(value) == null) {
|
||||
} else if (double.tryParse(
|
||||
value.replaceAll(",", "."),
|
||||
) ==
|
||||
null) {
|
||||
return "Le prix doit être un nombre";
|
||||
} else if (num.parse(value) < 0) {
|
||||
} else if (double.parse(value.replaceAll(",", ".")) <
|
||||
0) {
|
||||
return "Le prix doit être positif ou nul";
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (newValue) {
|
||||
price = double.parse(newValue!);
|
||||
price = double.parse(
|
||||
newValue?.replaceAll(",", ".") ?? "0",
|
||||
);
|
||||
},
|
||||
)
|
||||
: SizedBox(),
|
||||
|
|
@ -105,6 +112,7 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
|||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: Text("Annuler"),
|
||||
onPressed: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
|
|
@ -114,13 +122,15 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
|||
);
|
||||
widget.exitPopup(context);
|
||||
},
|
||||
child: Text("Annuler"),
|
||||
),
|
||||
TextButton(
|
||||
child: Text("Valider"),
|
||||
onPressed: () async {
|
||||
if (widget.viewModel.askPrice &&
|
||||
_formKey.currentState!.validate()) {
|
||||
_formKey.currentState!.save();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
var result = await widget.viewModel.sendNewBookInstance(
|
||||
|
|
@ -136,49 +146,61 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
|||
Navigator.of(context).pop();
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: Text(
|
||||
"ID : ${widget.viewModel.currentOwner!.firstName[0].toUpperCase()}${widget.viewModel.currentOwner!.lastName[0].toUpperCase()}${(price == 0) ? "PL" : price.toString()}",
|
||||
),
|
||||
content: Text(
|
||||
(widget.viewModel.currentOwner!.id ==
|
||||
widget.viewModel.ownerOfUser!.id)
|
||||
? "Ce livre appartient à la section. Vous pouvez mettre le code, ou poser une gomette, ..."
|
||||
: "Identifiant propriétaire de ce livre. Pensez à l'écrire pour retrouver lae propriétaire du livre lors de la vente ou du retour !",
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
widget.exitPopup(context);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Enregistré: ${widget.book.title}",
|
||||
),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Text("Ok"),
|
||||
),
|
||||
],
|
||||
),
|
||||
barrierDismissible: false,
|
||||
builder: (context) =>
|
||||
RegisteredBookPopup(widget: widget, price: price),
|
||||
);
|
||||
}
|
||||
break;
|
||||
case Error():
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Une erreur est survenue : ${result.error}",
|
||||
),
|
||||
),
|
||||
SnackBar(content: Text("Erreur : ${result.error}")),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
child: Text("Valider"),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class RegisteredBookPopup extends StatelessWidget {
|
||||
const RegisteredBookPopup({
|
||||
super.key,
|
||||
required this.widget,
|
||||
required this.price,
|
||||
});
|
||||
|
||||
final ConfirmationPopup widget;
|
||||
final double price;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
// This thing is the BookInstance's short ID
|
||||
title: Text(
|
||||
"ID : ${widget.viewModel.currentOwner!.firstName[0].toUpperCase()}${widget.viewModel.currentOwner!.lastName[0].toUpperCase()}${(price == 0) ? "PL" : price.toString()}",
|
||||
),
|
||||
content: Text(
|
||||
(widget.viewModel.currentOwner!.id == widget.viewModel.ownerOfUser!.id)
|
||||
? "Pensez à la gomette ! Ce livre appartient au syndicat."
|
||||
: "Identifiant propriétaire de ce livre. Pensez à l'écrire pour retrouver lae propriétaire du livre lors de la vente ou du retour !",
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: Text("Ok"),
|
||||
onPressed: () {
|
||||
widget.exitPopup(context);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text("Livre enregistré: ${widget.book.title}"),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
|
|
|
|||
Reference in a new issue