This commit is contained in:
Daniel Arantes Loverde
2026-02-15 08:18:06 -03:00
parent d7b8b1864a
commit 0f5ad4c268
35 changed files with 4090 additions and 309 deletions

View File

@@ -52,7 +52,16 @@ struct CartItemState: Identifiable {
var productId: String
var storeId: String
var name: String
var imageURL: String? = nil
var details: String? = nil
var addons: [CartItemAddonState] = []
var quantity: Int
var unitPrice: Double
}
struct CartItemAddonState: Identifiable, Hashable {
let id: String
var name: String
var quantity: Int
var unitPrice: Double
}
@@ -71,6 +80,7 @@ extension CartState {
storeName = nil
items = []
total = 0
SessionStateStore.clearCart()
}
mutating func add(item: CartItemState) {
@@ -80,6 +90,7 @@ extension CartState {
items.append(item)
}
recalculateTotal()
SessionStateStore.saveCart(self)
}
mutating func set(item: CartItemState) {
@@ -98,12 +109,18 @@ extension CartState {
storeName = nil
}
recalculateTotal()
if items.isEmpty {
SessionStateStore.clearCart()
} else {
SessionStateStore.saveCart(self)
}
}
mutating func increment(itemId: String) {
guard let index = items.firstIndex(where: { $0.id == itemId }) else { return }
items[index].quantity += 1
recalculateTotal()
SessionStateStore.saveCart(self)
}
mutating func decrement(itemId: String) {
@@ -117,5 +134,31 @@ extension CartState {
storeName = nil
}
recalculateTotal()
if items.isEmpty {
SessionStateStore.clearCart()
} else {
SessionStateStore.saveCart(self)
}
}
func toOrderItemsPayload() -> [CreateOrderItemPayload] {
items.map { item in
CreateOrderItemPayload(
productId: item.productId,
name: item.name,
qty: item.quantity,
price: item.unitPrice,
addons: item.addons
.filter { $0.quantity > 0 }
.map {
CreateOrderAddonPayload(
addonId: $0.id,
name: $0.name,
qty: $0.quantity,
price: $0.unitPrice
)
}
)
}
}
}