add favorite
This commit is contained in:
@@ -292,6 +292,59 @@ class ProductModel extends HiveObject {
|
||||
);
|
||||
}
|
||||
|
||||
/// Create ProductModel from Wishlist API JSON
|
||||
///
|
||||
/// The wishlist API returns a simplified product structure:
|
||||
/// - name: Item code (e.g., "GIB20 G04")
|
||||
/// - item_code: Item code (duplicate of name)
|
||||
/// - item_name: Display name (e.g., "Gibellina GIB20 G04")
|
||||
/// - item_group_name: Category (e.g., "OUTDOOR [20mm]")
|
||||
/// - custom_link_360: 360 view link
|
||||
/// - thumbnail: Thumbnail image URL
|
||||
/// - price: Price (usually 0 from wishlist)
|
||||
/// - currency: Currency code
|
||||
/// - conversion_of_sm: Conversion factor
|
||||
factory ProductModel.fromWishlistApi(Map<String, dynamic> json) {
|
||||
final now = DateTime.now();
|
||||
|
||||
// Handle thumbnail URL
|
||||
String thumbnailUrl = '';
|
||||
if (json['thumbnail'] != null && (json['thumbnail'] as String).isNotEmpty) {
|
||||
final thumbnailPath = json['thumbnail'] as String;
|
||||
if (thumbnailPath.startsWith('http')) {
|
||||
thumbnailUrl = thumbnailPath;
|
||||
} else if (thumbnailPath.startsWith('/')) {
|
||||
thumbnailUrl = '${ApiConstants.baseUrl}$thumbnailPath';
|
||||
} else {
|
||||
thumbnailUrl = '${ApiConstants.baseUrl}/$thumbnailPath';
|
||||
}
|
||||
}
|
||||
|
||||
return ProductModel(
|
||||
productId: json['item_code'] as String? ?? json['name'] as String,
|
||||
name: json['item_name'] as String? ?? json['name'] as String,
|
||||
description: null, // Not provided by wishlist API
|
||||
basePrice: (json['price'] as num?)?.toDouble() ?? 0.0,
|
||||
images: null, // Not provided by wishlist API
|
||||
thumbnail: thumbnailUrl,
|
||||
imageCaptions: null,
|
||||
customLink360: json['custom_link_360'] as String?,
|
||||
specifications: null,
|
||||
category: json['item_group_name'] as String?,
|
||||
brand: null, // Not provided by wishlist API
|
||||
unit: json['currency'] as String? ?? 'm²',
|
||||
conversionOfSm: json['conversion_of_sm'] != null
|
||||
? (json['conversion_of_sm'] as num).toDouble()
|
||||
: null,
|
||||
introAttributes: null,
|
||||
isActive: true, // Assume active if in wishlist
|
||||
isFeatured: false,
|
||||
erpnextItemCode: json['item_code'] as String? ?? json['name'] as String,
|
||||
createdAt: now,
|
||||
updatedAt: null,
|
||||
);
|
||||
}
|
||||
|
||||
/// Convert ProductModel to JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
|
||||
@@ -61,7 +61,7 @@ class _ProductDetailPageState extends ConsumerState<ProductDetailPage> {
|
||||
|
||||
void _toggleFavorite() async {
|
||||
// Toggle favorite using favorites provider
|
||||
await ref.read(favoritesProvider.notifier).toggleFavorite(widget.productId);
|
||||
await ref.read(favoriteProductsProvider.notifier).toggleFavorite(widget.productId);
|
||||
|
||||
// Show feedback
|
||||
final isFavorite = ref.read(isFavoriteProvider(widget.productId));
|
||||
|
||||
Reference in New Issue
Block a user