update cart/favorite
This commit is contained in:
@@ -9,7 +9,6 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:worker/features/cart/presentation/providers/cart_data_providers.dart';
|
||||
import 'package:worker/features/cart/presentation/providers/cart_state.dart';
|
||||
import 'package:worker/features/products/domain/entities/product.dart';
|
||||
import 'package:worker/features/products/presentation/providers/products_provider.dart';
|
||||
|
||||
part 'cart_provider.g.dart';
|
||||
|
||||
@@ -46,8 +45,12 @@ class Cart extends _$Cart {
|
||||
|
||||
/// Initialize cart by loading from API
|
||||
///
|
||||
/// Call this from UI on mount to load cart items from backend.
|
||||
/// Call this ONCE from HomePage on app startup.
|
||||
/// Cart API returns product details, no need to fetch each product separately.
|
||||
Future<void> initialize() async {
|
||||
// Skip if already loaded
|
||||
if (state.items.isNotEmpty) return;
|
||||
|
||||
final repository = await ref.read(cartRepositoryProvider.future);
|
||||
|
||||
// Set loading state
|
||||
@@ -55,6 +58,7 @@ class Cart extends _$Cart {
|
||||
|
||||
try {
|
||||
// Load cart items from API (with Hive fallback)
|
||||
// Cart API returns: item_code, item_name, image, conversion_of_sm, quantity, amount
|
||||
final cartItems = await repository.getCartItems();
|
||||
|
||||
// Get member tier from user profile
|
||||
@@ -63,41 +67,47 @@ class Cart extends _$Cart {
|
||||
const memberDiscountPercent = 15.0;
|
||||
|
||||
// Convert CartItem entities to CartItemData for UI
|
||||
// Use product data from cart API directly - no need to fetch each product
|
||||
final items = <CartItemData>[];
|
||||
final selectedItems = <String, bool>{};
|
||||
|
||||
// Fetch product details for each cart item
|
||||
final productsRepository = await ref.read(productsRepositoryProvider.future);
|
||||
|
||||
for (final cartItem in cartItems) {
|
||||
try {
|
||||
// Fetch full product entity from products repository
|
||||
final product = await productsRepository.getProductById(cartItem.productId);
|
||||
// Create minimal Product from cart item data (no need to fetch from API)
|
||||
final now = DateTime.now();
|
||||
final product = Product(
|
||||
productId: cartItem.productId,
|
||||
name: cartItem.itemName ?? cartItem.productId,
|
||||
basePrice: cartItem.unitPrice,
|
||||
images: cartItem.image != null ? [cartItem.image!] : [],
|
||||
thumbnail: cartItem.image ?? '',
|
||||
imageCaptions: const {},
|
||||
specifications: const {},
|
||||
conversionOfSm: cartItem.conversionOfSm,
|
||||
erpnextItemCode: cartItem.productId,
|
||||
isActive: true,
|
||||
isFeatured: false,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
);
|
||||
|
||||
// Calculate conversion for this item
|
||||
final converted = _calculateConversion(
|
||||
cartItem.quantity,
|
||||
product.conversionOfSm,
|
||||
);
|
||||
// Calculate conversion for this item
|
||||
final converted = _calculateConversion(
|
||||
cartItem.quantity,
|
||||
product.conversionOfSm,
|
||||
);
|
||||
|
||||
// Create CartItemData with full product info
|
||||
items.add(
|
||||
CartItemData(
|
||||
product: product,
|
||||
quantity: cartItem.quantity,
|
||||
quantityConverted: converted.convertedQuantity,
|
||||
boxes: converted.boxes,
|
||||
),
|
||||
);
|
||||
// Create CartItemData with product info from cart API
|
||||
items.add(
|
||||
CartItemData(
|
||||
product: product,
|
||||
quantity: cartItem.quantity,
|
||||
quantityConverted: converted.convertedQuantity,
|
||||
boxes: converted.boxes,
|
||||
),
|
||||
);
|
||||
|
||||
// Initialize as not selected by default
|
||||
selectedItems[product.productId] = false;
|
||||
} catch (productError) {
|
||||
// Skip this item if product can't be fetched
|
||||
// In production, use a proper logging framework
|
||||
// ignore: avoid_print
|
||||
print('[CartProvider] Failed to load product ${cartItem.productId}: $productError');
|
||||
}
|
||||
// Initialize as not selected by default
|
||||
selectedItems[product.productId] = false;
|
||||
}
|
||||
|
||||
final newState = CartState(
|
||||
@@ -150,6 +160,7 @@ class Cart extends _$Cart {
|
||||
itemIds: [product.erpnextItemCode ?? product.productId],
|
||||
quantities: [quantity],
|
||||
prices: [product.basePrice],
|
||||
conversionFactors: [product.conversionOfSm],
|
||||
);
|
||||
|
||||
// Calculate conversion
|
||||
@@ -332,6 +343,7 @@ class Cart extends _$Cart {
|
||||
itemId: item.product.erpnextItemCode ?? productId,
|
||||
quantity: quantity,
|
||||
price: item.product.basePrice,
|
||||
conversionFactor: item.product.conversionOfSm,
|
||||
);
|
||||
} catch (e) {
|
||||
// Silent fail - keep local state, user can retry later
|
||||
@@ -370,6 +382,7 @@ class Cart extends _$Cart {
|
||||
itemId: item.product.erpnextItemCode ?? productId,
|
||||
quantity: newQuantity,
|
||||
price: item.product.basePrice,
|
||||
conversionFactor: item.product.conversionOfSm,
|
||||
);
|
||||
|
||||
// Update local state
|
||||
|
||||
Reference in New Issue
Block a user