From 3da7878555090ace4ef0725ea117202334feba53 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Thu, 5 Sep 2024 09:09:51 +0200 Subject: [PATCH] update --- lib/api.dart | 25 ++++++- lib/itemview.dart | 183 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 201 insertions(+), 7 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index d1d74ad..a3adacf 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -28,6 +28,16 @@ class API { return lst as List; } + Future> getUniqueField( + String item, String variant, String field) async { + return jsonDecode(await getRequest( + "$instance/item/$item/$variant/unique?field=$field")) as List; + } + + Future> getLocations() async { + return jsonDecode(await getRequest("$instance/locations")); + } + Future getItem(String item) async { return Item(jsonDecode(await getRequest("$instance/item/$item"))); } @@ -41,13 +51,22 @@ class API { return jsonDecode(await getRequest("$instance/item/$item/inventory")); } - Future supplyItem( - String item, String variant, String price, String? origin) async { + Future supplyItem(String item, String variant, String price, + String? origin, String? location) async { + if (origin!.isEmpty) { + origin = null; + } + + if (location!.isEmpty) { + location = null; + } + return jsonDecode(await postRequest("$instance/supply", { "item": item, "variant": variant, "price": price, - "origin": origin + "origin": origin, + "location": location }))["uuid"]; } diff --git a/lib/itemview.dart b/lib/itemview.dart index 6f5a802..e96f46f 100644 --- a/lib/itemview.dart +++ b/lib/itemview.dart @@ -1,6 +1,5 @@ import 'package:cdb_ui/api.dart'; import 'package:flutter/material.dart'; -import 'api.dart'; class ItemView extends StatelessWidget { Item item; @@ -9,7 +8,23 @@ class ItemView extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( body: Column(children: [ - Text(item.category), + Row( + children: [ + Align( + alignment: Alignment.centerLeft, + child: Placeholder(), + ), // todo + Column( + children: [ + Text( + item.name, + style: TextStyle(fontWeight: FontWeight.bold), + ), + Text(item.category) + ], + ) + ], + ), SizedBox(height: 10), FutureBuilder( future: API().getInventory(item.id), @@ -56,14 +71,174 @@ class SupplyPage extends StatefulWidget { } class _SupplyPageState extends State { - String? variant; + late String variant; + + final _formKey = GlobalKey(); + + String _selectedOrigin = ""; + String _selectedLocation = ""; + String _price = ""; + + @override + void initState() { + super.initState(); + variant = widget.item.variants.keys.first; + } void _supply() async {} @override Widget build(BuildContext context) { return Scaffold( - body: Text("Add item ${widget.item.name}"), + appBar: AppBar( + title: Text('Add New Item'), + ), + body: FutureBuilder(future: () async { + return ( + await API().getLocations(), + await API().getUniqueField(widget.item.id, variant!, "origin") + ); + }(), builder: (context, snap) { + if (!snap.hasData) { + return CircularProgressIndicator(); + } + + var (location_map, origins) = snap.data!; + + // todo : fix locations + var locations = []; + + return Padding( + padding: const EdgeInsets.all(16.0), + child: Form( + key: _formKey, + child: Column( + children: [ + // Variant selection + DropdownButtonFormField( + hint: Text('Select Variant'), + value: variant, + onChanged: (value) { + setState(() { + variant = value!; + }); + }, + items: widget.item.variants.entries + .map>((variant) { + return DropdownMenuItem( + value: variant.key, + child: Text(variant.value.name), + ); + }).toList(), + validator: (value) { + if (value == null) { + return 'Please select a variant'; + } + return null; + }, + onSaved: (value) { + variant = value!; + }, + ), + + // Origin Field with Dropdown and Text Input + DropdownButtonFormField( + value: _selectedOrigin, + hint: Text('Select or Enter Origin'), + onChanged: (value) { + setState(() { + _selectedOrigin = value ?? ""; + + if (!_price.isEmpty) { + // todo : update price from latest + } + }); + }, + items: origins + .map>( + (origin) => DropdownMenuItem( + value: origin, + child: Text(origin), + )) + .toList(), + ), + TextFormField( + decoration: InputDecoration(labelText: 'Enter New Origin'), + onChanged: (value) { + setState(() { + _selectedOrigin = ""; // Clear dropdown selection + }); + }, + ), + + // Price Field + TextFormField( + decoration: InputDecoration(labelText: 'Price'), + keyboardType: TextInputType.number, + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter a price'; + } + if (double.tryParse(value) == null) { + return 'Please enter a valid number'; + } + return null; + }, + onSaved: (value) { + _price = value!; + }, + ), + + // Location Dropdown + DropdownButtonFormField( + hint: Text('Select Location'), + value: _selectedLocation, + onChanged: (value) { + setState(() { + _selectedLocation = value!; + }); + }, + items: locations.map>((location) { + return DropdownMenuItem( + value: location, + child: Text(location), + ); + }).toList(), + validator: (value) { + if (value == null) { + return 'Please select a location'; + } + return null; + }, + onSaved: (value) { + _selectedLocation = value!; + }, + ), + + SizedBox(height: 20), + + // Submit Button + ElevatedButton( + onPressed: () { + if (_formKey.currentState!.validate()) { + _formKey.currentState!.save(); + + API().supplyItem(widget.item.name, variant!, _price, + _selectedOrigin, _selectedLocation); + + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('Item added successfully!')), + ); + Navigator.of(context).pop(); + } + }, + child: Text('Add Item'), + ), + ], + ), + ), + ); + }), ); } }