+Add quantity field to ingredients

+Clear list now requires confirmation
+Confirm / Cancel buttons are now coloured
This commit is contained in:
Alexander Laevens
2022-12-07 02:13:34 -07:00
parent 31c4505e49
commit 4d0388b262
20 changed files with 396 additions and 83 deletions

View File

@@ -9,12 +9,10 @@ import 'package:one_trip/api/models/recipeingredient.dart';
class ShoppingList {
List<ListIngredient> ingredients;
int updates;
int homegroup;
ShoppingList({
required this.ingredients,
required this.updates,
required this.homegroup,
});
@@ -25,9 +23,7 @@ class ShoppingList {
}
return ShoppingList(
ingredients: ingredients,
updates: json["updates"] as int,
homegroup: json["homegroup"] as int);
ingredients: ingredients, homegroup: json["homegroup"] as int);
}
static Future<ShoppingList?> get(int id) async {
@@ -72,8 +68,8 @@ class ShoppingList {
bool anySuccesses = false;
for (RecipeIngredient ingredient in recipe.ingredients) {
ListIngredient? newIngredient =
await ListIngredient.create(ingredient.name, homegroup);
ListIngredient? newIngredient = await ListIngredient.create(
homegroup, ingredient.name, ingredient.quantity);
if (newIngredient != null) {
anySuccesses = true;
@@ -81,7 +77,7 @@ class ShoppingList {
}
if (anySuccesses) {
return patch(updates: updates + 1);
return get(homegroup);
}
return null;
@@ -98,7 +94,7 @@ class ShoppingList {
}
if (anySuccess) {
return patch(updates: updates + 1);
return get(homegroup);
}
return null;

View File

@@ -7,12 +7,14 @@ import 'package:http/http.dart' as http;
class ListIngredient {
int id;
String name;
String? quantity;
int list;
bool inCart;
ListIngredient({
required this.id,
required this.name,
required this.quantity,
required this.list,
required this.inCart,
});
@@ -21,21 +23,33 @@ class ListIngredient {
return ListIngredient(
id: json["id"] as int,
name: json["name"] as String,
quantity: json["quantity"] as String?,
list: json["list"] as int,
inCart: json["in_cart"] as bool,
);
}
static Future<ListIngredient?> create(String name, int list) async {
static Future<ListIngredient?> create(
int list, String name, String? quantity) async {
const String requestURL = "$baseURL/api/listingredients/";
String token = TokenSingleton().getToken();
Map<String, dynamic> body = {
"name": name,
"list": list,
};
if (quantity != null) {
body["quantity"] = quantity;
}
http.Response response = await http.post(
Uri.parse(requestURL),
headers: {"Authorization": "Token $token"},
body: {
"name": name,
"list": "$list",
headers: {
"Authorization": "Token $token",
"Content-Type": "application/json",
},
body: jsonEncode(body),
);
if (response.statusCode == 201) {
@@ -45,21 +59,27 @@ class ListIngredient {
}
}
Future<ListIngredient?> patch({String? name, bool? inCart}) async {
Future<ListIngredient?> patch(
{String? name, String? quantity, bool? inCart}) async {
String requestURL = "$baseURL/api/listingredients/$id/";
String token = TokenSingleton().getToken();
Map<String, String> body = {};
Map<String, dynamic> body = {"quantity": quantity ?? this.quantity};
if (name != null) {
body["name"] = name;
}
if (inCart != null) {
body["in_cart"] = "$inCart";
body["in_cart"] = inCart;
}
http.Response response = await http.patch(Uri.parse(requestURL),
headers: {"Authorization": "Token $token"}, body: body);
headers: {
"Authorization": "Token $token",
"Content-Type": "application/json",
},
body: jsonEncode(body));
if (response.statusCode == 200) {
return ListIngredient.fromJson(jsonDecode(response.body));
@@ -86,8 +106,9 @@ class ListIngredient {
other is ListIngredient &&
other.id == id &&
other.name == name &&
other.quantity == quantity &&
other.inCart == inCart;
@override
int get hashCode => Object.hash(id, name, inCart);
int get hashCode => Object.hash(id, name, quantity, inCart);
}

View File

@@ -7,11 +7,13 @@ import 'package:http/http.dart' as http;
class RecipeIngredient {
int id;
String name;
String? quantity;
int recipe;
RecipeIngredient({
required this.id,
required this.name,
required this.quantity,
required this.recipe,
});
@@ -19,20 +21,32 @@ class RecipeIngredient {
return RecipeIngredient(
id: json["id"] as int,
name: json["name"] as String,
quantity: json["quantity"] as String?,
recipe: json["recipe"] as int,
);
}
static Future<RecipeIngredient?> create(String name, int recipeID) async {
static Future<RecipeIngredient?> create(
int recipeID, String name, String? quantity) async {
const String requestURL = "$baseURL/api/recipeingredients/";
String token = TokenSingleton().getToken();
Map<String, dynamic> body = {
"name": name,
"recipe": recipeID,
};
if (quantity != null) {
body["quantity"] = quantity;
}
http.Response response = await http.post(
Uri.parse(requestURL),
headers: {"Authorization": "Token $token"},
body: {
"name": name,
"recipe": "$recipeID",
headers: {
"Authorization": "Token $token",
"Content-Type": "application/json",
},
body: jsonEncode(body),
);
if (response.statusCode == 201) {
@@ -42,12 +56,22 @@ class RecipeIngredient {
}
}
Future<RecipeIngredient?> patch(String name) async {
Future<RecipeIngredient?> patch({String? name, String? quantity}) async {
Map<String, dynamic> body = {"quantity": quantity};
if (name != null) {
body["name"] = name;
}
String requestURL = "$baseURL/api/recipeingredients/$id/";
String token = TokenSingleton().getToken();
http.Response response = await http.patch(Uri.parse(requestURL),
headers: {"Authorization": "Token $token"}, body: {"name": name});
headers: {
"Authorization": "Token $token",
"Content-Type": "application/json",
},
body: jsonEncode(body));
if (response.statusCode == 200) {
return RecipeIngredient.fromJson(jsonDecode(response.body));