261 lines
7.5 KiB
Dart
261 lines
7.5 KiB
Dart
import 'package:cheminova/constants/constant.dart';
|
|
import 'package:cheminova/models/product_model.dart';
|
|
import 'package:cheminova/services/api_client.dart';
|
|
import 'package:cheminova/services/api_urls.dart';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class ProductProvider extends ChangeNotifier {
|
|
ProductProvider() {
|
|
getProducts(1);
|
|
}
|
|
|
|
final _apiClient = ApiClient();
|
|
ProductResponse? productResponse;
|
|
List<Product> productList = [];
|
|
final List<Product> _selectedProducts = [];
|
|
|
|
bool _isLoading = false;
|
|
|
|
bool get isLoading => _isLoading;
|
|
|
|
List<Product> get selectedProducts => _selectedProducts;
|
|
|
|
void setLoading(bool loading) {
|
|
_isLoading = loading;
|
|
notifyListeners();
|
|
}
|
|
|
|
// Future<List<Product>?> getProducts(int page) async {
|
|
// setLoading(true);
|
|
// try {
|
|
// Response response = await _apiClient.get("${ApiUrls.getProducts}?page=$page");
|
|
// setLoading(false);
|
|
// if (response.statusCode == 200) {
|
|
// productResponse = ProductResponse.fromJson(response.data);
|
|
// productList = productResponse!.products;
|
|
// //notifyListeners();
|
|
// }
|
|
// } catch (e) {
|
|
// setLoading(false);
|
|
// String error = "Something went wrong";
|
|
// if (e is DioException) {
|
|
// error = e.response!.data['message'] ?? "Something went wrong";
|
|
// }
|
|
// ScaffoldMessenger.of(
|
|
// navigatorKey.currentContext!,
|
|
// ).showSnackBar(
|
|
// SnackBar(
|
|
// content: Text(
|
|
// error,
|
|
// ),
|
|
// ),
|
|
// );
|
|
// }
|
|
// }
|
|
|
|
|
|
Future<void> getProducts(int page) async {
|
|
// if (_isLoading) return; // Prevent duplicate requests
|
|
// setLoading(true);
|
|
try {
|
|
Response response = await _apiClient.get("${ApiUrls.getProducts}?page=$page");
|
|
if (response.statusCode == 200) {
|
|
final newProductResponse = ProductResponse.fromJson(response.data);
|
|
if (newProductResponse.products.isNotEmpty) {
|
|
// if (page == 6) {
|
|
// // Clear the list if it's the first page
|
|
// productList = newProductResponse.products;
|
|
// } else {
|
|
// Append new products for subsequent pages
|
|
productList.addAll(newProductResponse.products);
|
|
}
|
|
// _isLoading = false;
|
|
// notifyListeners();
|
|
// }
|
|
}
|
|
} catch (e) {
|
|
String error = "Something went wrong";
|
|
if (e is DioException) {
|
|
error = e.response?.data['message'] ?? "Something went wrong";
|
|
}
|
|
ScaffoldMessenger.of(navigatorKey.currentContext!).showSnackBar(
|
|
SnackBar(
|
|
content: Text(error),
|
|
),
|
|
);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Future<bool> submitSelectedProducts(
|
|
String addedFor, String addedForId) async {
|
|
setLoading(true);
|
|
try {
|
|
final data = {
|
|
"products": selectedProducts.map((product) {
|
|
return {
|
|
"SKU": product.SKU,
|
|
"ProductName": product.name,
|
|
"Sale": product.sale,
|
|
"Inventory": product.inventory,
|
|
"productid": product.id,
|
|
};
|
|
}).toList(),
|
|
"addedFor": addedFor,
|
|
"addedForId": addedForId,
|
|
};
|
|
|
|
Response response =
|
|
await _apiClient.post(ApiUrls.submitProducts, data: data);
|
|
setLoading(false);
|
|
if (response.statusCode == 201) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (e) {
|
|
setLoading(false);
|
|
String error = "Something went wrong";
|
|
if (e is DioException) {
|
|
error = e.response!.data['message'] ?? "Something went wrong";
|
|
}
|
|
ScaffoldMessenger.of(
|
|
navigatorKey.currentContext!,
|
|
).showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
error,
|
|
),
|
|
),
|
|
);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// import 'package:cheminova/constants/constant.dart';
|
|
// import 'package:cheminova/models/product_model.dart';
|
|
// import 'package:cheminova/services/api_client.dart';
|
|
// import 'package:cheminova/services/api_urls.dart';
|
|
// import 'package:dio/dio.dart';
|
|
// import 'package:flutter/material.dart';
|
|
//
|
|
// class ProductProvider extends ChangeNotifier {
|
|
// ProductProvider() {
|
|
// // Initialize with the first page of products
|
|
// getProducts(1);
|
|
// }
|
|
//
|
|
// final _apiClient = ApiClient();
|
|
// ProductResponse? productResponse; // To parse and store API response
|
|
// List<Product> productList = []; // Main list of products
|
|
// final List<Product> _selectedProducts = []; // For user-selected products
|
|
//
|
|
// bool _isLoading = false;
|
|
// bool _hasMoreData = true; // Track if more data is available
|
|
//
|
|
// bool get isLoading => _isLoading;
|
|
// bool get hasMoreData => _hasMoreData;
|
|
//
|
|
// List<Product> get selectedProducts => _selectedProducts;
|
|
//
|
|
// void setLoading(bool loading) {
|
|
// _isLoading = loading;
|
|
// notifyListeners();
|
|
// }
|
|
//
|
|
// Future<void> getProducts(int page) async {
|
|
// if (_isLoading || !_hasMoreData) return; // Prevent duplicate requests
|
|
// setLoading(true);
|
|
//
|
|
// try {
|
|
// final String url = "${ApiUrls.getProducts}?page=$page";
|
|
// Response response = await _apiClient.get(url);
|
|
//
|
|
// if (response.statusCode == 200) {
|
|
// productResponse = ProductResponse.fromJson(response.data);
|
|
//
|
|
// if (productResponse!.products.isNotEmpty) {
|
|
//
|
|
// selectedProducts.addAll(productResponse!.products);
|
|
// // if (page <= productResponse!.totalPages) {
|
|
// // // Replace the list if fetching the first page
|
|
// // productList = productResponse!.products;
|
|
// // } else {
|
|
// // // Append new products for subsequent pages
|
|
// // productList.addAll(productResponse!.products);
|
|
// // }
|
|
// notifyListeners();
|
|
// } else {
|
|
// // No more products to load
|
|
// _hasMoreData = false;
|
|
// }
|
|
// }
|
|
// } catch (e) {
|
|
// String error = "Something went wrong";
|
|
// if (e is DioException) {
|
|
// error = e.response?.data['message'] ?? "Something went wrong";
|
|
// }
|
|
// ScaffoldMessenger.of(
|
|
// navigatorKey.currentContext!,
|
|
// ).showSnackBar(
|
|
// SnackBar(
|
|
// content: Text(error),
|
|
// ),
|
|
// );
|
|
// } finally {
|
|
// setLoading(false);
|
|
// }
|
|
// }
|
|
//
|
|
// Future<bool> submitSelectedProducts(String addedFor, String addedForId) async {
|
|
// setLoading(true);
|
|
//
|
|
// try {
|
|
// final data = {
|
|
// "products": selectedProducts.map((product) {
|
|
// return {
|
|
// "SKU": product.SKU,
|
|
// "ProductName": product.name,
|
|
// "Sale": product.sale,
|
|
// "Inventory": product.inventory,
|
|
// "productid": product.id,
|
|
// };
|
|
// }).toList(),
|
|
// "addedFor": addedFor,
|
|
// "addedForId": addedForId,
|
|
// };
|
|
//
|
|
// Response response =
|
|
// await _apiClient.post(ApiUrls.submitProducts, data: data);
|
|
//
|
|
// if (response.statusCode == 201) {
|
|
// return true;
|
|
// } else {
|
|
// return false;
|
|
// }
|
|
// } catch (e) {
|
|
// String error = "Something went wrong";
|
|
// if (e is DioException) {
|
|
// error = e.response?.data['message'] ?? "Something went wrong";
|
|
// }
|
|
// ScaffoldMessenger.of(
|
|
// navigatorKey.currentContext!,
|
|
// ).showSnackBar(
|
|
// SnackBar(
|
|
// content: Text(error),
|
|
// ),
|
|
// );
|
|
// return false;
|
|
// } finally {
|
|
// setLoading(false);
|
|
// }
|
|
// }
|
|
// }
|