@@ -2,17 +2,22 @@ import 'dart:convert';
22
33import 'package:dio/dio.dart' ;
44import 'package:wooapp/api/cocart_api_client.dart' ;
5+ import 'package:wooapp/api/wp_api_client.dart' ;
6+ import 'package:wooapp/core/pair.dart' ;
57import 'package:wooapp/database/database.dart' ;
68import 'package:wooapp/locator.dart' ;
79import 'package:wooapp/model/cart_response.dart' ;
10+ import 'package:wooapp/model/variations_response.dart' ;
811
912class CartDataSourceImpl extends CartDataSource {
13+ final WpApiClient _wp = locator <WpApiClient >();
1014 final CoCartApiClient _api = locator <CoCartApiClient >();
1115 final AppDb _db = locator <AppDb >();
1216
1317 Future <Dio > _sendUserRequest () => _db.getUser ()
1418 .then ((user) => _api.withHeaders ({
15- 'Authorization' : 'Basic ${base64Encode (utf8 .encode ('${user .login }:${user .password }' ))}'
19+ 'Authorization' : 'Basic ${base64Encode (utf8 .encode ('${user .login }:${user .password }' ))}' ,
20+ 'Content-Type' : 'application/json; charset=UTF-8' ,
1621 }));
1722
1823 @override
@@ -30,8 +35,8 @@ class CartDataSourceImpl extends CartDataSource {
3035 @override
3136 Future <Response > addItem (int id, int count) => _sendUserRequest ()
3237 .then ((dio) => dio.post ('cart/add-item' , data: {
33- 'id' : id ,
34- 'quantity' : count,
38+ 'id' : '$ id ' ,
39+ 'quantity' : '$ count ' ,
3540 })).then ((response) {
3641 if (response.statusCode == 200 ) _db.addToCart (id);
3742 return response;
@@ -41,15 +46,22 @@ class CartDataSourceImpl extends CartDataSource {
4146 Future <Response > addVariableItem (
4247 int id,
4348 int count,
44- Map <String , dynamic > variation ,
49+ Map <String , String > map ,
4550 ) =>
46- _sendUserRequest ().then (
47- (dio) => dio.post (
51+ _wp.dio.get ('wp/v3/variations?id=$id ' )
52+ .then ((response) => (response.data as List ).map ((v) => Variation .fromJson (v)).toList ())
53+ .then ((variations) => mapVariations (variations, map))
54+ .then ((value) async {
55+ var dio = await _sendUserRequest ();
56+ return Pair <Dio , Map <String , String >>(dio, value);
57+ })
58+ .then (
59+ (payload) => payload.first.post (
4860 'cart/add-item' ,
4961 data: {
50- 'id' : id,
51- ' quantity' : count,
52- ' variation' : variation ,
62+ "id" : id. toString () ,
63+ " quantity" : count. toString () ,
64+ " variation" : payload.second ,
5365 },
5466 ),
5567 ).then ((response) {
@@ -91,7 +103,7 @@ abstract class CartDataSource {
91103
92104 Future <Response > addItem (int id, int count);
93105
94- Future <Response > addVariableItem (int id, int count, Map <String , dynamic > variation );
106+ Future <Response > addVariableItem (int id, int count, Map <String , String > map );
95107
96108 Future <Response > updateQuantity (String itemKey, int count);
97109
@@ -101,3 +113,18 @@ abstract class CartDataSource {
101113
102114 Future <Response > clearCart ();
103115}
116+
117+ Map <String , String > mapVariations (List <Variation > variations, Map <String , String > input) {
118+ Map <String , String > result = {};
119+ print ('DBG0 - $input ' );
120+ for (var key in input.keys) {
121+ Variation ? variation = variations.firstWhere ((v) => v.name == key, orElse: null );
122+ if (variation == null ) continue ;
123+ Term ? term = variation.terms.firstWhere ((t) => t.name == input[key], orElse: null );
124+ if (term == null ) continue ;
125+ result.addAll ({
126+ 'attribute_${variation .slug }' : '${term .slug }' ,
127+ });
128+ }
129+ return result;
130+ }
0 commit comments