This commit is contained in:
JMARyA 2024-09-05 09:09:51 +02:00
parent bcef1d2e48
commit 3da7878555
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 201 additions and 7 deletions

View file

@ -28,6 +28,16 @@ class API {
return lst as List<dynamic>;
}
Future<List<dynamic>> getUniqueField(
String item, String variant, String field) async {
return jsonDecode(await getRequest(
"$instance/item/$item/$variant/unique?field=$field")) as List<dynamic>;
}
Future<Map<String, dynamic>> getLocations() async {
return jsonDecode(await getRequest("$instance/locations"));
}
Future<Item> 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<String> supplyItem(
String item, String variant, String price, String? origin) async {
Future<String> 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"];
}

View file

@ -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<SupplyPage> {
String? variant;
late String variant;
final _formKey = GlobalKey<FormState>();
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<String>(
hint: Text('Select Variant'),
value: variant,
onChanged: (value) {
setState(() {
variant = value!;
});
},
items: widget.item.variants.entries
.map<DropdownMenuItem<String>>((variant) {
return DropdownMenuItem<String>(
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<String>(
value: _selectedOrigin,
hint: Text('Select or Enter Origin'),
onChanged: (value) {
setState(() {
_selectedOrigin = value ?? "";
if (!_price.isEmpty) {
// todo : update price from latest
}
});
},
items: origins
.map<DropdownMenuItem<String>>(
(origin) => DropdownMenuItem<String>(
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<String>(
hint: Text('Select Location'),
value: _selectedLocation,
onChanged: (value) {
setState(() {
_selectedLocation = value!;
});
},
items: locations.map<DropdownMenuItem<String>>((location) {
return DropdownMenuItem<String>(
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'),
),
],
),
),
);
}),
);
}
}