addio-ecomm-sdk 1.3.76 → 1.3.78

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.
Files changed (88) hide show
  1. package/dist/index.js +1066 -1647
  2. package/dist/index.js.map +1 -1
  3. package/dist/lib/constants/components/CreditCardInput.js +22 -36
  4. package/dist/lib/constants/components/CreditCardInput.js.map +1 -1
  5. package/dist/lib/constants/components/Snackbar.js +10 -20
  6. package/dist/lib/constants/components/Snackbar.js.map +1 -1
  7. package/dist/lib/constants/contexts/CartContext/CartContextProvider.js +566 -906
  8. package/dist/lib/constants/contexts/CartContext/CartContextProvider.js.map +1 -1
  9. package/dist/lib/constants/contexts/CartContext/index.js +34 -114
  10. package/dist/lib/constants/contexts/CartContext/index.js.map +1 -1
  11. package/dist/lib/constants/contexts/DataContext/DataContextProvider.js +103 -234
  12. package/dist/lib/constants/contexts/DataContext/DataContextProvider.js.map +1 -1
  13. package/dist/lib/constants/contexts/DataContext/index.js +22 -62
  14. package/dist/lib/constants/contexts/DataContext/index.js.map +1 -1
  15. package/dist/lib/constants/contexts/LogContext/LogContextProvider.js +24 -87
  16. package/dist/lib/constants/contexts/LogContext/LogContextProvider.js.map +1 -1
  17. package/dist/lib/constants/contexts/LogContext/index.js +4 -42
  18. package/dist/lib/constants/contexts/LogContext/index.js.map +1 -1
  19. package/dist/lib/constants/contexts/ModalContext/ModalContextProvider.js +16 -31
  20. package/dist/lib/constants/contexts/ModalContext/ModalContextProvider.js.map +1 -1
  21. package/dist/lib/constants/contexts/ModalContext/index.js +11 -13
  22. package/dist/lib/constants/contexts/ModalContext/index.js.map +1 -1
  23. package/dist/lib/constants/contexts/SDKContext/SDKProvider.js +14 -70
  24. package/dist/lib/constants/contexts/SDKContext/SDKProvider.js.map +1 -1
  25. package/dist/lib/constants/contexts/SDKContext/index.js +1 -1
  26. package/dist/lib/constants/contexts/SDKContext/index.js.map +1 -1
  27. package/dist/lib/constants/contexts/SnackbarContext/SnackbarContext.js +3 -3
  28. package/dist/lib/constants/contexts/SnackbarContext/SnackbarContext.js.map +1 -1
  29. package/dist/lib/constants/contexts/SnackbarContext/SnackbarContextProvider.js +17 -31
  30. package/dist/lib/constants/contexts/SnackbarContext/SnackbarContextProvider.js.map +1 -1
  31. package/dist/lib/constants/contexts/SnackbarContext/interfaces/ISnackbarContext.d.ts +2 -0
  32. package/dist/lib/constants/contexts/SnackbarContext/interfaces/ISnackbarContext.js +2 -2
  33. package/dist/lib/constants/contexts/SnackbarContext/interfaces/ISnackbarContext.js.map +1 -1
  34. package/dist/lib/constants/contexts/StockNotifContext/StockNotifContextProvider.js +51 -118
  35. package/dist/lib/constants/contexts/StockNotifContext/StockNotifContextProvider.js.map +1 -1
  36. package/dist/lib/constants/contexts/StockNotifContext/index.js +3 -3
  37. package/dist/lib/constants/contexts/StockNotifContext/index.js.map +1 -1
  38. package/dist/lib/constants/contexts/UserContext/UserContextProvider.js +240 -397
  39. package/dist/lib/constants/contexts/UserContext/UserContextProvider.js.map +1 -1
  40. package/dist/lib/constants/contexts/UserContext/index.js +15 -69
  41. package/dist/lib/constants/contexts/UserContext/index.js.map +1 -1
  42. package/dist/lib/constants/contexts/WishlistContext/WishlistContextProvider.js +157 -253
  43. package/dist/lib/constants/contexts/WishlistContext/WishlistContextProvider.js.map +1 -1
  44. package/dist/lib/constants/contexts/WishlistContext/index.js +4 -4
  45. package/dist/lib/constants/contexts/WishlistContext/index.js.map +1 -1
  46. package/dist/lib/constants/hooks/useCart.js +3 -3
  47. package/dist/lib/constants/hooks/useCart.js.map +1 -1
  48. package/dist/lib/constants/hooks/useData.js +3 -3
  49. package/dist/lib/constants/hooks/useData.js.map +1 -1
  50. package/dist/lib/constants/hooks/useLogs.js +3 -3
  51. package/dist/lib/constants/hooks/useLogs.js.map +1 -1
  52. package/dist/lib/constants/hooks/useModal.js +3 -3
  53. package/dist/lib/constants/hooks/useModal.js.map +1 -1
  54. package/dist/lib/constants/hooks/useSDK.js +3 -3
  55. package/dist/lib/constants/hooks/useSDK.js.map +1 -1
  56. package/dist/lib/constants/hooks/useSnackbar.js +3 -3
  57. package/dist/lib/constants/hooks/useSnackbar.js.map +1 -1
  58. package/dist/lib/constants/hooks/useStockNotif.js +3 -3
  59. package/dist/lib/constants/hooks/useStockNotif.js.map +1 -1
  60. package/dist/lib/constants/hooks/useUser.js +3 -3
  61. package/dist/lib/constants/hooks/useUser.js.map +1 -1
  62. package/dist/lib/constants/hooks/useWishlist.js +3 -3
  63. package/dist/lib/constants/hooks/useWishlist.js.map +1 -1
  64. package/dist/lib/models/image/index.js +5 -10
  65. package/dist/lib/models/image/index.js.map +1 -1
  66. package/dist/lib/models/stockNotification/index.js +58 -118
  67. package/dist/lib/models/stockNotification/index.js.map +1 -1
  68. package/dist/lib/models/wishlist/index.js +73 -142
  69. package/dist/lib/models/wishlist/index.js.map +1 -1
  70. package/dist/utils/algolia.js +27 -73
  71. package/dist/utils/algolia.js.map +1 -1
  72. package/dist/utils/analytics.js +68 -79
  73. package/dist/utils/analytics.js.map +1 -1
  74. package/dist/utils/cart.js +10 -11
  75. package/dist/utils/cart.js.map +1 -1
  76. package/dist/utils/contentful.js +31 -104
  77. package/dist/utils/contentful.js.map +1 -1
  78. package/dist/utils/functions.js +54 -71
  79. package/dist/utils/functions.js.map +1 -1
  80. package/dist/utils/getSDK.js +12 -70
  81. package/dist/utils/getSDK.js.map +1 -1
  82. package/dist/utils/initStoreData.js +12 -57
  83. package/dist/utils/initStoreData.js.map +1 -1
  84. package/dist/utils/larivieremeunier.js +1 -1
  85. package/dist/utils/larivieremeunier.js.map +1 -1
  86. package/dist/utils/technosupply.js +1 -1
  87. package/dist/utils/technosupply.js.map +1 -1
  88. package/package.json +2 -2
@@ -1,15 +1,4 @@
1
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
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
3
  if (k2 === undefined) k2 = k;
15
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -33,331 +22,205 @@ var __importStar = (this && this.__importStar) || function (mod) {
33
22
  __setModuleDefault(result, mod);
34
23
  return result;
35
24
  };
36
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
- return new (P || (P = Promise))(function (resolve, reject) {
39
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42
- step((generator = generator.apply(thisArg, _arguments || [])).next());
43
- });
44
- };
45
- var __generator = (this && this.__generator) || function (thisArg, body) {
46
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
47
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
48
- function verb(n) { return function (v) { return step([n, v]); }; }
49
- function step(op) {
50
- if (f) throw new TypeError("Generator is already executing.");
51
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
52
- 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;
53
- if (y = 0, t) op = [op[0] & 2, t.value];
54
- switch (op[0]) {
55
- case 0: case 1: t = op; break;
56
- case 4: _.label++; return { value: op[1], done: false };
57
- case 5: _.label++; y = op[1]; op = [0]; continue;
58
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
59
- default:
60
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
61
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
62
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
63
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
64
- if (t[2]) _.ops.pop();
65
- _.trys.pop(); continue;
66
- }
67
- op = body.call(thisArg, _);
68
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
69
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
70
- }
71
- };
72
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
73
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
74
27
  };
75
28
  Object.defineProperty(exports, "__esModule", { value: true });
76
- var jsx_runtime_1 = require("react/jsx-runtime");
77
- var react_1 = require("react");
78
- var index_1 = __importStar(require("./index"));
79
- var useSDK_1 = __importDefault(require("../../hooks/useSDK"));
80
- var moment_1 = __importDefault(require("moment"));
81
- var functions_1 = require("../../../../utils/functions");
82
- var UserContextProvider = function (_a) {
83
- var children = _a.children;
84
- var sdk = (0, useSDK_1.default)();
85
- var setUserLoggedIn = function (userData) { return __awaiter(void 0, void 0, void 0, function () {
86
- var isPro, isClerk;
87
- return __generator(this, function (_a) {
88
- isPro = !!userData.basic_info.customer_professionnal_info &&
89
- !!userData.basic_info.customer_professionnal_info.proof_doc_value;
90
- localStorage.setItem('userLogged', 'true');
91
- if (!!isPro) {
92
- localStorage.setItem('isPro', 'true');
93
- }
94
- isClerk = localStorage.getItem('isClerk');
95
- if (typeof window !== 'undefined' && !!isClerk) {
96
- window.addEventListener('beforeunload', function (e) { return __awaiter(void 0, void 0, void 0, function () {
97
- var dialogText;
98
- return __generator(this, function (_a) {
99
- if (!!!userData.temp_hash)
100
- return [2];
101
- e.preventDefault();
102
- dialogText = '';
103
- e.returnValue = dialogText;
104
- return [2, dialogText];
105
- });
106
- }); });
107
- }
108
- setState(function (prevState) { return (__assign(__assign({}, prevState), { isUserLoggedIn: true, userData: userData })); });
109
- return [2];
110
- });
111
- }); };
112
- var setUserLoggedOut = function (keepRepCart) { return __awaiter(void 0, void 0, void 0, function () {
113
- return __generator(this, function (_a) {
114
- switch (_a.label) {
115
- case 0:
116
- if (!!!sdk)
117
- return [2];
118
- if (!(!!state.userData && state.userData.temp_hash && !!!keepRepCart)) return [3, 2];
119
- return [4, sdk.user.killRepCart(state.userData.temp_hash)];
120
- case 1:
121
- _a.sent();
122
- _a.label = 2;
123
- case 2:
124
- sdk.user.logout();
125
- setState(function (prevState) { return (__assign(__assign({}, prevState), { isUserLoggedIn: false, isPro: false, customPriceList: undefined, userData: null, resetUserData: true })); });
126
- return [2];
127
- }
128
- });
129
- }); };
130
- var setUserPreferedStore = function (storeId) { return __awaiter(void 0, void 0, void 0, function () {
131
- return __generator(this, function (_a) {
132
- switch (_a.label) {
133
- case 0:
134
- if (!!!state.userData)
135
- return [2];
136
- return [4, saveUser(__assign(__assign({}, state.userData), { prefered_store: storeId }))];
137
- case 1:
138
- _a.sent();
139
- return [2];
140
- }
141
- });
142
- }); };
143
- var saveUser = function (customer) { return __awaiter(void 0, void 0, void 0, function () {
144
- return __generator(this, function (_a) {
145
- switch (_a.label) {
146
- case 0:
147
- if (!!!sdk)
148
- return [2, false];
149
- setState(function (prevState) { return (__assign(__assign({}, prevState), { userData: customer, isPro: !!customer.basic_info.customer_professionnal_info &&
150
- !!customer.basic_info.customer_professionnal_info.professionnal_title })); });
151
- return [4, sdk.user.saveUser(customer)];
152
- case 1: return [2, _a.sent()];
153
- }
154
- });
155
- }); };
156
- var deleteUser = function () { return __awaiter(void 0, void 0, void 0, function () {
157
- return __generator(this, function (_a) {
158
- switch (_a.label) {
159
- case 0:
160
- if (!!!sdk)
161
- return [2, false];
162
- return [4, sdk.user.deleteUser()];
163
- case 1: return [2, _a.sent()];
164
- }
165
- });
166
- }); };
167
- var getOrder = function (orderID, token) { return __awaiter(void 0, void 0, void 0, function () {
168
- var order, err_1;
169
- return __generator(this, function (_a) {
170
- switch (_a.label) {
171
- case 0:
172
- if (!!!sdk)
173
- return [2, 'sdk_not_defined'];
174
- _a.label = 1;
175
- case 1:
176
- _a.trys.push([1, 3, , 4]);
177
- return [4, sdk.user.getOrder(orderID, token)];
178
- case 2:
179
- order = _a.sent();
180
- return [2, order];
181
- case 3:
182
- err_1 = _a.sent();
183
- return [2, 'error'];
184
- case 4: return [2];
185
- }
186
- });
187
- }); };
188
- var changePassword = function (currentPassword, newPassword) { return __awaiter(void 0, void 0, void 0, function () {
189
- var err_2;
190
- return __generator(this, function (_a) {
191
- switch (_a.label) {
192
- case 0:
193
- if (!!!sdk)
194
- return [2, 'sdk_not_defined'];
195
- _a.label = 1;
196
- case 1:
197
- _a.trys.push([1, 3, , 4]);
198
- return [4, sdk.user.changePassword(currentPassword, newPassword)];
199
- case 2: return [2, _a.sent()];
200
- case 3:
201
- err_2 = _a.sent();
202
- return [2, err_2];
203
- case 4: return [2];
204
- }
205
- });
206
- }); };
207
- var changeProfilePicture = function (base64) { return __awaiter(void 0, void 0, void 0, function () {
208
- var upload_1, err_3;
209
- return __generator(this, function (_a) {
210
- switch (_a.label) {
211
- case 0:
212
- if (!!!sdk)
213
- return [2, 'sdk_not_defined'];
214
- _a.label = 1;
215
- case 1:
216
- _a.trys.push([1, 3, , 4]);
217
- return [4, sdk.user.changeProfilePicture(base64)];
218
- case 2:
219
- upload_1 = _a.sent();
220
- setState(function (prevState) {
221
- if (prevState.userData) {
222
- return __assign(__assign({}, prevState), { userData: __assign(__assign({}, prevState.userData), { basic_info: __assign(__assign({}, prevState.userData.basic_info), { customer_image: upload_1 === "Can't upload image." || upload_1 === 'done' ? undefined : upload_1 }) }) });
223
- }
224
- return prevState;
225
- });
226
- return [2, upload_1];
227
- case 3:
228
- err_3 = _a.sent();
229
- return [2, err_3];
230
- case 4: return [2];
231
- }
232
- });
233
- }); };
234
- var setCustomPriceList = function (list) {
235
- setState(function (prevState) { return (__assign(__assign({}, prevState), { customPriceList: list, fetchCustomPrices: false })); });
29
+ const jsx_runtime_1 = require("react/jsx-runtime");
30
+ const react_1 = require("react");
31
+ const index_1 = __importStar(require("./index"));
32
+ const useSDK_1 = __importDefault(require("../../hooks/useSDK"));
33
+ const moment_1 = __importDefault(require("moment"));
34
+ const functions_1 = require("../../../../utils/functions");
35
+ const UserContextProvider = ({ children }) => {
36
+ const sdk = (0, useSDK_1.default)();
37
+ const setUserLoggedIn = async (userData) => {
38
+ const isPro = !!userData.basic_info.customer_professionnal_info &&
39
+ !!userData.basic_info.customer_professionnal_info.proof_doc_value;
40
+ localStorage.setItem('userLogged', 'true');
41
+ if (!!isPro) {
42
+ localStorage.setItem('isPro', 'true');
43
+ }
44
+ const isClerk = localStorage.getItem('isClerk');
45
+ if (typeof window !== 'undefined' && !!isClerk) {
46
+ window.addEventListener('beforeunload', async (e) => {
47
+ if (!!!userData.temp_hash)
48
+ return;
49
+ e.preventDefault();
50
+ const dialogText = '';
51
+ e.returnValue = dialogText;
52
+ return dialogText;
53
+ });
54
+ }
55
+ setState((prevState) => (Object.assign(Object.assign({}, prevState), { isUserLoggedIn: true, userData: userData })));
56
+ };
57
+ const setUserLoggedOut = async (keepRepCart) => {
58
+ if (!!!sdk)
59
+ return;
60
+ if (!!state.userData && state.userData.temp_hash && !!!keepRepCart) {
61
+ await sdk.user.killRepCart(state.userData.temp_hash);
62
+ }
63
+ sdk.user.logout();
64
+ setState((prevState) => (Object.assign(Object.assign({}, prevState), { isUserLoggedIn: false, isPro: false, customPriceList: undefined, userData: null, resetUserData: true })));
65
+ };
66
+ const setUserPreferedStore = async (storeId) => {
67
+ if (!!!state.userData)
68
+ return;
69
+ await saveUser(Object.assign(Object.assign({}, state.userData), { prefered_store: storeId }));
70
+ };
71
+ const saveUser = async (customer) => {
72
+ if (!!!sdk)
73
+ return false;
74
+ setState((prevState) => (Object.assign(Object.assign({}, prevState), { userData: customer, isPro: !!customer.basic_info.customer_professionnal_info &&
75
+ !!customer.basic_info.customer_professionnal_info.professionnal_title })));
76
+ return await sdk.user.saveUser(customer);
77
+ };
78
+ const deleteUser = async () => {
79
+ if (!!!sdk)
80
+ return false;
81
+ return await sdk.user.deleteUser();
82
+ };
83
+ const getOrder = async (orderID, token) => {
84
+ if (!!!sdk)
85
+ return 'sdk_not_defined';
86
+ try {
87
+ const order = await sdk.user.getOrder(orderID, token);
88
+ return order;
89
+ }
90
+ catch (err) {
91
+ return 'error';
92
+ }
93
+ };
94
+ const changePassword = async (currentPassword, newPassword) => {
95
+ if (!!!sdk)
96
+ return 'sdk_not_defined';
97
+ try {
98
+ return await sdk.user.changePassword(currentPassword, newPassword);
99
+ }
100
+ catch (err) {
101
+ return err;
102
+ }
103
+ };
104
+ const changeProfilePicture = async (base64) => {
105
+ if (!!!sdk)
106
+ return 'sdk_not_defined';
107
+ try {
108
+ const upload = await sdk.user.changeProfilePicture(base64);
109
+ setState((prevState) => {
110
+ if (prevState.userData) {
111
+ return Object.assign(Object.assign({}, prevState), { userData: Object.assign(Object.assign({}, prevState.userData), { basic_info: Object.assign(Object.assign({}, prevState.userData.basic_info), { customer_image: upload === "Can't upload image." || upload === 'done' ? undefined : upload }) }) });
112
+ }
113
+ return prevState;
114
+ });
115
+ return upload;
116
+ }
117
+ catch (err) {
118
+ return err;
119
+ }
120
+ };
121
+ const setCustomPriceList = (list) => {
122
+ setState((prevState) => (Object.assign(Object.assign({}, prevState), { customPriceList: list, fetchCustomPrices: false })));
236
123
  localStorage.setItem('customPriceList', JSON.stringify(list));
237
124
  localStorage.setItem('lastCustomPriceFetch', (0, moment_1.default)().format('YYYY-MM-DD'));
238
125
  };
239
- var setLoaded = function (value) {
240
- setState(function (prevState) { return (__assign(__assign({}, prevState), { loaded: value })); });
126
+ const setLoaded = (value) => {
127
+ setState((prevState) => (Object.assign(Object.assign({}, prevState), { loaded: value })));
241
128
  };
242
- var _b = (0, react_1.useState)((0, index_1.getDefaultUserContext)()), state = _b[0], setState = _b[1];
243
- (0, react_1.useEffect)(function () {
129
+ const [state, setState] = (0, react_1.useState)((0, index_1.getDefaultUserContext)());
130
+ (0, react_1.useEffect)(() => {
244
131
  if (!!!sdk)
245
132
  return;
246
- var userLoggedData = localStorage.getItem('userLogged');
247
- var userIsPro = localStorage.getItem('isPro');
248
- var isClerk = localStorage.getItem('isClerk');
249
- var isLoggedIn = !!userLoggedData && JSON.parse(userLoggedData);
250
- var isPro = !!userLoggedData && !!userIsPro && JSON.parse(userIsPro);
251
- var newState = __assign({ isUserLoggedIn: !!!isClerk && !!isLoggedIn, isPro: !!!isClerk && !!isPro }, checkForCustomPriceList(!!userIsPro));
133
+ const userLoggedData = localStorage.getItem('userLogged');
134
+ const userIsPro = localStorage.getItem('isPro');
135
+ const isClerk = localStorage.getItem('isClerk');
136
+ const isLoggedIn = !!userLoggedData && JSON.parse(userLoggedData);
137
+ const isPro = !!userLoggedData && !!userIsPro && JSON.parse(userIsPro);
138
+ let newState = Object.assign({ isUserLoggedIn: !!!isClerk && !!isLoggedIn, isPro: !!!isClerk && !!isPro }, checkForCustomPriceList(!!userIsPro));
252
139
  if (!!isClerk) {
253
140
  localStorage.removeItem('isClerk');
254
141
  }
255
- return setState(function (prevState) { return (__assign(__assign(__assign({}, prevState), newState), { loaded: true })); });
142
+ return setState((prevState) => (Object.assign(Object.assign(Object.assign({}, prevState), newState), { loaded: true })));
256
143
  }, [sdk]);
257
- (0, react_1.useEffect)(function () {
144
+ (0, react_1.useEffect)(() => {
258
145
  if (!!!state.fetchCustomPrices || !!!state.isPro || !!!state.userData || !!!sdk)
259
146
  return;
260
- var fetchCustomPrices = function () { return __awaiter(void 0, void 0, void 0, function () {
261
- var proUser, newList, e_1;
262
- return __generator(this, function (_a) {
263
- switch (_a.label) {
264
- case 0:
265
- proUser = !!state.userData && !!state.isPro ? state.userData.id : '';
266
- if (!!!proUser)
267
- return [2];
268
- _a.label = 1;
269
- case 1:
270
- _a.trys.push([1, 3, , 4]);
271
- return [4, sdk.user.fetchCustomPriceList(proUser)];
272
- case 2:
273
- newList = _a.sent();
274
- if (!!newList) {
275
- setCustomPriceList(newList);
276
- }
277
- return [3, 4];
278
- case 3:
279
- e_1 = _a.sent();
280
- console.log('error on fetching customPrices from G2: ', e_1.toString());
281
- setState(function (prevState) { return (__assign(__assign({}, prevState), { fetchCustomPrices: false, customPriceList: undefined })); });
282
- return [3, 4];
283
- case 4: return [2];
147
+ const fetchCustomPrices = async () => {
148
+ const proUser = !!state.userData && !!state.isPro ? state.userData.id : '';
149
+ if (!!!proUser)
150
+ return;
151
+ try {
152
+ const newList = await sdk.user.fetchCustomPriceList(proUser);
153
+ if (!!newList) {
154
+ setCustomPriceList(newList);
284
155
  }
285
- });
286
- }); };
156
+ }
157
+ catch (e) {
158
+ console.log('error on fetching customPrices from G2: ', e.toString());
159
+ setState((prevState) => (Object.assign(Object.assign({}, prevState), { fetchCustomPrices: false, customPriceList: undefined })));
160
+ }
161
+ };
287
162
  fetchCustomPrices();
288
163
  }, [sdk, state.fetchCustomPrices, state.isPro, state.userData]);
289
- (0, react_1.useEffect)(function () {
164
+ (0, react_1.useEffect)(() => {
290
165
  if (!!!sdk)
291
166
  return;
292
167
  if (!state.isUserLoggedIn) {
293
168
  return;
294
169
  }
295
- var getContent = function () { return __awaiter(void 0, void 0, void 0, function () {
296
- var user, checkIsPro_1, err_4, error;
297
- return __generator(this, function (_a) {
298
- switch (_a.label) {
299
- case 0:
300
- _a.trys.push([0, 2, , 5]);
301
- return [4, sdk.user.getUser(!!localStorage.getItem('isClerk'))];
302
- case 1:
303
- user = (_a.sent());
304
- if (!!!user) {
305
- localStorage.setItem('isPro', JSON.stringify(false));
306
- localStorage.removeItem('foundInG2');
307
- if (!!localStorage.getItem('customPriceList')) {
308
- localStorage.removeItem('customPriceList');
309
- localStorage.removeItem('lastCustomPriceFetch');
310
- }
311
- return [2, setState(function (prevState) { return (__assign(__assign(__assign({}, prevState), { userData: null, isPro: false }), (!!prevState.customPriceList
312
- ? {
313
- customPriceList: undefined
314
- }
315
- : {}))); })];
316
- }
317
- checkIsPro_1 = !!user.basic_info.customer_professionnal_info &&
318
- !!user.basic_info.customer_professionnal_info.professionnal_title;
319
- setState(function (prevState) { return (__assign(__assign(__assign({}, prevState), { userData: user, isPro: checkIsPro_1 }), checkForCustomPriceList(checkIsPro_1))); });
320
- localStorage.setItem('isPro', JSON.stringify(checkIsPro_1));
321
- if (!!!checkIsPro_1) {
322
- localStorage.removeItem('foundInG2');
323
- if (!!localStorage.getItem('customPriceList')) {
324
- localStorage.removeItem('customPriceList');
325
- localStorage.removeItem('lastCustomPriceFetch');
326
- }
170
+ const getContent = async () => {
171
+ let user;
172
+ try {
173
+ user = (await sdk.user.getUser(!!localStorage.getItem('isClerk')));
174
+ if (!!!user) {
175
+ localStorage.setItem('isPro', JSON.stringify(false));
176
+ localStorage.removeItem('foundInG2');
177
+ if (!!localStorage.getItem('customPriceList')) {
178
+ localStorage.removeItem('customPriceList');
179
+ localStorage.removeItem('lastCustomPriceFetch');
180
+ }
181
+ return setState((prevState) => (Object.assign(Object.assign(Object.assign({}, prevState), { userData: null, isPro: false }), (!!prevState.customPriceList
182
+ ? {
183
+ customPriceList: undefined
327
184
  }
328
- return [3, 5];
329
- case 2:
330
- err_4 = _a.sent();
331
- if (!!!err_4.message) return [3, 4];
332
- error = JSON.parse(err_4.message);
333
- if (!(!!error && !!error.error)) return [3, 4];
334
- return [4, setUserLoggedOut()];
335
- case 3:
336
- _a.sent();
337
- _a.label = 4;
338
- case 4: return [3, 5];
339
- case 5: return [2];
185
+ : {}))));
340
186
  }
341
- });
342
- }); };
187
+ const checkIsPro = !!user.basic_info.customer_professionnal_info &&
188
+ !!user.basic_info.customer_professionnal_info.professionnal_title;
189
+ setState((prevState) => (Object.assign(Object.assign(Object.assign({}, prevState), { userData: user, isPro: checkIsPro }), checkForCustomPriceList(checkIsPro))));
190
+ localStorage.setItem('isPro', JSON.stringify(checkIsPro));
191
+ if (!!!checkIsPro) {
192
+ localStorage.removeItem('foundInG2');
193
+ if (!!localStorage.getItem('customPriceList')) {
194
+ localStorage.removeItem('customPriceList');
195
+ localStorage.removeItem('lastCustomPriceFetch');
196
+ }
197
+ }
198
+ }
199
+ catch (err) {
200
+ if (!!err.message) {
201
+ const error = JSON.parse(err.message);
202
+ if (!!error && !!error.error) {
203
+ await setUserLoggedOut();
204
+ }
205
+ }
206
+ }
207
+ };
343
208
  getContent();
344
209
  }, [sdk, state.isUserLoggedIn]);
345
- (0, react_1.useEffect)(function () {
346
- return function () {
210
+ (0, react_1.useEffect)(() => {
211
+ return () => {
347
212
  if (typeof window !== 'undefined') {
348
- window.removeEventListener('onbeforeunload', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
349
- return [2];
350
- }); }); });
213
+ window.removeEventListener('onbeforeunload', async () => { });
351
214
  }
352
215
  };
353
216
  }, []);
354
- var checkForCustomPriceList = function (userIsPro) {
355
- var hasCustomPriceList = !!userIsPro && localStorage.getItem('customPriceList');
356
- var lastCustomPriceFetch = !!userIsPro && localStorage.getItem('lastCustomPriceFetch');
357
- var foundProInG2 = !!userIsPro && localStorage.getItem('foundInG2');
358
- var lastFetchWasBefore = !!userIsPro &&
217
+ const checkForCustomPriceList = (userIsPro) => {
218
+ const hasCustomPriceList = !!userIsPro && localStorage.getItem('customPriceList');
219
+ const lastCustomPriceFetch = !!userIsPro && localStorage.getItem('lastCustomPriceFetch');
220
+ const foundProInG2 = !!userIsPro && localStorage.getItem('foundInG2');
221
+ const lastFetchWasBefore = !!userIsPro &&
359
222
  (!!!lastCustomPriceFetch || (!!lastCustomPriceFetch && (0, moment_1.default)().isAfter((0, moment_1.default)(lastCustomPriceFetch), 'day')));
360
- var parsedJSONCustomPrices = [];
223
+ let parsedJSONCustomPrices = [];
361
224
  if (!!hasCustomPriceList) {
362
225
  try {
363
226
  parsedJSONCustomPrices = JSON.parse(hasCustomPriceList);
@@ -366,106 +229,86 @@ var UserContextProvider = function (_a) {
366
229
  console.log(e.toString());
367
230
  }
368
231
  }
369
- return __assign(__assign({}, (!!!lastFetchWasBefore && !!foundProInG2
232
+ return Object.assign(Object.assign({}, (!!!lastFetchWasBefore && !!foundProInG2
370
233
  ? { customPriceList: parsedJSONCustomPrices }
371
234
  : { customPriceList: undefined })), (!!lastFetchWasBefore && !!foundProInG2 ? { fetchCustomPrices: true } : {}));
372
235
  };
373
- var getPromiseToPurchaseHistory = function (userId, filters) { return __awaiter(void 0, void 0, void 0, function () {
374
- var api, body, requestHistory, responseData, errorStr;
375
- return __generator(this, function (_a) {
376
- switch (_a.label) {
377
- case 0:
378
- if (!state.isPro)
379
- throw new Error('Only the PRO users have a promise to purchase');
380
- api = !!sdk ? sdk.getAPIInfos() : null;
381
- if (!!!api)
382
- return [2];
383
- if (!!!userId) {
384
- if (!!state.userData && !!state.userData.id)
385
- userId = state.userData.id;
386
- else
387
- throw new Error("Can't get user promise to purchase without userId");
388
- }
389
- body = undefined;
390
- if (!!filters) {
391
- body = __assign(__assign({}, (!!filters.bankTypeId
392
- ? {
393
- bank_type_id: filters.bankTypeId
394
- }
395
- : {})), (!!filters.unixDateRange
396
- ? {
397
- unix_date_range: filters.unixDateRange
398
- }
399
- : {}));
400
- }
401
- return [4, fetch("".concat(api.api, "/users/").concat(userId, "/promisestopurchase/history"), {
402
- method: 'POST',
403
- headers: {
404
- Authorization: "Bearer ".concat(api.token),
405
- 'Content-Type': 'application/json',
406
- 'x-gng-ecomm-token': api.userToken
407
- },
408
- body: !!body ? JSON.stringify(body) : undefined
409
- })];
410
- case 1:
411
- requestHistory = _a.sent();
412
- return [4, requestHistory.json()];
413
- case 2:
414
- responseData = _a.sent();
415
- if (responseData.status !== 200) {
416
- errorStr = !!!responseData.error
417
- ? ''
418
- : typeof responseData.error === 'string'
419
- ? responseData.error
420
- : (0, functions_1.getStringByLang)(responseData.error, 'fr');
421
- throw new Error('Error on history fetch: ' + errorStr);
422
- }
423
- return [2, responseData.data.history];
424
- }
236
+ const getPromiseToPurchaseHistory = async (userId, filters) => {
237
+ if (!state.isPro)
238
+ throw new Error('Only the PRO users have a promise to purchase');
239
+ const api = !!sdk ? sdk.getAPIInfos() : null;
240
+ if (!!!api)
241
+ return;
242
+ if (!!!userId) {
243
+ if (!!state.userData && !!state.userData.id)
244
+ userId = state.userData.id;
245
+ else
246
+ throw new Error("Can't get user promise to purchase without userId");
247
+ }
248
+ let body = undefined;
249
+ if (!!filters) {
250
+ body = Object.assign(Object.assign({}, (!!filters.bankTypeId
251
+ ? {
252
+ bank_type_id: filters.bankTypeId
253
+ }
254
+ : {})), (!!filters.unixDateRange
255
+ ? {
256
+ unix_date_range: filters.unixDateRange
257
+ }
258
+ : {}));
259
+ }
260
+ const requestHistory = await fetch(`${api.api}/users/${userId}/promisestopurchase/history`, {
261
+ method: 'POST',
262
+ headers: {
263
+ Authorization: `Bearer ${api.token}`,
264
+ 'Content-Type': 'application/json',
265
+ 'x-gng-ecomm-token': api.userToken
266
+ },
267
+ body: !!body ? JSON.stringify(body) : undefined
425
268
  });
426
- }); };
427
- var getPromiseToPurchaseRemainingAmounts = function (userId) { return __awaiter(void 0, void 0, void 0, function () {
428
- var api, requestHistory, responseData, errorStr;
429
- return __generator(this, function (_a) {
430
- switch (_a.label) {
431
- case 0:
432
- if (!state.isPro)
433
- throw new Error('Only the PRO users have a promise to purchase');
434
- api = !!sdk ? sdk.getAPIInfos() : null;
435
- if (!!!api)
436
- return [2];
437
- if (!!!userId) {
438
- if (!!state.userData && !!state.userData.id)
439
- userId = state.userData.id;
440
- else
441
- throw new Error("Can't get remaining amounts for user without userId");
442
- }
443
- return [4, fetch("".concat(api.api, "/users/").concat(userId, "/promisestopurchase/remainingAmounts"), {
444
- method: 'GET',
445
- headers: {
446
- Authorization: "Bearer ".concat(api.token),
447
- 'Content-Type': 'application/json',
448
- 'x-gng-ecomm-token': api.userToken
449
- }
450
- })];
451
- case 1:
452
- requestHistory = _a.sent();
453
- return [4, requestHistory.json()];
454
- case 2:
455
- responseData = _a.sent();
456
- if (responseData.status !== 200) {
457
- errorStr = !!!responseData.error
458
- ? ''
459
- : typeof responseData.error === 'string'
460
- ? responseData.error
461
- : (0, functions_1.getStringByLang)(responseData.error, 'fr');
462
- throw new Error('Error on remainingAmounts fetch: ' + errorStr);
463
- }
464
- return [2, responseData.data.availableAmountsForBanks];
269
+ const responseData = await requestHistory.json();
270
+ if (responseData.status !== 200) {
271
+ const errorStr = !!!responseData.error
272
+ ? ''
273
+ : typeof responseData.error === 'string'
274
+ ? responseData.error
275
+ : (0, functions_1.getStringByLang)(responseData.error, 'fr');
276
+ throw new Error('Error on history fetch: ' + errorStr);
277
+ }
278
+ return responseData.data.history;
279
+ };
280
+ const getPromiseToPurchaseRemainingAmounts = async (userId) => {
281
+ if (!state.isPro)
282
+ throw new Error('Only the PRO users have a promise to purchase');
283
+ const api = !!sdk ? sdk.getAPIInfos() : null;
284
+ if (!!!api)
285
+ return;
286
+ if (!!!userId) {
287
+ if (!!state.userData && !!state.userData.id)
288
+ userId = state.userData.id;
289
+ else
290
+ throw new Error("Can't get remaining amounts for user without userId");
291
+ }
292
+ const requestHistory = await fetch(`${api.api}/users/${userId}/promisestopurchase/remainingAmounts`, {
293
+ method: 'GET',
294
+ headers: {
295
+ Authorization: `Bearer ${api.token}`,
296
+ 'Content-Type': 'application/json',
297
+ 'x-gng-ecomm-token': api.userToken
465
298
  }
466
299
  });
467
- }); };
468
- return ((0, jsx_runtime_1.jsx)(index_1.default.Provider, __assign({ value: __assign(__assign({}, state), { setLoaded: setLoaded, setUserLoggedIn: setUserLoggedIn, setUserLoggedOut: setUserLoggedOut, saveUser: saveUser, deleteUser: deleteUser, getOrder: getOrder, changePassword: changePassword, changeProfilePicture: changeProfilePicture, setUserPreferedStore: setUserPreferedStore, setCustomPriceList: setCustomPriceList, getPromiseToPurchaseHistory: getPromiseToPurchaseHistory, getPromiseToPurchaseRemainingAmounts: getPromiseToPurchaseRemainingAmounts }) }, { children: children })));
300
+ const responseData = await requestHistory.json();
301
+ if (responseData.status !== 200) {
302
+ const errorStr = !!!responseData.error
303
+ ? ''
304
+ : typeof responseData.error === 'string'
305
+ ? responseData.error
306
+ : (0, functions_1.getStringByLang)(responseData.error, 'fr');
307
+ throw new Error('Error on remainingAmounts fetch: ' + errorStr);
308
+ }
309
+ return responseData.data.availableAmountsForBanks;
310
+ };
311
+ return ((0, jsx_runtime_1.jsx)(index_1.default.Provider, Object.assign({ value: Object.assign(Object.assign({}, state), { setLoaded: setLoaded, setUserLoggedIn: setUserLoggedIn, setUserLoggedOut: setUserLoggedOut, saveUser: saveUser, deleteUser: deleteUser, getOrder: getOrder, changePassword: changePassword, changeProfilePicture: changeProfilePicture, setUserPreferedStore: setUserPreferedStore, setCustomPriceList: setCustomPriceList, getPromiseToPurchaseHistory: getPromiseToPurchaseHistory, getPromiseToPurchaseRemainingAmounts: getPromiseToPurchaseRemainingAmounts }) }, { children: children })));
469
312
  };
470
313
  exports.default = UserContextProvider;
471
314
  //# sourceMappingURL=UserContextProvider.js.map