addio-ecomm-sdk 1.3.44 → 1.3.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +34 -34
- package/README.md +79 -79
- package/dist/index.d.ts +202 -202
- package/dist/index.js +1817 -1812
- package/dist/index.js.map +1 -1
- package/dist/lib/constants/components/CreditCardInput.d.ts +11 -11
- package/dist/lib/constants/components/CreditCardInput.js +79 -79
- package/dist/lib/constants/components/Snackbar.d.ts +11 -11
- package/dist/lib/constants/components/Snackbar.js +39 -39
- package/dist/lib/constants/contexts/CartContext/CartContextProvider.d.ts +7 -7
- package/dist/lib/constants/contexts/CartContext/CartContextProvider.js +981 -981
- package/dist/lib/constants/contexts/CartContext/CartContextProvider.js.map +1 -1
- package/dist/lib/constants/contexts/CartContext/index.d.ts +79 -79
- package/dist/lib/constants/contexts/CartContext/index.js +127 -127
- package/dist/lib/constants/contexts/DataContext/DataContextProvider.d.ts +15 -15
- package/dist/lib/constants/contexts/DataContext/DataContextProvider.js +299 -299
- package/dist/lib/constants/contexts/DataContext/index.d.ts +40 -40
- package/dist/lib/constants/contexts/DataContext/index.js +66 -66
- package/dist/lib/constants/contexts/LogContext/LogContextProvider.d.ts +6 -6
- package/dist/lib/constants/contexts/LogContext/LogContextProvider.js +103 -103
- package/dist/lib/constants/contexts/LogContext/index.d.ts +7 -7
- package/dist/lib/constants/contexts/LogContext/index.js +45 -45
- package/dist/lib/constants/contexts/ModalContext/ModalContextProvider.d.ts +7 -7
- package/dist/lib/constants/contexts/ModalContext/ModalContextProvider.js +76 -76
- package/dist/lib/constants/contexts/ModalContext/index.d.ts +14 -14
- package/dist/lib/constants/contexts/ModalContext/index.js +18 -18
- package/dist/lib/constants/contexts/SDKContext/SDKProvider.d.ts +8 -8
- package/dist/lib/constants/contexts/SDKContext/SDKProvider.js +79 -79
- package/dist/lib/constants/contexts/SDKContext/index.d.ts +4 -4
- package/dist/lib/constants/contexts/SDKContext/index.js +4 -4
- package/dist/lib/constants/contexts/SnackbarContext/SnackbarContext.d.ts +4 -4
- package/dist/lib/constants/contexts/SnackbarContext/SnackbarContext.js +6 -6
- package/dist/lib/constants/contexts/SnackbarContext/SnackbarContextProvider.d.ts +12 -12
- package/dist/lib/constants/contexts/SnackbarContext/SnackbarContextProvider.js +49 -49
- package/dist/lib/constants/contexts/SnackbarContext/interfaces/ISnackbarContext.d.ts +17 -17
- package/dist/lib/constants/contexts/SnackbarContext/interfaces/ISnackbarContext.js +21 -21
- package/dist/lib/constants/contexts/StockNotifContext/StockNotifContextProvider.d.ts +7 -7
- package/dist/lib/constants/contexts/StockNotifContext/StockNotifContextProvider.js +179 -179
- package/dist/lib/constants/contexts/StockNotifContext/index.d.ts +10 -10
- package/dist/lib/constants/contexts/StockNotifContext/index.js +10 -10
- package/dist/lib/constants/contexts/UserContext/UserContextProvider.d.ts +5 -5
- package/dist/lib/constants/contexts/UserContext/UserContextProvider.js +470 -470
- package/dist/lib/constants/contexts/UserContext/index.d.ts +54 -54
- package/dist/lib/constants/contexts/UserContext/index.js +82 -82
- package/dist/lib/constants/contexts/WishlistContext/WishlistContextProvider.d.ts +6 -6
- package/dist/lib/constants/contexts/WishlistContext/WishlistContextProvider.js +327 -327
- package/dist/lib/constants/contexts/WishlistContext/index.d.ts +12 -12
- package/dist/lib/constants/contexts/WishlistContext/index.js +12 -12
- package/dist/lib/constants/hooks/useCart.d.ts +2 -2
- package/dist/lib/constants/hooks/useCart.js +11 -11
- package/dist/lib/constants/hooks/useData.d.ts +2 -2
- package/dist/lib/constants/hooks/useData.js +11 -11
- package/dist/lib/constants/hooks/useLogs.d.ts +2 -2
- package/dist/lib/constants/hooks/useLogs.js +11 -11
- package/dist/lib/constants/hooks/useModal.d.ts +2 -2
- package/dist/lib/constants/hooks/useModal.js +11 -11
- package/dist/lib/constants/hooks/useSDK.d.ts +2 -2
- package/dist/lib/constants/hooks/useSDK.js +11 -11
- package/dist/lib/constants/hooks/useSnackbar.d.ts +2 -2
- package/dist/lib/constants/hooks/useSnackbar.js +11 -11
- package/dist/lib/constants/hooks/useStockNotif.d.ts +2 -2
- package/dist/lib/constants/hooks/useStockNotif.js +11 -11
- package/dist/lib/constants/hooks/useUser.d.ts +2 -2
- package/dist/lib/constants/hooks/useUser.js +11 -11
- package/dist/lib/constants/hooks/useWishlist.d.ts +2 -2
- package/dist/lib/constants/hooks/useWishlist.js +11 -11
- package/dist/lib/models/image/index.d.ts +6 -6
- package/dist/lib/models/image/index.js +15 -15
- package/dist/lib/models/stockNotification/index.d.ts +13 -13
- package/dist/lib/models/stockNotification/index.js +130 -130
- package/dist/lib/models/wishlist/index.d.ts +14 -14
- package/dist/lib/models/wishlist/index.js +155 -155
- package/dist/utils/StripeErrors.d.ts +24 -24
- package/dist/utils/StripeErrors.js +55 -55
- package/dist/utils/algolia.d.ts +3 -3
- package/dist/utils/algolia.js +81 -81
- package/dist/utils/analytics.d.ts +71 -71
- package/dist/utils/analytics.js +257 -257
- package/dist/utils/cart.d.ts +3 -3
- package/dist/utils/cart.js +66 -66
- package/dist/utils/contentful.d.ts +10 -10
- package/dist/utils/contentful.js +136 -136
- package/dist/utils/functions.d.ts +21 -21
- package/dist/utils/functions.js +195 -195
- package/dist/utils/getSDK.d.ts +4 -4
- package/dist/utils/getSDK.js +77 -77
- package/dist/utils/initStoreData.d.ts +3 -3
- package/dist/utils/initStoreData.js +62 -62
- package/dist/utils/interfaces/IClientCMSData.d.ts +132 -132
- package/dist/utils/interfaces/IClientCMSData.js +2 -2
- package/dist/utils/interfaces/IEndpointResult.d.ts +10 -10
- package/dist/utils/interfaces/IEndpointResult.js +2 -2
- package/dist/utils/interfaces/IFacebookUserProfile.d.ts +10 -10
- package/dist/utils/interfaces/IFacebookUserProfile.js +2 -2
- package/dist/utils/interfaces/IFilter.d.ts +23 -23
- package/dist/utils/interfaces/IFilter.js +2 -2
- package/dist/utils/interfaces/IGoogleUserProfile.d.ts +7 -7
- package/dist/utils/interfaces/IGoogleUserProfile.js +2 -2
- package/dist/utils/interfaces/ISDKConfig.d.ts +7 -7
- package/dist/utils/interfaces/ISDKConfig.js +2 -2
- package/dist/utils/interfaces/ISpaceData.d.ts +14 -14
- package/dist/utils/interfaces/ISpaceData.js +2 -2
- package/dist/utils/larivieremeunier.d.ts +3 -3
- package/dist/utils/larivieremeunier.js +156 -156
- package/dist/utils/technosupply.d.ts +3 -3
- package/dist/utils/technosupply.js +155 -155
- package/package.json +60 -60
- package/publish.sh +125 -125
|
@@ -1,982 +1,982 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
-
};
|
|
52
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
-
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
54
|
-
var react_1 = require("react");
|
|
55
|
-
var Cart_1 = require("addio-admin-sdk/dist/Interfaces/Cart");
|
|
56
|
-
var ISnackbarContext_1 = require("../SnackbarContext/interfaces/ISnackbarContext");
|
|
57
|
-
var _1 = __importDefault(require("."));
|
|
58
|
-
var useSnackbar_1 = __importDefault(require("../../hooks/useSnackbar"));
|
|
59
|
-
var useSDK_1 = __importDefault(require("../../hooks/useSDK"));
|
|
60
|
-
var useData_1 = __importDefault(require("../../hooks/useData"));
|
|
61
|
-
var useUser_1 = __importDefault(require("../../hooks/useUser"));
|
|
62
|
-
var functions_1 = require("../../../../utils/functions");
|
|
63
|
-
var functions_2 = require("../../../../utils/functions");
|
|
64
|
-
var cart_1 = require("addio-admin-sdk/dist/utils/cart");
|
|
65
|
-
var discount_1 = require("addio-admin-sdk/dist/utils/discount");
|
|
66
|
-
var g2_1 = require("addio-admin-sdk/dist/rules/GR/utils/g2");
|
|
67
|
-
var CartContextProvider = function (_a) {
|
|
68
|
-
var locale = _a.locale, children = _a.children;
|
|
69
|
-
var sdk = (0, useSDK_1.default)();
|
|
70
|
-
var currency = (0, useData_1.default)().currency;
|
|
71
|
-
var _b = (0, useUser_1.default)(), isUserLoggedIn = _b.isUserLoggedIn, userLoaded = _b.loaded;
|
|
72
|
-
var setSnackbar = (0, useSnackbar_1.default)().setSnackbar;
|
|
73
|
-
var _c = (0, react_1.useState)(), cartData = _c[0], setCartData = _c[1];
|
|
74
|
-
var _d = (0, react_1.useState)([]), cartErrors = _d[0], setCartErrors = _d[1];
|
|
75
|
-
var _e = (0, react_1.useState)(false), loading = _e[0], setLoading = _e[1];
|
|
76
|
-
var _f = (0, react_1.useState)(false), cartSliderOpen = _f[0], setCartSliderOpen = _f[1];
|
|
77
|
-
var _g = (0, react_1.useState)(), isGuest = _g[0], setIsGuest = _g[1];
|
|
78
|
-
var api = !!sdk ? sdk.getAPIInfos() : null;
|
|
79
|
-
var initCart = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
80
|
-
var cartIDInLocalStorage, newCart, _2;
|
|
81
|
-
return __generator(this, function (_a) {
|
|
82
|
-
switch (_a.label) {
|
|
83
|
-
case 0:
|
|
84
|
-
if (!!!api)
|
|
85
|
-
return [2];
|
|
86
|
-
cartIDInLocalStorage = localStorage.getItem("".concat(api.org, "__cart"));
|
|
87
|
-
if (!!!cartIDInLocalStorage) return [3, 4];
|
|
88
|
-
_a.label = 1;
|
|
89
|
-
case 1:
|
|
90
|
-
_a.trys.push([1, 3, , 4]);
|
|
91
|
-
return [4, getCartFromDatabase(cartIDInLocalStorage)];
|
|
92
|
-
case 2:
|
|
93
|
-
newCart = _a.sent();
|
|
94
|
-
if (!!newCart && !!!newCart.clerk_id) {
|
|
95
|
-
if (newCart.status < Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS) {
|
|
96
|
-
return [2, newCart];
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
localStorage.removeItem("".concat(api.org, "__cart"));
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return [3, 4];
|
|
103
|
-
case 3:
|
|
104
|
-
_2 = _a.sent();
|
|
105
|
-
return [2, undefined];
|
|
106
|
-
case 4: return [2, undefined];
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
}); };
|
|
110
|
-
var updateEmail = function (newCart, email, saveToDatabase) {
|
|
111
|
-
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
112
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
113
|
-
var cart;
|
|
114
|
-
return __generator(this, function (_a) {
|
|
115
|
-
switch (_a.label) {
|
|
116
|
-
case 0:
|
|
117
|
-
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
118
|
-
if (!!!saveToDatabase) return [3, 2];
|
|
119
|
-
return [4, saveCart({ user_email: email })];
|
|
120
|
-
case 1:
|
|
121
|
-
cart = _a.sent();
|
|
122
|
-
return [3, 3];
|
|
123
|
-
case 2:
|
|
124
|
-
if (!!cart) {
|
|
125
|
-
cart = __assign(__assign({}, cart), { user_email: email });
|
|
126
|
-
}
|
|
127
|
-
_a.label = 3;
|
|
128
|
-
case 3: return [2, cart];
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
};
|
|
133
|
-
var updateDefaultCustomer = function (newCart, email, id, pro_info, saveToDatabase) {
|
|
134
|
-
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
135
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
136
|
-
var cart, newCustomerInfo;
|
|
137
|
-
return __generator(this, function (_a) {
|
|
138
|
-
switch (_a.label) {
|
|
139
|
-
case 0:
|
|
140
|
-
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
141
|
-
newCustomerInfo = {
|
|
142
|
-
customer_id: id,
|
|
143
|
-
user_email: email,
|
|
144
|
-
customer_info: __assign(__assign(__assign(__assign(__assign(__assign({ customer_default_email: email }, (!!pro_info && pro_info.pro_proof_doc ? { pro_proof_doc: pro_info.pro_proof_doc } : {})), (!!pro_info && pro_info.pro_title ? { pro_title: pro_info.pro_title } : {})), (!!pro_info && pro_info.customer_external_code
|
|
145
|
-
? { customer_external_code: pro_info.customer_external_code }
|
|
146
|
-
: {})), (!!pro_info && pro_info.customer_external_group
|
|
147
|
-
? { customer_external_group: pro_info.customer_external_group }
|
|
148
|
-
: {})), (!!pro_info && pro_info.terms_code ? { terms_code: pro_info.terms_code } : {})), (!!pro_info && pro_info.company_name ? { company_name: pro_info.company_name } : {}))
|
|
149
|
-
};
|
|
150
|
-
if (!!!saveToDatabase) return [3, 2];
|
|
151
|
-
return [4, saveCart(newCustomerInfo)];
|
|
152
|
-
case 1:
|
|
153
|
-
_a.sent();
|
|
154
|
-
return [3, 3];
|
|
155
|
-
case 2:
|
|
156
|
-
cart = __assign(__assign({}, (!!cart ? cart : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB))), newCustomerInfo);
|
|
157
|
-
_a.label = 3;
|
|
158
|
-
case 3: return [2, cart];
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
};
|
|
163
|
-
var _handleInventoryError = function (shouldSave) { return __awaiter(void 0, void 0, void 0, function () {
|
|
164
|
-
return __generator(this, function (_a) {
|
|
165
|
-
switch (_a.label) {
|
|
166
|
-
case 0:
|
|
167
|
-
setSnackbar({
|
|
168
|
-
title: (0, functions_1.getStringByLang)({
|
|
169
|
-
en: '
|
|
170
|
-
fr: '
|
|
171
|
-
}, locale),
|
|
172
|
-
type: ISnackbarContext_1.SnackbarTypeEnum.ERROR,
|
|
173
|
-
isShown: true,
|
|
174
|
-
isCart: true
|
|
175
|
-
});
|
|
176
|
-
if (!!!shouldSave) return [3, 2];
|
|
177
|
-
return [4, saveCart(undefined)];
|
|
178
|
-
case 1:
|
|
179
|
-
_a.sent();
|
|
180
|
-
_a.label = 2;
|
|
181
|
-
case 2: return [2];
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
}); };
|
|
185
|
-
var addItem = function (newCart, items, retro, useParentName, saveToDatabase, defaultAdd, isServiceUpsellOfSku, customPriceList) {
|
|
186
|
-
if (retro === void 0) { retro = true; }
|
|
187
|
-
if (useParentName === void 0) { useParentName = false; }
|
|
188
|
-
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
189
|
-
if (defaultAdd === void 0) { defaultAdd = false; }
|
|
190
|
-
if (isServiceUpsellOfSku === void 0) { isServiceUpsellOfSku = false; }
|
|
191
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
192
|
-
var cart, addedValues, i, productName;
|
|
193
|
-
return __generator(this, function (_a) {
|
|
194
|
-
switch (_a.label) {
|
|
195
|
-
case 0:
|
|
196
|
-
if (!!!api || !!!sdk)
|
|
197
|
-
throw new Error('SDK not initialised');
|
|
198
|
-
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
199
|
-
if (!!isGuest && typeof isGuest === 'string' && !!cart.user_email) {
|
|
200
|
-
cart = __assign(__assign({}, cart), { user_email: isGuest });
|
|
201
|
-
}
|
|
202
|
-
return [4, (0, cart_1.addCartItems)(cart, Array.isArray(items) ? items : [items], sdk.products.getOneInventoryItem, api.org !== 'gr', _handleInventoryError, defaultAdd, isServiceUpsellOfSku, customPriceList)];
|
|
203
|
-
case 1:
|
|
204
|
-
addedValues = _a.sent();
|
|
205
|
-
cart = addedValues.cartData;
|
|
206
|
-
if (!!!saveToDatabase) return [3, 3];
|
|
207
|
-
return [4, saveCart(cart)];
|
|
208
|
-
case 2:
|
|
209
|
-
cart = _a.sent();
|
|
210
|
-
_a.label = 3;
|
|
211
|
-
case 3:
|
|
212
|
-
if (!!retro && !!items && (Array.isArray(items) ? !!items.length : true)) {
|
|
213
|
-
i = Array.isArray(items) ? items[0] : items;
|
|
214
|
-
productName = useParentName ? i.parent_name : i.name;
|
|
215
|
-
if (!!addedValues.updated) {
|
|
216
|
-
setSnackbar({
|
|
217
|
-
title: (0, functions_1.getStringByLang)({
|
|
218
|
-
en: "".concat(!!productName ? (0, functions_1.translate)(locale || 'en', productName) : 'The item', " has been successfuly updated in your cart!"),
|
|
219
|
-
fr: "".concat(!!productName ? (0, functions_1.translate)(locale || 'fr', productName) : "L'item", " a bien \u00E9t\u00E9 mis \u00E0 jour dans votre panier!")
|
|
220
|
-
}, locale),
|
|
221
|
-
type: ISnackbarContext_1.SnackbarTypeEnum.SUCCESS,
|
|
222
|
-
isShown: true,
|
|
223
|
-
isCart: true
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
else if (!!addedValues.added) {
|
|
227
|
-
setSnackbar({
|
|
228
|
-
title: (0, functions_1.getStringByLang)({
|
|
229
|
-
en: "".concat(!!productName ? (0, functions_1.translate)(locale || 'en', productName) : 'The item', " has been successfuly added to your cart!"),
|
|
230
|
-
fr: "".concat(!!productName ? (0, functions_1.translate)(locale || 'fr', productName) : "L'item", " a bien \u00E9t\u00E9 ajout\u00E9 \u00E0 votre panier!")
|
|
231
|
-
}, locale),
|
|
232
|
-
type: ISnackbarContext_1.SnackbarTypeEnum.SUCCESS,
|
|
233
|
-
isShown: true,
|
|
234
|
-
isCart: true
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
if (!!!addedValues.maxQtyItems) {
|
|
238
|
-
setCartSliderOpen(true);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
return [2, cart];
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
};
|
|
246
|
-
var updateItem = function (newCart, items, saveToDatabase) {
|
|
247
|
-
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
248
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
249
|
-
var updatedItemsCart, itemsToUpdate, updatedValues;
|
|
250
|
-
return __generator(this, function (_a) {
|
|
251
|
-
switch (_a.label) {
|
|
252
|
-
case 0:
|
|
253
|
-
if (!!!api || !!!sdk)
|
|
254
|
-
throw new Error('SDK not initialised');
|
|
255
|
-
updatedItemsCart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
256
|
-
itemsToUpdate = Array.isArray(items) ? items : [items];
|
|
257
|
-
return [4, (0, cart_1.updateCartItems)(updatedItemsCart, itemsToUpdate, sdk.products.getOneInventoryItem, api.org !== 'gr', _handleInventoryError)];
|
|
258
|
-
case 1:
|
|
259
|
-
updatedValues = _a.sent();
|
|
260
|
-
if (!!!saveToDatabase) return [3, 3];
|
|
261
|
-
return [4, saveCart({ items: updatedValues.updatedCart.items })];
|
|
262
|
-
case 2:
|
|
263
|
-
updatedItemsCart = _a.sent();
|
|
264
|
-
_a.label = 3;
|
|
265
|
-
case 3: return [2, updatedItemsCart];
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
});
|
|
269
|
-
};
|
|
270
|
-
var deleteItem = function (newCart, sku, saveToDatabase, onlyIfUpsellOf) {
|
|
271
|
-
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
272
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
273
|
-
var cart;
|
|
274
|
-
return __generator(this, function (_a) {
|
|
275
|
-
switch (_a.label) {
|
|
276
|
-
case 0:
|
|
277
|
-
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
278
|
-
cart = (0, cart_1.deleteCartItem)(cart, Array.isArray(sku) ? sku : [sku], onlyIfUpsellOf);
|
|
279
|
-
if (!!!saveToDatabase) return [3, 2];
|
|
280
|
-
return [4, saveCart({ items: cart.items })];
|
|
281
|
-
case 1:
|
|
282
|
-
cart = _a.sent();
|
|
283
|
-
_a.label = 2;
|
|
284
|
-
case 2: return [2, cart];
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
});
|
|
288
|
-
};
|
|
289
|
-
var verifyCart = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
290
|
-
var request, responseData, response, error_1;
|
|
291
|
-
return __generator(this, function (_a) {
|
|
292
|
-
switch (_a.label) {
|
|
293
|
-
case 0:
|
|
294
|
-
if (!!!api || !!!cartData)
|
|
295
|
-
return [2, false];
|
|
296
|
-
if (cartData.status >= Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS)
|
|
297
|
-
throw new Error("Can't process a paid or higher status cart.");
|
|
298
|
-
if (!!!cartData.id) return [3, 5];
|
|
299
|
-
_a.label = 1;
|
|
300
|
-
case 1:
|
|
301
|
-
_a.trys.push([1, 4, , 5]);
|
|
302
|
-
return [4, fetch("".concat(api.api, "/cart/").concat(cartData.id, "/verify"), {
|
|
303
|
-
method: 'GET',
|
|
304
|
-
headers: {
|
|
305
|
-
Authorization: "Bearer ".concat(api.token),
|
|
306
|
-
'Content-Type': 'application/json',
|
|
307
|
-
'x-gng-ecomm-token': api.userToken
|
|
308
|
-
}
|
|
309
|
-
})];
|
|
310
|
-
case 2:
|
|
311
|
-
request = _a.sent();
|
|
312
|
-
return [4, request.json()];
|
|
313
|
-
case 3:
|
|
314
|
-
responseData = (_a.sent());
|
|
315
|
-
if (responseData.status != 200) {
|
|
316
|
-
throw 'Erreur lors de la requête des quotes.' + responseData.error;
|
|
317
|
-
}
|
|
318
|
-
response = responseData.data;
|
|
319
|
-
setCartData(response.cart);
|
|
320
|
-
return [2, response.actionsDone];
|
|
321
|
-
case 4:
|
|
322
|
-
error_1 = _a.sent();
|
|
323
|
-
console.log('error in verifyCart: ', error_1);
|
|
324
|
-
return [3, 5];
|
|
325
|
-
case 5: return [2, false];
|
|
326
|
-
}
|
|
327
|
-
});
|
|
328
|
-
}); };
|
|
329
|
-
var uploadFileToOrder = function (newCart, file, propsName, saveToDatabase) {
|
|
330
|
-
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
331
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
332
|
-
var cart, base64, request, responseData;
|
|
333
|
-
var _a, _b, _c, _d;
|
|
334
|
-
return __generator(this, function (_e) {
|
|
335
|
-
switch (_e.label) {
|
|
336
|
-
case 0:
|
|
337
|
-
if (!!!api)
|
|
338
|
-
return [2];
|
|
339
|
-
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
340
|
-
if (!!!cart.id)
|
|
341
|
-
return [2, {
|
|
342
|
-
cart: cart,
|
|
343
|
-
data: undefined,
|
|
344
|
-
error: "Couldn't convert cart data"
|
|
345
|
-
}];
|
|
346
|
-
if (cart.status >= Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS)
|
|
347
|
-
throw new Error("Can't process a paid or higher status cart.");
|
|
348
|
-
return [4, (0, functions_2.toBase64)(file)];
|
|
349
|
-
case 1:
|
|
350
|
-
base64 = _e.sent();
|
|
351
|
-
if (typeof base64 !== 'string') {
|
|
352
|
-
return [2, {
|
|
353
|
-
cart: cart,
|
|
354
|
-
data: undefined,
|
|
355
|
-
error: "Couldn't convert file to base64"
|
|
356
|
-
}];
|
|
357
|
-
}
|
|
358
|
-
return [4, fetch("".concat(api.api, "/cart/").concat(cart.id, "/uploadBase64"), {
|
|
359
|
-
method: 'PUT',
|
|
360
|
-
headers: {
|
|
361
|
-
Authorization: "Bearer ".concat(api.token),
|
|
362
|
-
'Content-Type': 'application/json',
|
|
363
|
-
'x-gng-ecomm-token': api.userToken
|
|
364
|
-
},
|
|
365
|
-
body: JSON.stringify({
|
|
366
|
-
base64: base64
|
|
367
|
-
})
|
|
368
|
-
})];
|
|
369
|
-
case 2:
|
|
370
|
-
request = _e.sent();
|
|
371
|
-
return [4, request.json()];
|
|
372
|
-
case 3:
|
|
373
|
-
responseData = (_e.sent());
|
|
374
|
-
if (responseData.status != 200) {
|
|
375
|
-
throw new Error("Impossible d'uploader le fichier" + responseData.error);
|
|
376
|
-
}
|
|
377
|
-
if (!!!responseData.data) return [3, 6];
|
|
378
|
-
if (!!!saveToDatabase) return [3, 5];
|
|
379
|
-
return [4, saveCart({
|
|
380
|
-
shipping_payload: cart.shipping_payload
|
|
381
|
-
? __assign(__assign({}, cart.shipping_payload), (_a = {}, _a[propsName] = responseData.data, _a)) : (_b = {},
|
|
382
|
-
_b[propsName] = responseData.data,
|
|
383
|
-
_b)
|
|
384
|
-
})];
|
|
385
|
-
case 4:
|
|
386
|
-
cart = _e.sent();
|
|
387
|
-
return [3, 6];
|
|
388
|
-
case 5:
|
|
389
|
-
cart = __assign(__assign({}, cart), { shipping_payload: cart.shipping_payload
|
|
390
|
-
? __assign(__assign({}, cart.shipping_payload), (_c = {}, _c[propsName] = responseData.data, _c)) : (_d = {},
|
|
391
|
-
_d[propsName] = responseData.data,
|
|
392
|
-
_d) });
|
|
393
|
-
_e.label = 6;
|
|
394
|
-
case 6: return [2, { cart: cart, data: responseData.data, error: undefined }];
|
|
395
|
-
}
|
|
396
|
-
});
|
|
397
|
-
});
|
|
398
|
-
};
|
|
399
|
-
var emptyCart = function (removeCartIDFromLocalStorage) {
|
|
400
|
-
if (removeCartIDFromLocalStorage === void 0) { removeCartIDFromLocalStorage = true; }
|
|
401
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
402
|
-
var cartID, cartFromDB, err_1;
|
|
403
|
-
return __generator(this, function (_a) {
|
|
404
|
-
switch (_a.label) {
|
|
405
|
-
case 0:
|
|
406
|
-
if (!!!api || !!!sdk)
|
|
407
|
-
return [2, false];
|
|
408
|
-
if (!!!removeCartIDFromLocalStorage) return [3, 1];
|
|
409
|
-
if (process.env.NODE_ENV !== 'production')
|
|
410
|
-
console.log('`${api.org}__cart`', "".concat(api.org, "__cart"));
|
|
411
|
-
localStorage.removeItem("".concat(api.org, "__cart"));
|
|
412
|
-
setCartData(undefined);
|
|
413
|
-
return [3, 5];
|
|
414
|
-
case 1:
|
|
415
|
-
cartID = localStorage.getItem("".concat(api.org, "__cart"));
|
|
416
|
-
if (!!!cartID) return [3, 5];
|
|
417
|
-
_a.label = 2;
|
|
418
|
-
case 2:
|
|
419
|
-
_a.trys.push([2, 4, , 5]);
|
|
420
|
-
return [4, getCartFromDatabase(cartID)];
|
|
421
|
-
case 3:
|
|
422
|
-
cartFromDB = _a.sent();
|
|
423
|
-
if (!!cartFromDB && !!!cartFromDB.clerk_id) {
|
|
424
|
-
setCartData(cartFromDB);
|
|
425
|
-
}
|
|
426
|
-
return [3, 5];
|
|
427
|
-
case 4:
|
|
428
|
-
err_1 = _a.sent();
|
|
429
|
-
return [3, 5];
|
|
430
|
-
case 5: return [2, true];
|
|
431
|
-
}
|
|
432
|
-
});
|
|
433
|
-
});
|
|
434
|
-
};
|
|
435
|
-
var handlePayment = function (data) {
|
|
436
|
-
if (data === void 0) { data = {}; }
|
|
437
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
438
|
-
var request, responseData;
|
|
439
|
-
return __generator(this, function (_a) {
|
|
440
|
-
switch (_a.label) {
|
|
441
|
-
case 0:
|
|
442
|
-
if (!!!api || !!!sdk)
|
|
443
|
-
return [2, { error: { en: 'SDK not initialized', fr: 'SDK non initialisé' } }];
|
|
444
|
-
if (!!!cartData)
|
|
445
|
-
throw new Error("Can't pay an empty/undefined cart");
|
|
446
|
-
if (cartData.status >= Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS)
|
|
447
|
-
throw new Error("Can't process a paid or higher status cart.");
|
|
448
|
-
return [4, fetch("".concat(api.api, "/cart/").concat(cartData.id, "/payment"), {
|
|
449
|
-
method: 'POST',
|
|
450
|
-
headers: {
|
|
451
|
-
Authorization: "Bearer ".concat(api.token),
|
|
452
|
-
'Content-Type': 'application/json',
|
|
453
|
-
'x-gng-ecomm-token': api.userToken
|
|
454
|
-
},
|
|
455
|
-
body: JSON.stringify(__assign({ customer_email: cartData.user_email, customer_name: cartData.billing_address ? cartData.billing_address.expedition_recipient_name : 'Anonyme', cartTotal: Number((cartData.total * 100).toFixed(0)) }, data))
|
|
456
|
-
})];
|
|
457
|
-
case 1:
|
|
458
|
-
request = _a.sent();
|
|
459
|
-
return [4, request.json()];
|
|
460
|
-
case 2:
|
|
461
|
-
responseData = _a.sent();
|
|
462
|
-
if (responseData.status != 200) {
|
|
463
|
-
return [2, { error: responseData.error }];
|
|
464
|
-
}
|
|
465
|
-
if (!!responseData.token) {
|
|
466
|
-
sdk.setUserToken(responseData.token);
|
|
467
|
-
}
|
|
468
|
-
return [2, { accessToken: responseData.data }];
|
|
469
|
-
}
|
|
470
|
-
});
|
|
471
|
-
});
|
|
472
|
-
};
|
|
473
|
-
var setPaidBy = function (paidByValue, customPriceList) { return __awaiter(void 0, void 0, void 0, function () {
|
|
474
|
-
var partial, customIncentiveInfo, e_1;
|
|
475
|
-
return __generator(this, function (_a) {
|
|
476
|
-
switch (_a.label) {
|
|
477
|
-
case 0:
|
|
478
|
-
if (!!!cartData)
|
|
479
|
-
return [2];
|
|
480
|
-
partial = { paid_by: paidByValue, id: cartData.id };
|
|
481
|
-
if (!!customPriceList) {
|
|
482
|
-
customIncentiveInfo = (0, g2_1.getCustomIncentiveInfo)(customPriceList, cartData.discount_rules);
|
|
483
|
-
if (!!customIncentiveInfo) {
|
|
484
|
-
partial = __assign(__assign({}, partial), { discount_rules: customIncentiveInfo.updatedDiscountRules });
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
_a.label = 1;
|
|
488
|
-
case 1:
|
|
489
|
-
_a.trys.push([1, 3, , 4]);
|
|
490
|
-
return [4, saveCart(partial, false, undefined, false, true)];
|
|
491
|
-
case 2:
|
|
492
|
-
_a.sent();
|
|
493
|
-
return [3, 4];
|
|
494
|
-
case 3:
|
|
495
|
-
e_1 = _a.sent();
|
|
496
|
-
console.log('error on saving cart with paid_by value: ', e_1);
|
|
497
|
-
return [3, 4];
|
|
498
|
-
case 4: return [2];
|
|
499
|
-
}
|
|
500
|
-
});
|
|
501
|
-
}); };
|
|
502
|
-
var saveOrder = function (payment_payload) { return __awaiter(void 0, void 0, void 0, function () {
|
|
503
|
-
var fetchData, response, err_2;
|
|
504
|
-
return __generator(this, function (_a) {
|
|
505
|
-
switch (_a.label) {
|
|
506
|
-
case 0:
|
|
507
|
-
if (!!!api)
|
|
508
|
-
throw new Error('SDK not initialised');
|
|
509
|
-
if (!!!cartData)
|
|
510
|
-
throw new Error("Can't pay an empty/undefined cart");
|
|
511
|
-
if (cartData.status >= Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS)
|
|
512
|
-
throw new Error("Can't process a paid or higher status cart.");
|
|
513
|
-
_a.label = 1;
|
|
514
|
-
case 1:
|
|
515
|
-
_a.trys.push([1, 4, , 5]);
|
|
516
|
-
return [4, fetch("".concat(api.api, "/cart/").concat(cartData.id, "/saveOrder"), {
|
|
517
|
-
method: 'POST',
|
|
518
|
-
headers: {
|
|
519
|
-
authorization: "Bearer ".concat(api.token),
|
|
520
|
-
'Content-Type': 'application/json',
|
|
521
|
-
'x-gng-ecomm-token': api.userToken
|
|
522
|
-
},
|
|
523
|
-
body: JSON.stringify({ payment_payload: payment_payload })
|
|
524
|
-
})];
|
|
525
|
-
case 2:
|
|
526
|
-
fetchData = _a.sent();
|
|
527
|
-
return [4, fetchData.json()];
|
|
528
|
-
case 3:
|
|
529
|
-
response = (_a.sent());
|
|
530
|
-
if (response.status !== 200) {
|
|
531
|
-
return [2, 'error'];
|
|
532
|
-
}
|
|
533
|
-
return [2, response.data];
|
|
534
|
-
case 4:
|
|
535
|
-
err_2 = _a.sent();
|
|
536
|
-
return [2, 'error'];
|
|
537
|
-
case 5: return [2];
|
|
538
|
-
}
|
|
539
|
-
});
|
|
540
|
-
}); };
|
|
541
|
-
var removeDiscountRule = function (cart, code) { return __awaiter(void 0, void 0, void 0, function () {
|
|
542
|
-
var e_2;
|
|
543
|
-
return __generator(this, function (_a) {
|
|
544
|
-
switch (_a.label) {
|
|
545
|
-
case 0:
|
|
546
|
-
_a.trys.push([0, 2, , 3]);
|
|
547
|
-
return [4, saveCart({
|
|
548
|
-
subtotalWithoutPromotions: null,
|
|
549
|
-
cart_promo_applied: null,
|
|
550
|
-
cart_promos_applied: !!cart.cart_promos_applied
|
|
551
|
-
? cart.cart_promos_applied.filter(function (x) { return x.promo_code !== code; })
|
|
552
|
-
: null
|
|
553
|
-
})];
|
|
554
|
-
case 1:
|
|
555
|
-
_a.sent();
|
|
556
|
-
return [2, true];
|
|
557
|
-
case 2:
|
|
558
|
-
e_2 = _a.sent();
|
|
559
|
-
console.log('error on removing discountRule: ', e_2.toString());
|
|
560
|
-
return [2, false];
|
|
561
|
-
case 3: return [2];
|
|
562
|
-
}
|
|
563
|
-
});
|
|
564
|
-
}); };
|
|
565
|
-
var updateBillingAddress = function (newCart, expeditionInfo, saveToDatabase, setCAIfNoCountry) {
|
|
566
|
-
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
567
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
568
|
-
var cart, newExpeditionInfo;
|
|
569
|
-
return __generator(this, function (_a) {
|
|
570
|
-
switch (_a.label) {
|
|
571
|
-
case 0:
|
|
572
|
-
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
573
|
-
newExpeditionInfo = expeditionInfo;
|
|
574
|
-
if (!!setCAIfNoCountry && !!expeditionInfo && !!!expeditionInfo.address.country) {
|
|
575
|
-
newExpeditionInfo = __assign(__assign({}, expeditionInfo), { address: __assign(__assign({}, expeditionInfo.address), { country: 'CA' }) });
|
|
576
|
-
}
|
|
577
|
-
if (!!!saveToDatabase) return [3, 2];
|
|
578
|
-
return [4, saveCart({ billing_address: newExpeditionInfo })];
|
|
579
|
-
case 1:
|
|
580
|
-
cart = _a.sent();
|
|
581
|
-
return [3, 3];
|
|
582
|
-
case 2:
|
|
583
|
-
cart = __assign(__assign({}, cart), { billing_address: newExpeditionInfo });
|
|
584
|
-
_a.label = 3;
|
|
585
|
-
case 3: return [2, cart];
|
|
586
|
-
}
|
|
587
|
-
});
|
|
588
|
-
});
|
|
589
|
-
};
|
|
590
|
-
var updateShippingAddress = function (newCart, isPickupAtStore, expeditionInfo, saveToDatabase, setCAIfNoCountry) {
|
|
591
|
-
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
592
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
593
|
-
var cart, newExpeditionInfo;
|
|
594
|
-
return __generator(this, function (_a) {
|
|
595
|
-
switch (_a.label) {
|
|
596
|
-
case 0:
|
|
597
|
-
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
598
|
-
newExpeditionInfo = expeditionInfo;
|
|
599
|
-
if (!!setCAIfNoCountry && !!expeditionInfo && !!!expeditionInfo.address.country) {
|
|
600
|
-
newExpeditionInfo = __assign(__assign({}, expeditionInfo), { address: __assign(__assign({}, expeditionInfo.address), { country: 'CA' }) });
|
|
601
|
-
}
|
|
602
|
-
if (!!!saveToDatabase) return [3, 2];
|
|
603
|
-
return [4, saveCart({
|
|
604
|
-
is_pickup: isPickupAtStore,
|
|
605
|
-
shipping_address: newExpeditionInfo,
|
|
606
|
-
shipping_selected: undefined
|
|
607
|
-
})];
|
|
608
|
-
case 1:
|
|
609
|
-
cart = _a.sent();
|
|
610
|
-
return [3, 3];
|
|
611
|
-
case 2:
|
|
612
|
-
cart = __assign(__assign({}, cart), { is_pickup: isPickupAtStore, shipping_address: newExpeditionInfo, shipping_selected: undefined });
|
|
613
|
-
_a.label = 3;
|
|
614
|
-
case 3: return [2, cart];
|
|
615
|
-
}
|
|
616
|
-
});
|
|
617
|
-
});
|
|
618
|
-
};
|
|
619
|
-
var getIsSameBillingThenShipping = function () {
|
|
620
|
-
if (!!!cartData)
|
|
621
|
-
return false;
|
|
622
|
-
if (!!!cartData.billing_address && !!!cartData.shipping_address)
|
|
623
|
-
return false;
|
|
624
|
-
return (!!cartData.billing_address &&
|
|
625
|
-
!!cartData.shipping_address &&
|
|
626
|
-
(0, functions_1.string_to_slug)(cartData.billing_address.address.postal_code) ===
|
|
627
|
-
(0, functions_1.string_to_slug)(cartData.shipping_address.address.postal_code) &&
|
|
628
|
-
(0, functions_1.string_to_slug)(cartData.billing_address.address.line1) === (0, functions_1.string_to_slug)(cartData.shipping_address.address.line1));
|
|
629
|
-
};
|
|
630
|
-
var setShippingPayload = function (newCart, payload, saveToDatabase) {
|
|
631
|
-
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
632
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
633
|
-
var cart;
|
|
634
|
-
return __generator(this, function (_a) {
|
|
635
|
-
switch (_a.label) {
|
|
636
|
-
case 0:
|
|
637
|
-
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
638
|
-
if (!!!saveToDatabase) return [3, 2];
|
|
639
|
-
return [4, saveCart({ shipping_payload: payload })];
|
|
640
|
-
case 1:
|
|
641
|
-
cart = _a.sent();
|
|
642
|
-
return [3, 3];
|
|
643
|
-
case 2:
|
|
644
|
-
cart = __assign(__assign({}, cart), { shipping_payload: payload });
|
|
645
|
-
_a.label = 3;
|
|
646
|
-
case 3: return [2, cart];
|
|
647
|
-
}
|
|
648
|
-
});
|
|
649
|
-
});
|
|
650
|
-
};
|
|
651
|
-
var getShippingQuotes = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
652
|
-
var request, responseData;
|
|
653
|
-
return __generator(this, function (_a) {
|
|
654
|
-
switch (_a.label) {
|
|
655
|
-
case 0:
|
|
656
|
-
if (!!!api || !!!sdk)
|
|
657
|
-
return [2];
|
|
658
|
-
if (!!!cartData)
|
|
659
|
-
throw new Error("Can't get shipping quotes for an empty/undefined cart");
|
|
660
|
-
if (cartData.status >= Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS)
|
|
661
|
-
throw new Error("Can't process a paid or higher status cart.");
|
|
662
|
-
return [4, fetch("".concat(api.api, "/cart/getShippingQuotes"), {
|
|
663
|
-
method: 'POST',
|
|
664
|
-
headers: {
|
|
665
|
-
Authorization: "Bearer ".concat(api.token),
|
|
666
|
-
'x-gng-ecomm-token': api.userToken
|
|
667
|
-
},
|
|
668
|
-
body: cartData.id
|
|
669
|
-
})];
|
|
670
|
-
case 1:
|
|
671
|
-
request = _a.sent();
|
|
672
|
-
return [4, request.json()];
|
|
673
|
-
case 2:
|
|
674
|
-
responseData = (_a.sent());
|
|
675
|
-
if (responseData.status != 200 || !!responseData.error) {
|
|
676
|
-
throw 'Erreur lors de la requête des quotes.' + responseData.error;
|
|
677
|
-
}
|
|
678
|
-
if (responseData.token) {
|
|
679
|
-
sdk.setUserToken(responseData.token);
|
|
680
|
-
}
|
|
681
|
-
return [2, responseData.data.shippingQuotes];
|
|
682
|
-
}
|
|
683
|
-
});
|
|
684
|
-
}); };
|
|
685
|
-
var selectShippingQuote = function (quote) { return __awaiter(void 0, void 0, void 0, function () {
|
|
686
|
-
var request, responseData, updatedCart;
|
|
687
|
-
return __generator(this, function (_a) {
|
|
688
|
-
switch (_a.label) {
|
|
689
|
-
case 0:
|
|
690
|
-
if (!!!api || !!!sdk)
|
|
691
|
-
return [2];
|
|
692
|
-
if (!!!cartData)
|
|
693
|
-
throw new Error("Can't select shipping quote for an empty/undefined cart");
|
|
694
|
-
return [4, fetch("".concat(api.api, "/cart/").concat(cartData.id, "/setSelectedShippingMethod"), {
|
|
695
|
-
method: 'POST',
|
|
696
|
-
headers: {
|
|
697
|
-
Authorization: "Bearer ".concat(api.token),
|
|
698
|
-
'Content-Type': 'application/json',
|
|
699
|
-
'x-gng-ecomm-token': api.userToken
|
|
700
|
-
},
|
|
701
|
-
body: JSON.stringify(quote)
|
|
702
|
-
})];
|
|
703
|
-
case 1:
|
|
704
|
-
request = _a.sent();
|
|
705
|
-
return [4, request.json()];
|
|
706
|
-
case 2:
|
|
707
|
-
responseData = _a.sent();
|
|
708
|
-
if (responseData.status != 200) {
|
|
709
|
-
throw new Error('Impossible de sélectionner la shipping quote ' + responseData.error);
|
|
710
|
-
}
|
|
711
|
-
if (responseData.token) {
|
|
712
|
-
sdk.setUserToken(responseData.token);
|
|
713
|
-
}
|
|
714
|
-
updatedCart = responseData.data;
|
|
715
|
-
setCartData(updatedCart);
|
|
716
|
-
return [2, updatedCart];
|
|
717
|
-
}
|
|
718
|
-
});
|
|
719
|
-
}); };
|
|
720
|
-
var setPromoCode = function (promo_code) { return __awaiter(void 0, void 0, void 0, function () {
|
|
721
|
-
var error_2;
|
|
722
|
-
return __generator(this, function (_a) {
|
|
723
|
-
switch (_a.label) {
|
|
724
|
-
case 0:
|
|
725
|
-
_a.trys.push([0, 2, , 3]);
|
|
726
|
-
return [4, saveCart({}, true, promo_code)];
|
|
727
|
-
case 1:
|
|
728
|
-
_a.sent();
|
|
729
|
-
return [3, 3];
|
|
730
|
-
case 2:
|
|
731
|
-
error_2 = _a.sent();
|
|
732
|
-
if (process.env.NODE_ENV === 'development')
|
|
733
|
-
console.log("error on SDK while applying promo on cart", error_2);
|
|
734
|
-
return [3, 3];
|
|
735
|
-
case 3: return [2];
|
|
736
|
-
}
|
|
737
|
-
});
|
|
738
|
-
}); };
|
|
739
|
-
var getCartFromDatabase = function (cardID) { return __awaiter(void 0, void 0, void 0, function () {
|
|
740
|
-
var request, responseData;
|
|
741
|
-
return __generator(this, function (_a) {
|
|
742
|
-
switch (_a.label) {
|
|
743
|
-
case 0:
|
|
744
|
-
if (!!!api || !!!sdk)
|
|
745
|
-
return [2];
|
|
746
|
-
return [4, fetch("".concat(api.api, "/cart/").concat(cardID), {
|
|
747
|
-
method: 'GET',
|
|
748
|
-
headers: {
|
|
749
|
-
Authorization: "Bearer ".concat(api.token),
|
|
750
|
-
'Content-Type': 'application/json',
|
|
751
|
-
'x-gng-ecomm-token': api.userToken
|
|
752
|
-
}
|
|
753
|
-
})];
|
|
754
|
-
case 1:
|
|
755
|
-
request = _a.sent();
|
|
756
|
-
return [4, request.json()];
|
|
757
|
-
case 2:
|
|
758
|
-
responseData = _a.sent();
|
|
759
|
-
if (responseData.status !== 200) {
|
|
760
|
-
throw new Error('Impossible de fetch le cart: ' + responseData.error);
|
|
761
|
-
}
|
|
762
|
-
if (responseData.token) {
|
|
763
|
-
sdk.setUserToken(responseData.token);
|
|
764
|
-
}
|
|
765
|
-
return [2, responseData.data];
|
|
766
|
-
}
|
|
767
|
-
});
|
|
768
|
-
}); };
|
|
769
|
-
var saveCart = function (cart, saveToDatabase, promo_code, onlyNewCart, partialOnly) {
|
|
770
|
-
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
771
|
-
if (onlyNewCart === void 0) { onlyNewCart = false; }
|
|
772
|
-
if (partialOnly === void 0) { partialOnly = false; }
|
|
773
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
774
|
-
var newCartData, response, responseData, cartIDInLocalStorage;
|
|
775
|
-
return __generator(this, function (_a) {
|
|
776
|
-
switch (_a.label) {
|
|
777
|
-
case 0:
|
|
778
|
-
newCartData = __assign(__assign({}, (!!cartData && !!!onlyNewCart ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB))), cart);
|
|
779
|
-
if (!!!newCartData)
|
|
780
|
-
throw new Error("Can't save an empty/undefined cart");
|
|
781
|
-
if (newCartData.status >= Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS)
|
|
782
|
-
throw new Error("Can't process a paid or higher status cart.");
|
|
783
|
-
if (!!!api) return [3, 3];
|
|
784
|
-
return [4, fetch("".concat(api.api, "/cart"), {
|
|
785
|
-
method: 'POST',
|
|
786
|
-
headers: {
|
|
787
|
-
Authorization: "Bearer ".concat(api.token),
|
|
788
|
-
'Content-Type': 'application/json',
|
|
789
|
-
'x-gng-ecomm-token': api.userToken
|
|
790
|
-
},
|
|
791
|
-
body: JSON.stringify({ cartData: newCartData, currency: currency, locale: locale, promo_code: promo_code, saveToDatabase: saveToDatabase, partialOnly: partialOnly })
|
|
792
|
-
})];
|
|
793
|
-
case 1:
|
|
794
|
-
response = _a.sent();
|
|
795
|
-
return [4, response.json()];
|
|
796
|
-
case 2:
|
|
797
|
-
responseData = _a.sent();
|
|
798
|
-
if (responseData.status != 200 || !!responseData.error) {
|
|
799
|
-
setCartErrors([
|
|
800
|
-
typeof responseData.error === 'string'
|
|
801
|
-
? responseData.error
|
|
802
|
-
: (0, functions_1.getStringByLang)(responseData.error, locale)
|
|
803
|
-
]);
|
|
804
|
-
}
|
|
805
|
-
else {
|
|
806
|
-
if (!!responseData.data) {
|
|
807
|
-
cartIDInLocalStorage = localStorage.getItem("".concat(api.org, "__cart"));
|
|
808
|
-
if (!!responseData.data.id && (!!!cartIDInLocalStorage || cartIDInLocalStorage !== responseData.data.id)) {
|
|
809
|
-
localStorage.setItem("".concat(api.org, "__cart"), responseData.data.id);
|
|
810
|
-
}
|
|
811
|
-
newCartData = responseData.data;
|
|
812
|
-
setCartData(newCartData);
|
|
813
|
-
setCartErrors([]);
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
return [3, 4];
|
|
817
|
-
case 3:
|
|
818
|
-
setCartData(newCartData);
|
|
819
|
-
_a.label = 4;
|
|
820
|
-
case 4: return [2, newCartData];
|
|
821
|
-
}
|
|
822
|
-
});
|
|
823
|
-
});
|
|
824
|
-
};
|
|
825
|
-
var getApplicablePromos = function (forceShipping) { return __awaiter(void 0, void 0, void 0, function () {
|
|
826
|
-
var request, responseData;
|
|
827
|
-
return __generator(this, function (_a) {
|
|
828
|
-
switch (_a.label) {
|
|
829
|
-
case 0:
|
|
830
|
-
if (!!!api || !!!sdk)
|
|
831
|
-
return [2];
|
|
832
|
-
if (!!!cartData)
|
|
833
|
-
throw new Error("Can't get promos if no cart data");
|
|
834
|
-
return [4, fetch("".concat(api.api, "/cart/").concat(cartData.id, "/getApplicablePromosWithShipping"), {
|
|
835
|
-
method: 'GET',
|
|
836
|
-
headers: {
|
|
837
|
-
Authorization: "Bearer ".concat(api.token),
|
|
838
|
-
'Content-Type': 'application/json',
|
|
839
|
-
'x-gng-ecomm-token': api.userToken
|
|
840
|
-
}
|
|
841
|
-
})];
|
|
842
|
-
case 1:
|
|
843
|
-
request = _a.sent();
|
|
844
|
-
return [4, request.json()];
|
|
845
|
-
case 2:
|
|
846
|
-
responseData = _a.sent();
|
|
847
|
-
if (responseData.status !== 200) {
|
|
848
|
-
throw new Error('Impossible de fetch les promos associé: ' + responseData.error);
|
|
849
|
-
}
|
|
850
|
-
return [2, responseData.data];
|
|
851
|
-
}
|
|
852
|
-
});
|
|
853
|
-
}); };
|
|
854
|
-
var canBePaidWithPA = function (userId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
855
|
-
var request, responseData;
|
|
856
|
-
return __generator(this, function (_a) {
|
|
857
|
-
switch (_a.label) {
|
|
858
|
-
case 0:
|
|
859
|
-
if (!!!api)
|
|
860
|
-
return [2];
|
|
861
|
-
if (!!!cartData)
|
|
862
|
-
throw new Error("Can't check if cart is payable with PA if cartData is undefined.");
|
|
863
|
-
if (!!!userId)
|
|
864
|
-
throw new Error("Can't check if cart is payable with PA without userId");
|
|
865
|
-
return [4, fetch("".concat(api.api, "/cart/").concat(cartData.id, "/canBePaidWithPA"), {
|
|
866
|
-
method: 'POST',
|
|
867
|
-
headers: {
|
|
868
|
-
Authorization: "Bearer ".concat(api.token),
|
|
869
|
-
'Content-Type': 'application/json',
|
|
870
|
-
'x-gng-ecomm-token': api.userToken
|
|
871
|
-
},
|
|
872
|
-
body: JSON.stringify({ userId: userId })
|
|
873
|
-
})];
|
|
874
|
-
case 1:
|
|
875
|
-
request = _a.sent();
|
|
876
|
-
return [4, request.json()];
|
|
877
|
-
case 2:
|
|
878
|
-
responseData = _a.sent();
|
|
879
|
-
if (responseData.status !== 200) {
|
|
880
|
-
throw new Error("Can't apply promise to purchase to cart: " + responseData.error);
|
|
881
|
-
}
|
|
882
|
-
return [2, responseData.data];
|
|
883
|
-
}
|
|
884
|
-
});
|
|
885
|
-
}); };
|
|
886
|
-
var getPaymentIncentiveDiscountAmount = function (skipCheckForRuleFields) {
|
|
887
|
-
if (!!!cartData)
|
|
888
|
-
return;
|
|
889
|
-
if (!!cartData.promo_after_taxes_amount)
|
|
890
|
-
return cartData.promo_after_taxes_amount;
|
|
891
|
-
var couldApplyIncentive = (0, discount_1.checkIfCouldApplyIncentivePromo)(cartData, skipCheckForRuleFields);
|
|
892
|
-
if (!!!couldApplyIncentive)
|
|
893
|
-
return;
|
|
894
|
-
var incentiveCalculatedAmount = (0, discount_1.getAfterTaxesPromosTotalAmount)(cartData, true);
|
|
895
|
-
return incentiveCalculatedAmount;
|
|
896
|
-
};
|
|
897
|
-
(0, react_1.useEffect)(function () {
|
|
898
|
-
if (!!!locale || !!!cartData || cartData.locale === locale)
|
|
899
|
-
return;
|
|
900
|
-
saveCart({ locale: locale });
|
|
901
|
-
}, [locale]);
|
|
902
|
-
(0, react_1.useEffect)(function () {
|
|
903
|
-
if (!!!sdk || !userLoaded)
|
|
904
|
-
return;
|
|
905
|
-
var checkCartCustomerValid = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
906
|
-
var customerId, customerEmail, customerDoc;
|
|
907
|
-
return __generator(this, function (_a) {
|
|
908
|
-
switch (_a.label) {
|
|
909
|
-
case 0:
|
|
910
|
-
if (!(!isUserLoggedIn && !!cartData)) return [3, 2];
|
|
911
|
-
customerId = cartData.customer_id;
|
|
912
|
-
customerEmail = !!cartData.customer_info ? cartData.customer_info.customer_default_email : '';
|
|
913
|
-
customerDoc = !!cartData.customer_info ? cartData.customer_info.pro_proof_doc : '';
|
|
914
|
-
if (!(!!customerId || !!customerEmail || !!customerDoc)) return [3, 2];
|
|
915
|
-
return [4, emptyCart()];
|
|
916
|
-
case 1:
|
|
917
|
-
_a.sent();
|
|
918
|
-
_a.label = 2;
|
|
919
|
-
case 2: return [2];
|
|
920
|
-
}
|
|
921
|
-
});
|
|
922
|
-
}); };
|
|
923
|
-
checkCartCustomerValid();
|
|
924
|
-
}, [sdk, isUserLoggedIn, userLoaded, cartData]);
|
|
925
|
-
(0, react_1.useEffect)(function () {
|
|
926
|
-
if (!!!api)
|
|
927
|
-
return;
|
|
928
|
-
var getContent = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
929
|
-
var cartData;
|
|
930
|
-
return __generator(this, function (_a) {
|
|
931
|
-
switch (_a.label) {
|
|
932
|
-
case 0: return [4, initCart()];
|
|
933
|
-
case 1:
|
|
934
|
-
cartData = _a.sent();
|
|
935
|
-
if (!!!cartData)
|
|
936
|
-
return [2];
|
|
937
|
-
setCartData(cartData);
|
|
938
|
-
return [2];
|
|
939
|
-
}
|
|
940
|
-
});
|
|
941
|
-
}); };
|
|
942
|
-
getContent();
|
|
943
|
-
}, [sdk]);
|
|
944
|
-
return ((0, jsx_runtime_1.jsx)(_1.default.Provider, __assign({ value: {
|
|
945
|
-
loading: loading,
|
|
946
|
-
cartData: cartData,
|
|
947
|
-
cartErrors: cartErrors,
|
|
948
|
-
cartSliderOpen: cartSliderOpen,
|
|
949
|
-
isGuest: isGuest,
|
|
950
|
-
setIsGuest: setIsGuest,
|
|
951
|
-
setCartErrors: setCartErrors,
|
|
952
|
-
setCartData: setCartData,
|
|
953
|
-
saveCart: saveCart,
|
|
954
|
-
setCartLoading: setLoading,
|
|
955
|
-
getCartFromDatabase: getCartFromDatabase,
|
|
956
|
-
handlePayment: handlePayment,
|
|
957
|
-
setCartSliderOpen: setCartSliderOpen,
|
|
958
|
-
addItem: addItem,
|
|
959
|
-
deleteItem: deleteItem,
|
|
960
|
-
updateItem: updateItem,
|
|
961
|
-
verifyCart: verifyCart,
|
|
962
|
-
uploadFileToOrder: uploadFileToOrder,
|
|
963
|
-
updateEmail: updateEmail,
|
|
964
|
-
updateDefaultCustomer: updateDefaultCustomer,
|
|
965
|
-
emptyCart: emptyCart,
|
|
966
|
-
saveOrder: saveOrder,
|
|
967
|
-
updateShippingAddress: updateShippingAddress,
|
|
968
|
-
updateBillingAddress: updateBillingAddress,
|
|
969
|
-
getIsSameBillingThenShipping: getIsSameBillingThenShipping,
|
|
970
|
-
setShippingPayload: setShippingPayload,
|
|
971
|
-
removeDiscountRule: removeDiscountRule,
|
|
972
|
-
getShippingQuotes: getShippingQuotes,
|
|
973
|
-
selectShippingQuote: selectShippingQuote,
|
|
974
|
-
setPromoCode: setPromoCode,
|
|
975
|
-
getApplicablePromos: getApplicablePromos,
|
|
976
|
-
canBePaidWithPA: canBePaidWithPA,
|
|
977
|
-
getPaymentIncentiveDiscountAmount: getPaymentIncentiveDiscountAmount,
|
|
978
|
-
setPaidBy: setPaidBy
|
|
979
|
-
} }, { children: children })));
|
|
980
|
-
};
|
|
981
|
-
exports.default = CartContextProvider;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
54
|
+
var react_1 = require("react");
|
|
55
|
+
var Cart_1 = require("addio-admin-sdk/dist/Interfaces/Cart");
|
|
56
|
+
var ISnackbarContext_1 = require("../SnackbarContext/interfaces/ISnackbarContext");
|
|
57
|
+
var _1 = __importDefault(require("."));
|
|
58
|
+
var useSnackbar_1 = __importDefault(require("../../hooks/useSnackbar"));
|
|
59
|
+
var useSDK_1 = __importDefault(require("../../hooks/useSDK"));
|
|
60
|
+
var useData_1 = __importDefault(require("../../hooks/useData"));
|
|
61
|
+
var useUser_1 = __importDefault(require("../../hooks/useUser"));
|
|
62
|
+
var functions_1 = require("../../../../utils/functions");
|
|
63
|
+
var functions_2 = require("../../../../utils/functions");
|
|
64
|
+
var cart_1 = require("addio-admin-sdk/dist/utils/cart");
|
|
65
|
+
var discount_1 = require("addio-admin-sdk/dist/utils/discount");
|
|
66
|
+
var g2_1 = require("addio-admin-sdk/dist/rules/GR/utils/g2");
|
|
67
|
+
var CartContextProvider = function (_a) {
|
|
68
|
+
var locale = _a.locale, children = _a.children;
|
|
69
|
+
var sdk = (0, useSDK_1.default)();
|
|
70
|
+
var currency = (0, useData_1.default)().currency;
|
|
71
|
+
var _b = (0, useUser_1.default)(), isUserLoggedIn = _b.isUserLoggedIn, userLoaded = _b.loaded;
|
|
72
|
+
var setSnackbar = (0, useSnackbar_1.default)().setSnackbar;
|
|
73
|
+
var _c = (0, react_1.useState)(), cartData = _c[0], setCartData = _c[1];
|
|
74
|
+
var _d = (0, react_1.useState)([]), cartErrors = _d[0], setCartErrors = _d[1];
|
|
75
|
+
var _e = (0, react_1.useState)(false), loading = _e[0], setLoading = _e[1];
|
|
76
|
+
var _f = (0, react_1.useState)(false), cartSliderOpen = _f[0], setCartSliderOpen = _f[1];
|
|
77
|
+
var _g = (0, react_1.useState)(), isGuest = _g[0], setIsGuest = _g[1];
|
|
78
|
+
var api = !!sdk ? sdk.getAPIInfos() : null;
|
|
79
|
+
var initCart = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
80
|
+
var cartIDInLocalStorage, newCart, _2;
|
|
81
|
+
return __generator(this, function (_a) {
|
|
82
|
+
switch (_a.label) {
|
|
83
|
+
case 0:
|
|
84
|
+
if (!!!api)
|
|
85
|
+
return [2];
|
|
86
|
+
cartIDInLocalStorage = localStorage.getItem("".concat(api.org, "__cart"));
|
|
87
|
+
if (!!!cartIDInLocalStorage) return [3, 4];
|
|
88
|
+
_a.label = 1;
|
|
89
|
+
case 1:
|
|
90
|
+
_a.trys.push([1, 3, , 4]);
|
|
91
|
+
return [4, getCartFromDatabase(cartIDInLocalStorage)];
|
|
92
|
+
case 2:
|
|
93
|
+
newCart = _a.sent();
|
|
94
|
+
if (!!newCart && !!!newCart.clerk_id) {
|
|
95
|
+
if (newCart.status < Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS) {
|
|
96
|
+
return [2, newCart];
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
localStorage.removeItem("".concat(api.org, "__cart"));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return [3, 4];
|
|
103
|
+
case 3:
|
|
104
|
+
_2 = _a.sent();
|
|
105
|
+
return [2, undefined];
|
|
106
|
+
case 4: return [2, undefined];
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}); };
|
|
110
|
+
var updateEmail = function (newCart, email, saveToDatabase) {
|
|
111
|
+
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
112
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
113
|
+
var cart;
|
|
114
|
+
return __generator(this, function (_a) {
|
|
115
|
+
switch (_a.label) {
|
|
116
|
+
case 0:
|
|
117
|
+
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
118
|
+
if (!!!saveToDatabase) return [3, 2];
|
|
119
|
+
return [4, saveCart({ user_email: email })];
|
|
120
|
+
case 1:
|
|
121
|
+
cart = _a.sent();
|
|
122
|
+
return [3, 3];
|
|
123
|
+
case 2:
|
|
124
|
+
if (!!cart) {
|
|
125
|
+
cart = __assign(__assign({}, cart), { user_email: email });
|
|
126
|
+
}
|
|
127
|
+
_a.label = 3;
|
|
128
|
+
case 3: return [2, cart];
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
var updateDefaultCustomer = function (newCart, email, id, pro_info, saveToDatabase) {
|
|
134
|
+
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
135
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
136
|
+
var cart, newCustomerInfo;
|
|
137
|
+
return __generator(this, function (_a) {
|
|
138
|
+
switch (_a.label) {
|
|
139
|
+
case 0:
|
|
140
|
+
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
141
|
+
newCustomerInfo = {
|
|
142
|
+
customer_id: id,
|
|
143
|
+
user_email: email,
|
|
144
|
+
customer_info: __assign(__assign(__assign(__assign(__assign(__assign({ customer_default_email: email }, (!!pro_info && pro_info.pro_proof_doc ? { pro_proof_doc: pro_info.pro_proof_doc } : {})), (!!pro_info && pro_info.pro_title ? { pro_title: pro_info.pro_title } : {})), (!!pro_info && pro_info.customer_external_code
|
|
145
|
+
? { customer_external_code: pro_info.customer_external_code }
|
|
146
|
+
: {})), (!!pro_info && pro_info.customer_external_group
|
|
147
|
+
? { customer_external_group: pro_info.customer_external_group }
|
|
148
|
+
: {})), (!!pro_info && pro_info.terms_code ? { terms_code: pro_info.terms_code } : {})), (!!pro_info && pro_info.company_name ? { company_name: pro_info.company_name } : {}))
|
|
149
|
+
};
|
|
150
|
+
if (!!!saveToDatabase) return [3, 2];
|
|
151
|
+
return [4, saveCart(newCustomerInfo)];
|
|
152
|
+
case 1:
|
|
153
|
+
_a.sent();
|
|
154
|
+
return [3, 3];
|
|
155
|
+
case 2:
|
|
156
|
+
cart = __assign(__assign({}, (!!cart ? cart : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB))), newCustomerInfo);
|
|
157
|
+
_a.label = 3;
|
|
158
|
+
case 3: return [2, cart];
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
var _handleInventoryError = function (shouldSave) { return __awaiter(void 0, void 0, void 0, function () {
|
|
164
|
+
return __generator(this, function (_a) {
|
|
165
|
+
switch (_a.label) {
|
|
166
|
+
case 0:
|
|
167
|
+
setSnackbar({
|
|
168
|
+
title: (0, functions_1.getStringByLang)({
|
|
169
|
+
en: 'This item is unavailable, or you haved reached the maximum amount you can add to your cart.',
|
|
170
|
+
fr: 'Cet item est indisponible, ou vous avez atteint la quantité maximale déjà au panier.'
|
|
171
|
+
}, locale),
|
|
172
|
+
type: ISnackbarContext_1.SnackbarTypeEnum.ERROR,
|
|
173
|
+
isShown: true,
|
|
174
|
+
isCart: true
|
|
175
|
+
});
|
|
176
|
+
if (!!!shouldSave) return [3, 2];
|
|
177
|
+
return [4, saveCart(undefined)];
|
|
178
|
+
case 1:
|
|
179
|
+
_a.sent();
|
|
180
|
+
_a.label = 2;
|
|
181
|
+
case 2: return [2];
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}); };
|
|
185
|
+
var addItem = function (newCart, items, retro, useParentName, saveToDatabase, defaultAdd, isServiceUpsellOfSku, customPriceList) {
|
|
186
|
+
if (retro === void 0) { retro = true; }
|
|
187
|
+
if (useParentName === void 0) { useParentName = false; }
|
|
188
|
+
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
189
|
+
if (defaultAdd === void 0) { defaultAdd = false; }
|
|
190
|
+
if (isServiceUpsellOfSku === void 0) { isServiceUpsellOfSku = false; }
|
|
191
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
192
|
+
var cart, addedValues, i, productName;
|
|
193
|
+
return __generator(this, function (_a) {
|
|
194
|
+
switch (_a.label) {
|
|
195
|
+
case 0:
|
|
196
|
+
if (!!!api || !!!sdk)
|
|
197
|
+
throw new Error('SDK not initialised');
|
|
198
|
+
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
199
|
+
if (!!isGuest && typeof isGuest === 'string' && !!cart.user_email) {
|
|
200
|
+
cart = __assign(__assign({}, cart), { user_email: isGuest });
|
|
201
|
+
}
|
|
202
|
+
return [4, (0, cart_1.addCartItems)(cart, Array.isArray(items) ? items : [items], sdk.products.getOneInventoryItem, api.org !== 'gr', _handleInventoryError, defaultAdd, isServiceUpsellOfSku, customPriceList)];
|
|
203
|
+
case 1:
|
|
204
|
+
addedValues = _a.sent();
|
|
205
|
+
cart = addedValues.cartData;
|
|
206
|
+
if (!!!saveToDatabase) return [3, 3];
|
|
207
|
+
return [4, saveCart(cart)];
|
|
208
|
+
case 2:
|
|
209
|
+
cart = _a.sent();
|
|
210
|
+
_a.label = 3;
|
|
211
|
+
case 3:
|
|
212
|
+
if (!!retro && !!items && (Array.isArray(items) ? !!items.length : true)) {
|
|
213
|
+
i = Array.isArray(items) ? items[0] : items;
|
|
214
|
+
productName = useParentName ? i.parent_name : i.name;
|
|
215
|
+
if (!!addedValues.updated) {
|
|
216
|
+
setSnackbar({
|
|
217
|
+
title: (0, functions_1.getStringByLang)({
|
|
218
|
+
en: "".concat(!!productName ? (0, functions_1.translate)(locale || 'en', productName) : 'The item', " has been successfuly updated in your cart!"),
|
|
219
|
+
fr: "".concat(!!productName ? (0, functions_1.translate)(locale || 'fr', productName) : "L'item", " a bien \u00E9t\u00E9 mis \u00E0 jour dans votre panier!")
|
|
220
|
+
}, locale),
|
|
221
|
+
type: ISnackbarContext_1.SnackbarTypeEnum.SUCCESS,
|
|
222
|
+
isShown: true,
|
|
223
|
+
isCart: true
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
else if (!!addedValues.added) {
|
|
227
|
+
setSnackbar({
|
|
228
|
+
title: (0, functions_1.getStringByLang)({
|
|
229
|
+
en: "".concat(!!productName ? (0, functions_1.translate)(locale || 'en', productName) : 'The item', " has been successfuly added to your cart!"),
|
|
230
|
+
fr: "".concat(!!productName ? (0, functions_1.translate)(locale || 'fr', productName) : "L'item", " a bien \u00E9t\u00E9 ajout\u00E9 \u00E0 votre panier!")
|
|
231
|
+
}, locale),
|
|
232
|
+
type: ISnackbarContext_1.SnackbarTypeEnum.SUCCESS,
|
|
233
|
+
isShown: true,
|
|
234
|
+
isCart: true
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
if (!!!addedValues.maxQtyItems) {
|
|
238
|
+
setCartSliderOpen(true);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return [2, cart];
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
var updateItem = function (newCart, items, saveToDatabase) {
|
|
247
|
+
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
248
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
249
|
+
var updatedItemsCart, itemsToUpdate, updatedValues;
|
|
250
|
+
return __generator(this, function (_a) {
|
|
251
|
+
switch (_a.label) {
|
|
252
|
+
case 0:
|
|
253
|
+
if (!!!api || !!!sdk)
|
|
254
|
+
throw new Error('SDK not initialised');
|
|
255
|
+
updatedItemsCart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
256
|
+
itemsToUpdate = Array.isArray(items) ? items : [items];
|
|
257
|
+
return [4, (0, cart_1.updateCartItems)(updatedItemsCart, itemsToUpdate, sdk.products.getOneInventoryItem, api.org !== 'gr', _handleInventoryError)];
|
|
258
|
+
case 1:
|
|
259
|
+
updatedValues = _a.sent();
|
|
260
|
+
if (!!!saveToDatabase) return [3, 3];
|
|
261
|
+
return [4, saveCart({ items: updatedValues.updatedCart.items })];
|
|
262
|
+
case 2:
|
|
263
|
+
updatedItemsCart = _a.sent();
|
|
264
|
+
_a.label = 3;
|
|
265
|
+
case 3: return [2, updatedItemsCart];
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
};
|
|
270
|
+
var deleteItem = function (newCart, sku, saveToDatabase, onlyIfUpsellOf) {
|
|
271
|
+
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
272
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
273
|
+
var cart;
|
|
274
|
+
return __generator(this, function (_a) {
|
|
275
|
+
switch (_a.label) {
|
|
276
|
+
case 0:
|
|
277
|
+
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
278
|
+
cart = (0, cart_1.deleteCartItem)(cart, Array.isArray(sku) ? sku : [sku], onlyIfUpsellOf);
|
|
279
|
+
if (!!!saveToDatabase) return [3, 2];
|
|
280
|
+
return [4, saveCart({ items: cart.items })];
|
|
281
|
+
case 1:
|
|
282
|
+
cart = _a.sent();
|
|
283
|
+
_a.label = 2;
|
|
284
|
+
case 2: return [2, cart];
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
};
|
|
289
|
+
var verifyCart = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
290
|
+
var request, responseData, response, error_1;
|
|
291
|
+
return __generator(this, function (_a) {
|
|
292
|
+
switch (_a.label) {
|
|
293
|
+
case 0:
|
|
294
|
+
if (!!!api || !!!cartData)
|
|
295
|
+
return [2, false];
|
|
296
|
+
if (cartData.status >= Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS)
|
|
297
|
+
throw new Error("Can't process a paid or higher status cart.");
|
|
298
|
+
if (!!!cartData.id) return [3, 5];
|
|
299
|
+
_a.label = 1;
|
|
300
|
+
case 1:
|
|
301
|
+
_a.trys.push([1, 4, , 5]);
|
|
302
|
+
return [4, fetch("".concat(api.api, "/cart/").concat(cartData.id, "/verify"), {
|
|
303
|
+
method: 'GET',
|
|
304
|
+
headers: {
|
|
305
|
+
Authorization: "Bearer ".concat(api.token),
|
|
306
|
+
'Content-Type': 'application/json',
|
|
307
|
+
'x-gng-ecomm-token': api.userToken
|
|
308
|
+
}
|
|
309
|
+
})];
|
|
310
|
+
case 2:
|
|
311
|
+
request = _a.sent();
|
|
312
|
+
return [4, request.json()];
|
|
313
|
+
case 3:
|
|
314
|
+
responseData = (_a.sent());
|
|
315
|
+
if (responseData.status != 200) {
|
|
316
|
+
throw 'Erreur lors de la requête des quotes.' + responseData.error;
|
|
317
|
+
}
|
|
318
|
+
response = responseData.data;
|
|
319
|
+
setCartData(response.cart);
|
|
320
|
+
return [2, response.actionsDone];
|
|
321
|
+
case 4:
|
|
322
|
+
error_1 = _a.sent();
|
|
323
|
+
console.log('error in verifyCart: ', error_1);
|
|
324
|
+
return [3, 5];
|
|
325
|
+
case 5: return [2, false];
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
}); };
|
|
329
|
+
var uploadFileToOrder = function (newCart, file, propsName, saveToDatabase) {
|
|
330
|
+
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
331
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
332
|
+
var cart, base64, request, responseData;
|
|
333
|
+
var _a, _b, _c, _d;
|
|
334
|
+
return __generator(this, function (_e) {
|
|
335
|
+
switch (_e.label) {
|
|
336
|
+
case 0:
|
|
337
|
+
if (!!!api)
|
|
338
|
+
return [2];
|
|
339
|
+
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
340
|
+
if (!!!cart.id)
|
|
341
|
+
return [2, {
|
|
342
|
+
cart: cart,
|
|
343
|
+
data: undefined,
|
|
344
|
+
error: "Couldn't convert cart data"
|
|
345
|
+
}];
|
|
346
|
+
if (cart.status >= Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS)
|
|
347
|
+
throw new Error("Can't process a paid or higher status cart.");
|
|
348
|
+
return [4, (0, functions_2.toBase64)(file)];
|
|
349
|
+
case 1:
|
|
350
|
+
base64 = _e.sent();
|
|
351
|
+
if (typeof base64 !== 'string') {
|
|
352
|
+
return [2, {
|
|
353
|
+
cart: cart,
|
|
354
|
+
data: undefined,
|
|
355
|
+
error: "Couldn't convert file to base64"
|
|
356
|
+
}];
|
|
357
|
+
}
|
|
358
|
+
return [4, fetch("".concat(api.api, "/cart/").concat(cart.id, "/uploadBase64"), {
|
|
359
|
+
method: 'PUT',
|
|
360
|
+
headers: {
|
|
361
|
+
Authorization: "Bearer ".concat(api.token),
|
|
362
|
+
'Content-Type': 'application/json',
|
|
363
|
+
'x-gng-ecomm-token': api.userToken
|
|
364
|
+
},
|
|
365
|
+
body: JSON.stringify({
|
|
366
|
+
base64: base64
|
|
367
|
+
})
|
|
368
|
+
})];
|
|
369
|
+
case 2:
|
|
370
|
+
request = _e.sent();
|
|
371
|
+
return [4, request.json()];
|
|
372
|
+
case 3:
|
|
373
|
+
responseData = (_e.sent());
|
|
374
|
+
if (responseData.status != 200) {
|
|
375
|
+
throw new Error("Impossible d'uploader le fichier" + responseData.error);
|
|
376
|
+
}
|
|
377
|
+
if (!!!responseData.data) return [3, 6];
|
|
378
|
+
if (!!!saveToDatabase) return [3, 5];
|
|
379
|
+
return [4, saveCart({
|
|
380
|
+
shipping_payload: cart.shipping_payload
|
|
381
|
+
? __assign(__assign({}, cart.shipping_payload), (_a = {}, _a[propsName] = responseData.data, _a)) : (_b = {},
|
|
382
|
+
_b[propsName] = responseData.data,
|
|
383
|
+
_b)
|
|
384
|
+
})];
|
|
385
|
+
case 4:
|
|
386
|
+
cart = _e.sent();
|
|
387
|
+
return [3, 6];
|
|
388
|
+
case 5:
|
|
389
|
+
cart = __assign(__assign({}, cart), { shipping_payload: cart.shipping_payload
|
|
390
|
+
? __assign(__assign({}, cart.shipping_payload), (_c = {}, _c[propsName] = responseData.data, _c)) : (_d = {},
|
|
391
|
+
_d[propsName] = responseData.data,
|
|
392
|
+
_d) });
|
|
393
|
+
_e.label = 6;
|
|
394
|
+
case 6: return [2, { cart: cart, data: responseData.data, error: undefined }];
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
};
|
|
399
|
+
var emptyCart = function (removeCartIDFromLocalStorage) {
|
|
400
|
+
if (removeCartIDFromLocalStorage === void 0) { removeCartIDFromLocalStorage = true; }
|
|
401
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
402
|
+
var cartID, cartFromDB, err_1;
|
|
403
|
+
return __generator(this, function (_a) {
|
|
404
|
+
switch (_a.label) {
|
|
405
|
+
case 0:
|
|
406
|
+
if (!!!api || !!!sdk)
|
|
407
|
+
return [2, false];
|
|
408
|
+
if (!!!removeCartIDFromLocalStorage) return [3, 1];
|
|
409
|
+
if (process.env.NODE_ENV !== 'production')
|
|
410
|
+
console.log('`${api.org}__cart`', "".concat(api.org, "__cart"));
|
|
411
|
+
localStorage.removeItem("".concat(api.org, "__cart"));
|
|
412
|
+
setCartData(undefined);
|
|
413
|
+
return [3, 5];
|
|
414
|
+
case 1:
|
|
415
|
+
cartID = localStorage.getItem("".concat(api.org, "__cart"));
|
|
416
|
+
if (!!!cartID) return [3, 5];
|
|
417
|
+
_a.label = 2;
|
|
418
|
+
case 2:
|
|
419
|
+
_a.trys.push([2, 4, , 5]);
|
|
420
|
+
return [4, getCartFromDatabase(cartID)];
|
|
421
|
+
case 3:
|
|
422
|
+
cartFromDB = _a.sent();
|
|
423
|
+
if (!!cartFromDB && !!!cartFromDB.clerk_id) {
|
|
424
|
+
setCartData(cartFromDB);
|
|
425
|
+
}
|
|
426
|
+
return [3, 5];
|
|
427
|
+
case 4:
|
|
428
|
+
err_1 = _a.sent();
|
|
429
|
+
return [3, 5];
|
|
430
|
+
case 5: return [2, true];
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
};
|
|
435
|
+
var handlePayment = function (data) {
|
|
436
|
+
if (data === void 0) { data = {}; }
|
|
437
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
438
|
+
var request, responseData;
|
|
439
|
+
return __generator(this, function (_a) {
|
|
440
|
+
switch (_a.label) {
|
|
441
|
+
case 0:
|
|
442
|
+
if (!!!api || !!!sdk)
|
|
443
|
+
return [2, { error: { en: 'SDK not initialized', fr: 'SDK non initialisé' } }];
|
|
444
|
+
if (!!!cartData)
|
|
445
|
+
throw new Error("Can't pay an empty/undefined cart");
|
|
446
|
+
if (cartData.status >= Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS)
|
|
447
|
+
throw new Error("Can't process a paid or higher status cart.");
|
|
448
|
+
return [4, fetch("".concat(api.api, "/cart/").concat(cartData.id, "/payment"), {
|
|
449
|
+
method: 'POST',
|
|
450
|
+
headers: {
|
|
451
|
+
Authorization: "Bearer ".concat(api.token),
|
|
452
|
+
'Content-Type': 'application/json',
|
|
453
|
+
'x-gng-ecomm-token': api.userToken
|
|
454
|
+
},
|
|
455
|
+
body: JSON.stringify(__assign({ customer_email: cartData.user_email, customer_name: cartData.billing_address ? cartData.billing_address.expedition_recipient_name : 'Anonyme', cartTotal: Number((cartData.total * 100).toFixed(0)) }, data))
|
|
456
|
+
})];
|
|
457
|
+
case 1:
|
|
458
|
+
request = _a.sent();
|
|
459
|
+
return [4, request.json()];
|
|
460
|
+
case 2:
|
|
461
|
+
responseData = _a.sent();
|
|
462
|
+
if (responseData.status != 200) {
|
|
463
|
+
return [2, { error: responseData.error }];
|
|
464
|
+
}
|
|
465
|
+
if (!!responseData.token) {
|
|
466
|
+
sdk.setUserToken(responseData.token);
|
|
467
|
+
}
|
|
468
|
+
return [2, { accessToken: responseData.data }];
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
});
|
|
472
|
+
};
|
|
473
|
+
var setPaidBy = function (paidByValue, customPriceList) { return __awaiter(void 0, void 0, void 0, function () {
|
|
474
|
+
var partial, customIncentiveInfo, e_1;
|
|
475
|
+
return __generator(this, function (_a) {
|
|
476
|
+
switch (_a.label) {
|
|
477
|
+
case 0:
|
|
478
|
+
if (!!!cartData)
|
|
479
|
+
return [2];
|
|
480
|
+
partial = { paid_by: paidByValue, id: cartData.id };
|
|
481
|
+
if (!!customPriceList) {
|
|
482
|
+
customIncentiveInfo = (0, g2_1.getCustomIncentiveInfo)(customPriceList, cartData.discount_rules);
|
|
483
|
+
if (!!customIncentiveInfo) {
|
|
484
|
+
partial = __assign(__assign({}, partial), { discount_rules: customIncentiveInfo.updatedDiscountRules });
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
_a.label = 1;
|
|
488
|
+
case 1:
|
|
489
|
+
_a.trys.push([1, 3, , 4]);
|
|
490
|
+
return [4, saveCart(partial, false, undefined, false, true)];
|
|
491
|
+
case 2:
|
|
492
|
+
_a.sent();
|
|
493
|
+
return [3, 4];
|
|
494
|
+
case 3:
|
|
495
|
+
e_1 = _a.sent();
|
|
496
|
+
console.log('error on saving cart with paid_by value: ', e_1);
|
|
497
|
+
return [3, 4];
|
|
498
|
+
case 4: return [2];
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
}); };
|
|
502
|
+
var saveOrder = function (payment_payload) { return __awaiter(void 0, void 0, void 0, function () {
|
|
503
|
+
var fetchData, response, err_2;
|
|
504
|
+
return __generator(this, function (_a) {
|
|
505
|
+
switch (_a.label) {
|
|
506
|
+
case 0:
|
|
507
|
+
if (!!!api)
|
|
508
|
+
throw new Error('SDK not initialised');
|
|
509
|
+
if (!!!cartData)
|
|
510
|
+
throw new Error("Can't pay an empty/undefined cart");
|
|
511
|
+
if (cartData.status >= Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS)
|
|
512
|
+
throw new Error("Can't process a paid or higher status cart.");
|
|
513
|
+
_a.label = 1;
|
|
514
|
+
case 1:
|
|
515
|
+
_a.trys.push([1, 4, , 5]);
|
|
516
|
+
return [4, fetch("".concat(api.api, "/cart/").concat(cartData.id, "/saveOrder"), {
|
|
517
|
+
method: 'POST',
|
|
518
|
+
headers: {
|
|
519
|
+
authorization: "Bearer ".concat(api.token),
|
|
520
|
+
'Content-Type': 'application/json',
|
|
521
|
+
'x-gng-ecomm-token': api.userToken
|
|
522
|
+
},
|
|
523
|
+
body: JSON.stringify({ payment_payload: payment_payload })
|
|
524
|
+
})];
|
|
525
|
+
case 2:
|
|
526
|
+
fetchData = _a.sent();
|
|
527
|
+
return [4, fetchData.json()];
|
|
528
|
+
case 3:
|
|
529
|
+
response = (_a.sent());
|
|
530
|
+
if (response.status !== 200) {
|
|
531
|
+
return [2, 'error'];
|
|
532
|
+
}
|
|
533
|
+
return [2, response.data];
|
|
534
|
+
case 4:
|
|
535
|
+
err_2 = _a.sent();
|
|
536
|
+
return [2, 'error'];
|
|
537
|
+
case 5: return [2];
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
}); };
|
|
541
|
+
var removeDiscountRule = function (cart, code) { return __awaiter(void 0, void 0, void 0, function () {
|
|
542
|
+
var e_2;
|
|
543
|
+
return __generator(this, function (_a) {
|
|
544
|
+
switch (_a.label) {
|
|
545
|
+
case 0:
|
|
546
|
+
_a.trys.push([0, 2, , 3]);
|
|
547
|
+
return [4, saveCart({
|
|
548
|
+
subtotalWithoutPromotions: null,
|
|
549
|
+
cart_promo_applied: null,
|
|
550
|
+
cart_promos_applied: !!cart.cart_promos_applied
|
|
551
|
+
? cart.cart_promos_applied.filter(function (x) { return x.promo_code !== code; })
|
|
552
|
+
: null
|
|
553
|
+
})];
|
|
554
|
+
case 1:
|
|
555
|
+
_a.sent();
|
|
556
|
+
return [2, true];
|
|
557
|
+
case 2:
|
|
558
|
+
e_2 = _a.sent();
|
|
559
|
+
console.log('error on removing discountRule: ', e_2.toString());
|
|
560
|
+
return [2, false];
|
|
561
|
+
case 3: return [2];
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
}); };
|
|
565
|
+
var updateBillingAddress = function (newCart, expeditionInfo, saveToDatabase, setCAIfNoCountry) {
|
|
566
|
+
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
567
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
568
|
+
var cart, newExpeditionInfo;
|
|
569
|
+
return __generator(this, function (_a) {
|
|
570
|
+
switch (_a.label) {
|
|
571
|
+
case 0:
|
|
572
|
+
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
573
|
+
newExpeditionInfo = expeditionInfo;
|
|
574
|
+
if (!!setCAIfNoCountry && !!expeditionInfo && !!!expeditionInfo.address.country) {
|
|
575
|
+
newExpeditionInfo = __assign(__assign({}, expeditionInfo), { address: __assign(__assign({}, expeditionInfo.address), { country: 'CA' }) });
|
|
576
|
+
}
|
|
577
|
+
if (!!!saveToDatabase) return [3, 2];
|
|
578
|
+
return [4, saveCart({ billing_address: newExpeditionInfo })];
|
|
579
|
+
case 1:
|
|
580
|
+
cart = _a.sent();
|
|
581
|
+
return [3, 3];
|
|
582
|
+
case 2:
|
|
583
|
+
cart = __assign(__assign({}, cart), { billing_address: newExpeditionInfo });
|
|
584
|
+
_a.label = 3;
|
|
585
|
+
case 3: return [2, cart];
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
});
|
|
589
|
+
};
|
|
590
|
+
var updateShippingAddress = function (newCart, isPickupAtStore, expeditionInfo, saveToDatabase, setCAIfNoCountry) {
|
|
591
|
+
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
592
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
593
|
+
var cart, newExpeditionInfo;
|
|
594
|
+
return __generator(this, function (_a) {
|
|
595
|
+
switch (_a.label) {
|
|
596
|
+
case 0:
|
|
597
|
+
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
598
|
+
newExpeditionInfo = expeditionInfo;
|
|
599
|
+
if (!!setCAIfNoCountry && !!expeditionInfo && !!!expeditionInfo.address.country) {
|
|
600
|
+
newExpeditionInfo = __assign(__assign({}, expeditionInfo), { address: __assign(__assign({}, expeditionInfo.address), { country: 'CA' }) });
|
|
601
|
+
}
|
|
602
|
+
if (!!!saveToDatabase) return [3, 2];
|
|
603
|
+
return [4, saveCart({
|
|
604
|
+
is_pickup: isPickupAtStore,
|
|
605
|
+
shipping_address: newExpeditionInfo,
|
|
606
|
+
shipping_selected: undefined
|
|
607
|
+
})];
|
|
608
|
+
case 1:
|
|
609
|
+
cart = _a.sent();
|
|
610
|
+
return [3, 3];
|
|
611
|
+
case 2:
|
|
612
|
+
cart = __assign(__assign({}, cart), { is_pickup: isPickupAtStore, shipping_address: newExpeditionInfo, shipping_selected: undefined });
|
|
613
|
+
_a.label = 3;
|
|
614
|
+
case 3: return [2, cart];
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
});
|
|
618
|
+
};
|
|
619
|
+
var getIsSameBillingThenShipping = function () {
|
|
620
|
+
if (!!!cartData)
|
|
621
|
+
return false;
|
|
622
|
+
if (!!!cartData.billing_address && !!!cartData.shipping_address)
|
|
623
|
+
return false;
|
|
624
|
+
return (!!cartData.billing_address &&
|
|
625
|
+
!!cartData.shipping_address &&
|
|
626
|
+
(0, functions_1.string_to_slug)(cartData.billing_address.address.postal_code) ===
|
|
627
|
+
(0, functions_1.string_to_slug)(cartData.shipping_address.address.postal_code) &&
|
|
628
|
+
(0, functions_1.string_to_slug)(cartData.billing_address.address.line1) === (0, functions_1.string_to_slug)(cartData.shipping_address.address.line1));
|
|
629
|
+
};
|
|
630
|
+
var setShippingPayload = function (newCart, payload, saveToDatabase) {
|
|
631
|
+
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
632
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
633
|
+
var cart;
|
|
634
|
+
return __generator(this, function (_a) {
|
|
635
|
+
switch (_a.label) {
|
|
636
|
+
case 0:
|
|
637
|
+
cart = !!newCart ? newCart : !!cartData ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB);
|
|
638
|
+
if (!!!saveToDatabase) return [3, 2];
|
|
639
|
+
return [4, saveCart({ shipping_payload: payload })];
|
|
640
|
+
case 1:
|
|
641
|
+
cart = _a.sent();
|
|
642
|
+
return [3, 3];
|
|
643
|
+
case 2:
|
|
644
|
+
cart = __assign(__assign({}, cart), { shipping_payload: payload });
|
|
645
|
+
_a.label = 3;
|
|
646
|
+
case 3: return [2, cart];
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
});
|
|
650
|
+
};
|
|
651
|
+
var getShippingQuotes = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
652
|
+
var request, responseData;
|
|
653
|
+
return __generator(this, function (_a) {
|
|
654
|
+
switch (_a.label) {
|
|
655
|
+
case 0:
|
|
656
|
+
if (!!!api || !!!sdk)
|
|
657
|
+
return [2];
|
|
658
|
+
if (!!!cartData)
|
|
659
|
+
throw new Error("Can't get shipping quotes for an empty/undefined cart");
|
|
660
|
+
if (cartData.status >= Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS)
|
|
661
|
+
throw new Error("Can't process a paid or higher status cart.");
|
|
662
|
+
return [4, fetch("".concat(api.api, "/cart/getShippingQuotes"), {
|
|
663
|
+
method: 'POST',
|
|
664
|
+
headers: {
|
|
665
|
+
Authorization: "Bearer ".concat(api.token),
|
|
666
|
+
'x-gng-ecomm-token': api.userToken
|
|
667
|
+
},
|
|
668
|
+
body: cartData.id
|
|
669
|
+
})];
|
|
670
|
+
case 1:
|
|
671
|
+
request = _a.sent();
|
|
672
|
+
return [4, request.json()];
|
|
673
|
+
case 2:
|
|
674
|
+
responseData = (_a.sent());
|
|
675
|
+
if (responseData.status != 200 || !!responseData.error) {
|
|
676
|
+
throw 'Erreur lors de la requête des quotes.' + responseData.error;
|
|
677
|
+
}
|
|
678
|
+
if (responseData.token) {
|
|
679
|
+
sdk.setUserToken(responseData.token);
|
|
680
|
+
}
|
|
681
|
+
return [2, responseData.data.shippingQuotes];
|
|
682
|
+
}
|
|
683
|
+
});
|
|
684
|
+
}); };
|
|
685
|
+
var selectShippingQuote = function (quote) { return __awaiter(void 0, void 0, void 0, function () {
|
|
686
|
+
var request, responseData, updatedCart;
|
|
687
|
+
return __generator(this, function (_a) {
|
|
688
|
+
switch (_a.label) {
|
|
689
|
+
case 0:
|
|
690
|
+
if (!!!api || !!!sdk)
|
|
691
|
+
return [2];
|
|
692
|
+
if (!!!cartData)
|
|
693
|
+
throw new Error("Can't select shipping quote for an empty/undefined cart");
|
|
694
|
+
return [4, fetch("".concat(api.api, "/cart/").concat(cartData.id, "/setSelectedShippingMethod"), {
|
|
695
|
+
method: 'POST',
|
|
696
|
+
headers: {
|
|
697
|
+
Authorization: "Bearer ".concat(api.token),
|
|
698
|
+
'Content-Type': 'application/json',
|
|
699
|
+
'x-gng-ecomm-token': api.userToken
|
|
700
|
+
},
|
|
701
|
+
body: JSON.stringify(quote)
|
|
702
|
+
})];
|
|
703
|
+
case 1:
|
|
704
|
+
request = _a.sent();
|
|
705
|
+
return [4, request.json()];
|
|
706
|
+
case 2:
|
|
707
|
+
responseData = _a.sent();
|
|
708
|
+
if (responseData.status != 200) {
|
|
709
|
+
throw new Error('Impossible de sélectionner la shipping quote ' + responseData.error);
|
|
710
|
+
}
|
|
711
|
+
if (responseData.token) {
|
|
712
|
+
sdk.setUserToken(responseData.token);
|
|
713
|
+
}
|
|
714
|
+
updatedCart = responseData.data;
|
|
715
|
+
setCartData(updatedCart);
|
|
716
|
+
return [2, updatedCart];
|
|
717
|
+
}
|
|
718
|
+
});
|
|
719
|
+
}); };
|
|
720
|
+
var setPromoCode = function (promo_code) { return __awaiter(void 0, void 0, void 0, function () {
|
|
721
|
+
var error_2;
|
|
722
|
+
return __generator(this, function (_a) {
|
|
723
|
+
switch (_a.label) {
|
|
724
|
+
case 0:
|
|
725
|
+
_a.trys.push([0, 2, , 3]);
|
|
726
|
+
return [4, saveCart({}, true, promo_code)];
|
|
727
|
+
case 1:
|
|
728
|
+
_a.sent();
|
|
729
|
+
return [3, 3];
|
|
730
|
+
case 2:
|
|
731
|
+
error_2 = _a.sent();
|
|
732
|
+
if (process.env.NODE_ENV === 'development')
|
|
733
|
+
console.log("error on SDK while applying promo on cart", error_2);
|
|
734
|
+
return [3, 3];
|
|
735
|
+
case 3: return [2];
|
|
736
|
+
}
|
|
737
|
+
});
|
|
738
|
+
}); };
|
|
739
|
+
var getCartFromDatabase = function (cardID) { return __awaiter(void 0, void 0, void 0, function () {
|
|
740
|
+
var request, responseData;
|
|
741
|
+
return __generator(this, function (_a) {
|
|
742
|
+
switch (_a.label) {
|
|
743
|
+
case 0:
|
|
744
|
+
if (!!!api || !!!sdk)
|
|
745
|
+
return [2];
|
|
746
|
+
return [4, fetch("".concat(api.api, "/cart/").concat(cardID), {
|
|
747
|
+
method: 'GET',
|
|
748
|
+
headers: {
|
|
749
|
+
Authorization: "Bearer ".concat(api.token),
|
|
750
|
+
'Content-Type': 'application/json',
|
|
751
|
+
'x-gng-ecomm-token': api.userToken
|
|
752
|
+
}
|
|
753
|
+
})];
|
|
754
|
+
case 1:
|
|
755
|
+
request = _a.sent();
|
|
756
|
+
return [4, request.json()];
|
|
757
|
+
case 2:
|
|
758
|
+
responseData = _a.sent();
|
|
759
|
+
if (responseData.status !== 200) {
|
|
760
|
+
throw new Error('Impossible de fetch le cart: ' + responseData.error);
|
|
761
|
+
}
|
|
762
|
+
if (responseData.token) {
|
|
763
|
+
sdk.setUserToken(responseData.token);
|
|
764
|
+
}
|
|
765
|
+
return [2, responseData.data];
|
|
766
|
+
}
|
|
767
|
+
});
|
|
768
|
+
}); };
|
|
769
|
+
var saveCart = function (cart, saveToDatabase, promo_code, onlyNewCart, partialOnly) {
|
|
770
|
+
if (saveToDatabase === void 0) { saveToDatabase = true; }
|
|
771
|
+
if (onlyNewCart === void 0) { onlyNewCart = false; }
|
|
772
|
+
if (partialOnly === void 0) { partialOnly = false; }
|
|
773
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
774
|
+
var newCartData, response, responseData, cartIDInLocalStorage;
|
|
775
|
+
return __generator(this, function (_a) {
|
|
776
|
+
switch (_a.label) {
|
|
777
|
+
case 0:
|
|
778
|
+
newCartData = __assign(__assign({}, (!!cartData && !!!onlyNewCart ? cartData : (0, Cart_1.getDefaultCart)(Cart_1.CartTypeEnum.WEB))), cart);
|
|
779
|
+
if (!!!newCartData)
|
|
780
|
+
throw new Error("Can't save an empty/undefined cart");
|
|
781
|
+
if (newCartData.status >= Cart_1.CartStatusEnum.CHECKOUT_PAYMENT_SUCCESS)
|
|
782
|
+
throw new Error("Can't process a paid or higher status cart.");
|
|
783
|
+
if (!!!api) return [3, 3];
|
|
784
|
+
return [4, fetch("".concat(api.api, "/cart"), {
|
|
785
|
+
method: 'POST',
|
|
786
|
+
headers: {
|
|
787
|
+
Authorization: "Bearer ".concat(api.token),
|
|
788
|
+
'Content-Type': 'application/json',
|
|
789
|
+
'x-gng-ecomm-token': api.userToken
|
|
790
|
+
},
|
|
791
|
+
body: JSON.stringify({ cartData: newCartData, currency: currency, locale: locale, promo_code: promo_code, saveToDatabase: saveToDatabase, partialOnly: partialOnly })
|
|
792
|
+
})];
|
|
793
|
+
case 1:
|
|
794
|
+
response = _a.sent();
|
|
795
|
+
return [4, response.json()];
|
|
796
|
+
case 2:
|
|
797
|
+
responseData = _a.sent();
|
|
798
|
+
if (responseData.status != 200 || !!responseData.error) {
|
|
799
|
+
setCartErrors([
|
|
800
|
+
typeof responseData.error === 'string'
|
|
801
|
+
? responseData.error
|
|
802
|
+
: (0, functions_1.getStringByLang)(responseData.error, locale)
|
|
803
|
+
]);
|
|
804
|
+
}
|
|
805
|
+
else {
|
|
806
|
+
if (!!responseData.data) {
|
|
807
|
+
cartIDInLocalStorage = localStorage.getItem("".concat(api.org, "__cart"));
|
|
808
|
+
if (!!responseData.data.id && (!!!cartIDInLocalStorage || cartIDInLocalStorage !== responseData.data.id)) {
|
|
809
|
+
localStorage.setItem("".concat(api.org, "__cart"), responseData.data.id);
|
|
810
|
+
}
|
|
811
|
+
newCartData = responseData.data;
|
|
812
|
+
setCartData(newCartData);
|
|
813
|
+
setCartErrors([]);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
return [3, 4];
|
|
817
|
+
case 3:
|
|
818
|
+
setCartData(newCartData);
|
|
819
|
+
_a.label = 4;
|
|
820
|
+
case 4: return [2, newCartData];
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
});
|
|
824
|
+
};
|
|
825
|
+
var getApplicablePromos = function (forceShipping) { return __awaiter(void 0, void 0, void 0, function () {
|
|
826
|
+
var request, responseData;
|
|
827
|
+
return __generator(this, function (_a) {
|
|
828
|
+
switch (_a.label) {
|
|
829
|
+
case 0:
|
|
830
|
+
if (!!!api || !!!sdk)
|
|
831
|
+
return [2];
|
|
832
|
+
if (!!!cartData)
|
|
833
|
+
throw new Error("Can't get promos if no cart data");
|
|
834
|
+
return [4, fetch("".concat(api.api, "/cart/").concat(cartData.id, "/getApplicablePromosWithShipping"), {
|
|
835
|
+
method: 'GET',
|
|
836
|
+
headers: {
|
|
837
|
+
Authorization: "Bearer ".concat(api.token),
|
|
838
|
+
'Content-Type': 'application/json',
|
|
839
|
+
'x-gng-ecomm-token': api.userToken
|
|
840
|
+
}
|
|
841
|
+
})];
|
|
842
|
+
case 1:
|
|
843
|
+
request = _a.sent();
|
|
844
|
+
return [4, request.json()];
|
|
845
|
+
case 2:
|
|
846
|
+
responseData = _a.sent();
|
|
847
|
+
if (responseData.status !== 200) {
|
|
848
|
+
throw new Error('Impossible de fetch les promos associé: ' + responseData.error);
|
|
849
|
+
}
|
|
850
|
+
return [2, responseData.data];
|
|
851
|
+
}
|
|
852
|
+
});
|
|
853
|
+
}); };
|
|
854
|
+
var canBePaidWithPA = function (userId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
855
|
+
var request, responseData;
|
|
856
|
+
return __generator(this, function (_a) {
|
|
857
|
+
switch (_a.label) {
|
|
858
|
+
case 0:
|
|
859
|
+
if (!!!api)
|
|
860
|
+
return [2];
|
|
861
|
+
if (!!!cartData)
|
|
862
|
+
throw new Error("Can't check if cart is payable with PA if cartData is undefined.");
|
|
863
|
+
if (!!!userId)
|
|
864
|
+
throw new Error("Can't check if cart is payable with PA without userId");
|
|
865
|
+
return [4, fetch("".concat(api.api, "/cart/").concat(cartData.id, "/canBePaidWithPA"), {
|
|
866
|
+
method: 'POST',
|
|
867
|
+
headers: {
|
|
868
|
+
Authorization: "Bearer ".concat(api.token),
|
|
869
|
+
'Content-Type': 'application/json',
|
|
870
|
+
'x-gng-ecomm-token': api.userToken
|
|
871
|
+
},
|
|
872
|
+
body: JSON.stringify({ userId: userId })
|
|
873
|
+
})];
|
|
874
|
+
case 1:
|
|
875
|
+
request = _a.sent();
|
|
876
|
+
return [4, request.json()];
|
|
877
|
+
case 2:
|
|
878
|
+
responseData = _a.sent();
|
|
879
|
+
if (responseData.status !== 200) {
|
|
880
|
+
throw new Error("Can't apply promise to purchase to cart: " + responseData.error);
|
|
881
|
+
}
|
|
882
|
+
return [2, responseData.data];
|
|
883
|
+
}
|
|
884
|
+
});
|
|
885
|
+
}); };
|
|
886
|
+
var getPaymentIncentiveDiscountAmount = function (skipCheckForRuleFields) {
|
|
887
|
+
if (!!!cartData)
|
|
888
|
+
return;
|
|
889
|
+
if (!!cartData.promo_after_taxes_amount)
|
|
890
|
+
return cartData.promo_after_taxes_amount;
|
|
891
|
+
var couldApplyIncentive = (0, discount_1.checkIfCouldApplyIncentivePromo)(cartData, skipCheckForRuleFields);
|
|
892
|
+
if (!!!couldApplyIncentive)
|
|
893
|
+
return;
|
|
894
|
+
var incentiveCalculatedAmount = (0, discount_1.getAfterTaxesPromosTotalAmount)(cartData, true);
|
|
895
|
+
return incentiveCalculatedAmount;
|
|
896
|
+
};
|
|
897
|
+
(0, react_1.useEffect)(function () {
|
|
898
|
+
if (!!!locale || !!!cartData || cartData.locale === locale)
|
|
899
|
+
return;
|
|
900
|
+
saveCart({ locale: locale });
|
|
901
|
+
}, [locale]);
|
|
902
|
+
(0, react_1.useEffect)(function () {
|
|
903
|
+
if (!!!sdk || !userLoaded)
|
|
904
|
+
return;
|
|
905
|
+
var checkCartCustomerValid = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
906
|
+
var customerId, customerEmail, customerDoc;
|
|
907
|
+
return __generator(this, function (_a) {
|
|
908
|
+
switch (_a.label) {
|
|
909
|
+
case 0:
|
|
910
|
+
if (!(!isUserLoggedIn && !!cartData)) return [3, 2];
|
|
911
|
+
customerId = cartData.customer_id;
|
|
912
|
+
customerEmail = !!cartData.customer_info ? cartData.customer_info.customer_default_email : '';
|
|
913
|
+
customerDoc = !!cartData.customer_info ? cartData.customer_info.pro_proof_doc : '';
|
|
914
|
+
if (!(!!customerId || !!customerEmail || !!customerDoc)) return [3, 2];
|
|
915
|
+
return [4, emptyCart()];
|
|
916
|
+
case 1:
|
|
917
|
+
_a.sent();
|
|
918
|
+
_a.label = 2;
|
|
919
|
+
case 2: return [2];
|
|
920
|
+
}
|
|
921
|
+
});
|
|
922
|
+
}); };
|
|
923
|
+
checkCartCustomerValid();
|
|
924
|
+
}, [sdk, isUserLoggedIn, userLoaded, cartData]);
|
|
925
|
+
(0, react_1.useEffect)(function () {
|
|
926
|
+
if (!!!api)
|
|
927
|
+
return;
|
|
928
|
+
var getContent = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
929
|
+
var cartData;
|
|
930
|
+
return __generator(this, function (_a) {
|
|
931
|
+
switch (_a.label) {
|
|
932
|
+
case 0: return [4, initCart()];
|
|
933
|
+
case 1:
|
|
934
|
+
cartData = _a.sent();
|
|
935
|
+
if (!!!cartData)
|
|
936
|
+
return [2];
|
|
937
|
+
setCartData(cartData);
|
|
938
|
+
return [2];
|
|
939
|
+
}
|
|
940
|
+
});
|
|
941
|
+
}); };
|
|
942
|
+
getContent();
|
|
943
|
+
}, [sdk]);
|
|
944
|
+
return ((0, jsx_runtime_1.jsx)(_1.default.Provider, __assign({ value: {
|
|
945
|
+
loading: loading,
|
|
946
|
+
cartData: cartData,
|
|
947
|
+
cartErrors: cartErrors,
|
|
948
|
+
cartSliderOpen: cartSliderOpen,
|
|
949
|
+
isGuest: isGuest,
|
|
950
|
+
setIsGuest: setIsGuest,
|
|
951
|
+
setCartErrors: setCartErrors,
|
|
952
|
+
setCartData: setCartData,
|
|
953
|
+
saveCart: saveCart,
|
|
954
|
+
setCartLoading: setLoading,
|
|
955
|
+
getCartFromDatabase: getCartFromDatabase,
|
|
956
|
+
handlePayment: handlePayment,
|
|
957
|
+
setCartSliderOpen: setCartSliderOpen,
|
|
958
|
+
addItem: addItem,
|
|
959
|
+
deleteItem: deleteItem,
|
|
960
|
+
updateItem: updateItem,
|
|
961
|
+
verifyCart: verifyCart,
|
|
962
|
+
uploadFileToOrder: uploadFileToOrder,
|
|
963
|
+
updateEmail: updateEmail,
|
|
964
|
+
updateDefaultCustomer: updateDefaultCustomer,
|
|
965
|
+
emptyCart: emptyCart,
|
|
966
|
+
saveOrder: saveOrder,
|
|
967
|
+
updateShippingAddress: updateShippingAddress,
|
|
968
|
+
updateBillingAddress: updateBillingAddress,
|
|
969
|
+
getIsSameBillingThenShipping: getIsSameBillingThenShipping,
|
|
970
|
+
setShippingPayload: setShippingPayload,
|
|
971
|
+
removeDiscountRule: removeDiscountRule,
|
|
972
|
+
getShippingQuotes: getShippingQuotes,
|
|
973
|
+
selectShippingQuote: selectShippingQuote,
|
|
974
|
+
setPromoCode: setPromoCode,
|
|
975
|
+
getApplicablePromos: getApplicablePromos,
|
|
976
|
+
canBePaidWithPA: canBePaidWithPA,
|
|
977
|
+
getPaymentIncentiveDiscountAmount: getPaymentIncentiveDiscountAmount,
|
|
978
|
+
setPaidBy: setPaidBy
|
|
979
|
+
} }, { children: children })));
|
|
980
|
+
};
|
|
981
|
+
exports.default = CartContextProvider;
|
|
982
982
|
//# sourceMappingURL=CartContextProvider.js.map
|