208 lines
6.7 KiB
Dart
208 lines
6.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:seshat/domain/models/book.dart';
|
|
import 'package:seshat/ui/add_page/view_model/add_view_model.dart';
|
|
import 'package:seshat/utils/result.dart';
|
|
|
|
class ConfirmationPopup extends StatefulWidget {
|
|
const ConfirmationPopup({
|
|
super.key,
|
|
required this.exitPopup,
|
|
required this.viewModel,
|
|
required this.book,
|
|
});
|
|
|
|
final Function(BuildContext) exitPopup;
|
|
final AddViewModel viewModel;
|
|
final Book book;
|
|
|
|
@override
|
|
State<ConfirmationPopup> createState() => _ConfirmationPopupState();
|
|
}
|
|
|
|
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(
|
|
key: _formKey,
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
RichText(
|
|
text: TextSpan(
|
|
style: theme.textTheme.bodyMedium,
|
|
children: [
|
|
TextSpan(
|
|
text: "Titre : ",
|
|
style: TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
TextSpan(text: widget.book.title),
|
|
],
|
|
),
|
|
),
|
|
RichText(
|
|
text: TextSpan(
|
|
style: theme.textTheme.bodyMedium,
|
|
children: [
|
|
TextSpan(
|
|
text: "Auteur·ice : ",
|
|
style: TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
TextSpan(text: widget.book.author),
|
|
],
|
|
),
|
|
),
|
|
RichText(
|
|
text: TextSpan(
|
|
style: theme.textTheme.bodyMedium,
|
|
children: [
|
|
TextSpan(
|
|
text: "Prix à neuf : ",
|
|
style: TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
TextSpan(text: widget.book.priceNew.replaceAll("EUR", "€")),
|
|
],
|
|
),
|
|
),
|
|
|
|
SizedBox(height: 10),
|
|
(widget.viewModel.askPrice)
|
|
? TextFormField(
|
|
decoration: InputDecoration(
|
|
labelText: "Prix",
|
|
border: OutlineInputBorder(),
|
|
suffixText: "€",
|
|
),
|
|
keyboardType: TextInputType.numberWithOptions(
|
|
decimal: true,
|
|
signed: true,
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return "Indiquez un prix";
|
|
} else if (double.tryParse(
|
|
value.replaceAll(",", "."),
|
|
) ==
|
|
null) {
|
|
return "Le prix doit être un nombre";
|
|
} else if (double.parse(value.replaceAll(",", ".")) <
|
|
0) {
|
|
return "Le prix doit être positif ou nul";
|
|
}
|
|
return null;
|
|
},
|
|
onSaved: (newValue) {
|
|
price = double.parse(
|
|
newValue?.replaceAll(",", ".") ?? "0",
|
|
);
|
|
},
|
|
)
|
|
: SizedBox(),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
child: Text("Annuler"),
|
|
onPressed: () {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text("Le livre n'a pas été enregistré"),
|
|
behavior: SnackBarBehavior.floating,
|
|
),
|
|
);
|
|
widget.exitPopup(context);
|
|
},
|
|
),
|
|
TextButton(
|
|
child: Text("Valider"),
|
|
onPressed: () async {
|
|
if (widget.viewModel.askPrice &&
|
|
_formKey.currentState!.validate()) {
|
|
_formKey.currentState!.save();
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
var result = await widget.viewModel.sendNewBookInstance(
|
|
widget.book,
|
|
widget.viewModel.currentOwner!,
|
|
widget.viewModel.ongoingBal!,
|
|
price,
|
|
);
|
|
|
|
switch (result) {
|
|
case Ok():
|
|
if (context.mounted) {
|
|
Navigator.of(context).pop();
|
|
showDialog(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (context) =>
|
|
RegisteredBookPopup(widget: widget, price: price),
|
|
);
|
|
}
|
|
break;
|
|
case Error():
|
|
if (context.mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text("Erreur : ${result.error}")),
|
|
);
|
|
}
|
|
}
|
|
},
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
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),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|