feat: start working on sell

This commit is contained in:
alzalia1 2025-08-14 13:42:20 +02:00
parent f378c8c0be
commit 1803a2b119
8 changed files with 94 additions and 31 deletions

View file

@ -16,6 +16,22 @@ class OwnerRepository {
final WebsocketClient _wsClient; final WebsocketClient _wsClient;
late final StreamSubscription sub; late final StreamSubscription sub;
List<Owner>? _cachedOwners; List<Owner>? _cachedOwners;
Owner? _sectionOwner;
Future<Result<Owner>> get sectionOwner async {
if (_sectionOwner != null) {
return Result.ok(_sectionOwner!);
}
final result = await _apiClient.getSectionOwner();
switch (result) {
case Ok():
_sectionOwner = result.value;
break;
default:
break;
}
return result;
}
/// Adds an [Owner] to the database, and gets the resulting [Owner]. /// Adds an [Owner] to the database, and gets the resulting [Owner].
Future<Result<Owner>> addOwner( Future<Result<Owner>> addOwner(

View file

@ -304,6 +304,27 @@ class ApiClient {
* ==================== * ====================
*/ */
Future<Result<Owner>> getSectionOwner() async {
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(
Uri.parse("https://$apiBasePath/owner/self"),
headers: headers,
);
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
return Result.ok(Owner.fromJSON(json));
} else {
throw "Unknown error";
}
} catch (e) {
return Result.error(Exception(e));
} finally {
client.close();
}
}
/// Call on `/owners` to get a list of all [Owner]s /// Call on `/owners` to get a list of all [Owner]s
Future<Result<List<Owner>>> getOwners() async { Future<Result<List<Owner>>> getOwners() async {
final client = Client(); final client = Client();

View file

@ -8,6 +8,7 @@ abstract final class Routes {
// ==[ SELL ]== // ==[ SELL ]==
static const sell = '/sell'; static const sell = '/sell';
static const sellScan = '/sell/scan';
// ==[ AUTH ]== // ==[ AUTH ]==
static const login = '/login'; static const login = '/login';

View file

@ -45,6 +45,9 @@ class AddViewModel extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
Owner? _sectionOwner;
Owner? get sectionOwner => _sectionOwner;
List<Owner> _owners = []; List<Owner> _owners = [];
List<Owner>? get owners => _owners; List<Owner>? get owners => _owners;
Future<Result<Owner>> addOwner( Future<Result<Owner>> addOwner(
@ -176,9 +179,17 @@ class AddViewModel extends ChangeNotifier {
), ),
); );
case Error(): case Error():
break;
}
return result; return result;
} }
final result2 = await _ownerRepository.sectionOwner;
switch (result2) {
case Ok():
_sectionOwner = result2.value;
break;
default:
}
return result2;
}
} }

View file

@ -129,15 +129,17 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
case Ok(): case Ok():
if (context.mounted) { if (context.mounted) {
Navigator.of(context).pop(); Navigator.of(context).pop();
}
showDialog( showDialog(
context: context, context: context,
builder: (context) => AlertDialog( builder: (context) => AlertDialog(
title: Text( title: Text(
"ID : ${widget.viewModel.currentOwner!.firstName[0]}${widget.viewModel.currentOwner!.lastName[0]}${(price == 0) ? "PL" : price.toString()}", "ID : ${widget.viewModel.currentOwner!.firstName[0].toUpperCase()}${widget.viewModel.currentOwner!.lastName[0].toUpperCase()}${(price == 0) ? "PL" : price.toString()}",
), ),
content: Text( content: Text(
"Identifiant propriétaire de ce livre. Pensez à l'écrire pour retrouver lae propriétaire du livre lors de la vente ou du retour !", (widget.viewModel.currentOwner!.id ==
widget.viewModel.sectionOwner!.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: [ actions: [
TextButton( TextButton(
@ -145,7 +147,9 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
widget.exitPopup(context); widget.exitPopup(context);
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text("Enregistré: ${widget.book.title}"), content: Text(
"Enregistré: ${widget.book.title}",
),
behavior: SnackBarBehavior.floating, behavior: SnackBarBehavior.floating,
), ),
); );
@ -155,6 +159,7 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
], ],
), ),
); );
}
break; break;
case Error(): case Error():
if (context.mounted) { if (context.mounted) {

View file

@ -1,10 +1,16 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:seshat/ui/sell_page/view_model/sell_view_model.dart'; import 'package:seshat/ui/sell_page/view_model/sell_view_model.dart';
class ManualScanPopup extends StatelessWidget { class ManualScanPopup extends StatelessWidget {
ManualScanPopup({required this.viewModel}); const ManualScanPopup({
super.key,
required this.viewModel,
required this.controller,
});
final SellViewModel viewModel; final SellViewModel viewModel;
final MobileScannerController controller;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -29,6 +35,7 @@ class ManualScanPopup extends StatelessWidget {
actions: [ actions: [
TextButton( TextButton(
onPressed: () { onPressed: () {
controller.start();
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
child: Text("Annuler"), child: Text("Annuler"),

View file

@ -61,11 +61,14 @@ class _ScanScreenState extends State<ScanScreen> {
backgroundColor: WidgetStatePropertyAll(theme.cardColor), backgroundColor: WidgetStatePropertyAll(theme.cardColor),
), ),
onPressed: () { onPressed: () {
controller.stop();
showDialog( showDialog(
context: context, context: context,
barrierDismissible: false, barrierDismissible: false,
builder: (context) => builder: (context) => ManualScanPopup(
ManualScanPopup(viewModel: widget.viewModel), viewModel: widget.viewModel,
controller: controller,
),
); );
}, },
child: Text("Vendre un livre sans scanner"), child: Text("Vendre un livre sans scanner"),

View file

@ -154,7 +154,6 @@ class _SellPageState extends State<SellPage> {
], ],
), ),
), ),
(widget.viewModel.showScan) (widget.viewModel.showScan)
? ScanScreen(viewModel: widget.viewModel) ? ScanScreen(viewModel: widget.viewModel)
: SizedBox(), : SizedBox(),