Add: Getting items by its ids

This commit is contained in:
2024-05-23 22:12:07 +03:00
parent d6f64465b3
commit 0438a6feec
3 changed files with 34 additions and 23 deletions

View File

@@ -60,12 +60,12 @@ class _BasketPageState extends State<BasketPage> {
List<GymItem> cartItems = []; List<GymItem> cartItems = [];
double totalPrice = 0; double totalPrice = 0;
List<GymItem> gymCart = []; List<GymItem> gymCart = [];
bool _isLoading = true;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
Future.microtask(() => getCart().then((value) async { Future.microtask(() => getCart().then((value) async {
//TODO: Сделать через получение конкретных товаров через getItemsByIds
final itemIds = final itemIds =
value.map((element) => element['id'] as String).toList(); value.map((element) => element['id'] as String).toList();
final items = await getItemsByIds(context, itemIds); final items = await getItemsByIds(context, itemIds);
@@ -78,6 +78,7 @@ class _BasketPageState extends State<BasketPage> {
}).toList(); }).toList();
totalPrice = cartItems.fold( totalPrice = cartItems.fold(
0, (sum, item) => sum + item.price * item.localCount); 0, (sum, item) => sum + item.price * item.localCount);
_isLoading = false;
}); });
})); }));
} }
@@ -230,7 +231,7 @@ class _BasketPageState extends State<BasketPage> {
body: Column( body: Column(
children: [ children: [
const GymLinkHeader(title: "Корзина"), const GymLinkHeader(title: "Корзина"),
gymCart.isEmpty _isLoading
? const Expanded( ? const Expanded(
child: Center(child: CircularProgressIndicator())) child: Center(child: CircularProgressIndicator()))
: cartItems.isEmpty : cartItems.isEmpty

View File

@@ -61,12 +61,15 @@ class _OrderConfirmationPageState extends State<OrderConfirmationPage> {
List<GymItem> cartItems = []; List<GymItem> cartItems = [];
double totalPrice = 0; double totalPrice = 0;
List<GymItem> gymCart = []; List<GymItem> gymCart = [];
bool _isLoading = true;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
Future.microtask(() => getCart().then((value) async { Future.microtask(() => getCart().then((value) async {
final items = await getItems(context); final itemIds =
value.map((element) => element['id'] as String).toList();
final items = await getItemsByIds(context, itemIds);
setState(() { setState(() {
gymCart = items; gymCart = items;
cartItems = value.map((element) { cartItems = value.map((element) {
@@ -76,6 +79,7 @@ class _OrderConfirmationPageState extends State<OrderConfirmationPage> {
}).toList(); }).toList();
totalPrice = cartItems.fold( totalPrice = cartItems.fold(
0, (sum, item) => sum + item.price * item.localCount); 0, (sum, item) => sum + item.price * item.localCount);
_isLoading = false;
}); });
})); }));
} }
@@ -98,24 +102,26 @@ class _OrderConfirmationPageState extends State<OrderConfirmationPage> {
const GymLinkHeader(title: 'Оформление заказа'), const GymLinkHeader(title: 'Оформление заказа'),
const MarkdownBody(data: '## Состав заказа:'), const MarkdownBody(data: '## Состав заказа:'),
Expanded( Expanded(
child: ConstrainedBox( child: _isLoading
constraints: const BoxConstraints(maxHeight: 350), ? const Center(child: CircularProgressIndicator())
child: ListView.builder( : ConstrainedBox(
itemCount: cartItems.length, constraints: const BoxConstraints(maxHeight: 350),
itemBuilder: (context, index) { child: ListView.builder(
final item = cartItems[index]; itemCount: cartItems.length,
return OrderConfirmItemCard( itemBuilder: (context, index) {
name: item.title, final item = cartItems[index];
image: Image( return OrderConfirmItemCard(
image: NetworkImage(item.images[0].url), name: item.title,
width: 50, image: Image(
height: 50), image: NetworkImage(item.images[0].url),
price: item.price, width: 50,
count: item.localCount, height: 50),
); price: item.price,
}, count: item.localCount,
), );
), },
),
),
), ),
const SizedBox( const SizedBox(
height: 10, height: 10,

View File

@@ -37,15 +37,19 @@ Future<List<GymItem>> getItemsByIds(
BuildContext context, List<String> ids) async { BuildContext context, List<String> ids) async {
final token = context.read<GymLinkProvider>().token; final token = context.read<GymLinkProvider>().token;
if (token != '') { if (token != '') {
if (ids.isEmpty) {
return [];
}
final Uri url = final Uri url =
Uri.http('gymlink.freemyip.com:8080', 'api/product/get-products'); Uri.http('gymlink.freemyip.com:8080', 'api/product/get-products');
try { try {
final response = await http.post(url, final response = await http.post(url,
headers: { headers: {
'Authorization': 'Bearer $token', 'Authorization': 'Bearer $token',
'Content-Type': 'application/json'
}, },
body: jsonEncode(ids.toList())); body: jsonEncode({"ids": ids}));
if (response.statusCode == 200) { if (response.statusCode == 201) {
final data = final data =
jsonDecode(utf8.decode(response.bodyBytes)) as List<dynamic>; jsonDecode(utf8.decode(response.bodyBytes)) as List<dynamic>;
final items = data.map((e) => GymItem.fromJson(e)).toList(); final items = data.map((e) => GymItem.fromJson(e)).toList();