@washingtonpost/subs-de-inputs 0.2.0-canary.0 → 0.2.0

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.
@@ -1,19 +1,20 @@
1
- import { getCookie, WPGeo, JSON_HEADERS, ENDPOINTS, ResponseStatus } from '@washingtonpost/subs-sdk';
1
+ import { getCookie, WPGeo, JSON_HEADERS, ResponseStatus, ENDPOINTS } from '@washingtonpost/subs-sdk';
2
2
  import React, { useState, useEffect } from 'react';
3
3
  import { Select, theme, styled } from '@washingtonpost/wpds-ui-kit';
4
+ import { useScript, ScriptStatus } from '@washingtonpost/subs-hooks';
4
5
 
5
- var CollectionBehaviors = {
6
+ const CollectionBehaviors = {
6
7
  COLLECT: 'COLLECT',
7
8
  DO_NOT_COLLECT: 'DO_NOT_COLLECT'
8
9
  };
9
- var AttributesState = {
10
+ const AttributesState = {
10
11
  SUCCESS: '100'
11
12
  };
12
- var IngestType = {
13
+ const IngestType = {
13
14
  EXPLICIT: 'explicit',
14
15
  IMPLICIT: 'implicit'
15
16
  };
16
- var IngestResponseState = {
17
+ const IngestResponseState = {
17
18
  SUCCESS: '100',
18
19
  SYSTEM_ERROR: '101',
19
20
  INVALID_TYPE: '102',
@@ -22,624 +23,204 @@ var IngestResponseState = {
22
23
  INVALID_ATTRIBUTE_DEFINITION: '105',
23
24
  INVALID_META_DEFINITION: '106',
24
25
  UNAUTHENTICATED: '107',
25
- MISMATCHED_IDENTIFIER: '108'
26
+ MISMATCHED_IDENTIFIER: '108',
27
+ DISABLED_ATTRIBUTE_DEFINITION: '109',
28
+ DO_NOT_COLLECT: '110'
26
29
  };
27
30
 
28
- var hasRequiredPrivacyCookies = function hasRequiredPrivacyCookies() {
31
+ const hasRequiredPrivacyCookies = () => {
29
32
  var _WPGeo;
30
33
  if (typeof window === 'undefined') {
31
34
  return false;
32
35
  }
33
- var wp_usp = getCookie('wp_usp');
34
- var countryCode = (_WPGeo = WPGeo()) == null ? void 0 : _WPGeo.country_code;
36
+ const wp_usp = getCookie('wp_usp');
37
+ const countryCode = (_WPGeo = WPGeo()) === null || _WPGeo === void 0 ? void 0 : _WPGeo.country_code;
35
38
  return !!(wp_usp && countryCode === 'US');
36
39
  };
37
40
 
38
- function _regeneratorRuntime() {
39
- _regeneratorRuntime = function () {
40
- return exports;
41
- };
42
- var exports = {},
43
- Op = Object.prototype,
44
- hasOwn = Op.hasOwnProperty,
45
- defineProperty = Object.defineProperty || function (obj, key, desc) {
46
- obj[key] = desc.value;
47
- },
48
- $Symbol = "function" == typeof Symbol ? Symbol : {},
49
- iteratorSymbol = $Symbol.iterator || "@@iterator",
50
- asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
51
- toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
52
- function define(obj, key, value) {
53
- return Object.defineProperty(obj, key, {
54
- value: value,
55
- enumerable: !0,
56
- configurable: !0,
57
- writable: !0
58
- }), obj[key];
59
- }
41
+ const base = `${ENDPOINTS.base}/de/v1`;
42
+ const attributesCache = {};
43
+ const getAttributes = async _ref => {
44
+ let {
45
+ fieldName
46
+ } = _ref;
47
+ if (attributesCache[fieldName]) {
48
+ return attributesCache[fieldName];
49
+ }
50
+ const fieldNames = [fieldName];
60
51
  try {
61
- define({}, "");
62
- } catch (err) {
63
- define = function (obj, key, value) {
64
- return obj[key] = value;
65
- };
66
- }
67
- function wrap(innerFn, outerFn, self, tryLocsList) {
68
- var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
69
- generator = Object.create(protoGenerator.prototype),
70
- context = new Context(tryLocsList || []);
71
- return defineProperty(generator, "_invoke", {
72
- value: makeInvokeMethod(innerFn, self, context)
73
- }), generator;
74
- }
75
- function tryCatch(fn, obj, arg) {
76
- try {
77
- return {
78
- type: "normal",
79
- arg: fn.call(obj, arg)
80
- };
81
- } catch (err) {
82
- return {
83
- type: "throw",
84
- arg: err
85
- };
52
+ const url = new URL(`${base}/attributes`);
53
+ url.searchParams.set('attributes', fieldNames.join(','));
54
+ const data = await fetch(url.toString(), {
55
+ credentials: 'include',
56
+ headers: JSON_HEADERS
57
+ });
58
+ const json = await data.json();
59
+ if (data.ok && json.status === ResponseStatus.SUCCESS) {
60
+ const attributes = json.attributes || [];
61
+ attributesCache[fieldName] = attributes;
62
+ return attributes;
63
+ } else {
64
+ return [];
86
65
  }
66
+ } catch (e) {
67
+ console.debug(e);
68
+ return [];
87
69
  }
88
- exports.wrap = wrap;
89
- var ContinueSentinel = {};
90
- function Generator() {}
91
- function GeneratorFunction() {}
92
- function GeneratorFunctionPrototype() {}
93
- var IteratorPrototype = {};
94
- define(IteratorPrototype, iteratorSymbol, function () {
95
- return this;
96
- });
97
- var getProto = Object.getPrototypeOf,
98
- NativeIteratorPrototype = getProto && getProto(getProto(values([])));
99
- NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
100
- var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
101
- function defineIteratorMethods(prototype) {
102
- ["next", "throw", "return"].forEach(function (method) {
103
- define(prototype, method, function (arg) {
104
- return this._invoke(method, arg);
105
- });
70
+ };
71
+ const ingest = async _ref2 => {
72
+ let {
73
+ submitData: {
74
+ fieldName,
75
+ value
76
+ },
77
+ type,
78
+ source
79
+ } = _ref2;
80
+ const url = `${base}/ingest`;
81
+ const wapo_login_id = getCookie('wapo_login_id');
82
+ if (!hasRequiredPrivacyCookies()) {
83
+ throw new Error('does not satisfy cookie check');
84
+ }
85
+ let attributeInfo = attributesCache[fieldName];
86
+ if (!attributeInfo) {
87
+ attributeInfo = await getAttributes({
88
+ fieldName
106
89
  });
107
90
  }
108
- function AsyncIterator(generator, PromiseImpl) {
109
- function invoke(method, arg, resolve, reject) {
110
- var record = tryCatch(generator[method], generator, arg);
111
- if ("throw" !== record.type) {
112
- var result = record.arg,
113
- value = result.value;
114
- return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
115
- invoke("next", value, resolve, reject);
116
- }, function (err) {
117
- invoke("throw", err, resolve, reject);
118
- }) : PromiseImpl.resolve(value).then(function (unwrapped) {
119
- result.value = unwrapped, resolve(result);
120
- }, function (error) {
121
- return invoke("throw", error, resolve, reject);
122
- });
123
- }
124
- reject(record.arg);
91
+ if (attributeInfo[0] && attributeInfo[0].name === fieldName && attributeInfo[0].collection_behavior === CollectionBehaviors.DO_NOT_COLLECT) {
92
+ throw new Error('do not collect');
93
+ }
94
+ const jucid = localStorage.getItem('uuid');
95
+ const ga = getCookie('_ga');
96
+ const payload = {
97
+ jucid,
98
+ ga,
99
+ type,
100
+ wapo_login_id,
101
+ data: {
102
+ fieldName,
103
+ value
104
+ },
105
+ metadata: {
106
+ source
125
107
  }
126
- var previousPromise;
127
- defineProperty(this, "_invoke", {
128
- value: function (method, arg) {
129
- function callInvokeWithMethodAndArg() {
130
- return new PromiseImpl(function (resolve, reject) {
131
- invoke(method, arg, resolve, reject);
132
- });
133
- }
134
- return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
135
- }
108
+ };
109
+ try {
110
+ const response = await fetch(url, {
111
+ method: 'POST',
112
+ credentials: 'include',
113
+ headers: JSON_HEADERS,
114
+ body: JSON.stringify(payload)
136
115
  });
116
+ const json = await response.json();
117
+ return json;
118
+ } catch (e) {
119
+ console.debug(e);
120
+ return null;
137
121
  }
138
- function makeInvokeMethod(innerFn, self, context) {
139
- var state = "suspendedStart";
140
- return function (method, arg) {
141
- if ("executing" === state) throw new Error("Generator is already running");
142
- if ("completed" === state) {
143
- if ("throw" === method) throw arg;
144
- return doneResult();
145
- }
146
- for (context.method = method, context.arg = arg;;) {
147
- var delegate = context.delegate;
148
- if (delegate) {
149
- var delegateResult = maybeInvokeDelegate(delegate, context);
150
- if (delegateResult) {
151
- if (delegateResult === ContinueSentinel) continue;
152
- return delegateResult;
153
- }
154
- }
155
- if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
156
- if ("suspendedStart" === state) throw state = "completed", context.arg;
157
- context.dispatchException(context.arg);
158
- } else "return" === context.method && context.abrupt("return", context.arg);
159
- state = "executing";
160
- var record = tryCatch(innerFn, self, context);
161
- if ("normal" === record.type) {
162
- if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
163
- return {
164
- value: record.arg,
165
- done: context.done
166
- };
122
+ };
123
+
124
+ const scriptSrc = `${ENDPOINTS.staticAssets === 'https://subscribe.washingtonpost.com/static' ? 'https://www.washingtonpost.com/subscribe/static/' : ENDPOINTS.staticAssets}/de-utils/twpdeu.min.js`;
125
+ const DESelect = _ref => {
126
+ let {
127
+ source,
128
+ fieldName,
129
+ label,
130
+ dataDictionaryConfig,
131
+ defaultValue,
132
+ disabled,
133
+ submit,
134
+ onChange = () => {},
135
+ onFinished = () => {},
136
+ valuesFilter = () => true,
137
+ children
138
+ } = _ref;
139
+ const [config, setConfig] = useState(dataDictionaryConfig);
140
+ const [selected, setSelected] = useState('');
141
+ const scriptStatus = useScript(scriptSrc);
142
+ useEffect(() => {
143
+ const fetchConfig = async () => {
144
+ try {
145
+ var _window;
146
+ const config = await ((_window = window) === null || _window === void 0 || (_window = _window.__twpdeu) === null || _window === void 0 ? void 0 : _window.getFieldConfigs({
147
+ fieldName
148
+ }));
149
+ if (config) {
150
+ setConfig(config[0]);
151
+ } else {
152
+ console.error('unable to get config', fieldName);
167
153
  }
168
- "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
154
+ } catch (e) {
155
+ console.warn('unable to get config', fieldName, e);
169
156
  }
170
157
  };
171
- }
172
- function maybeInvokeDelegate(delegate, context) {
173
- var methodName = context.method,
174
- method = delegate.iterator[methodName];
175
- if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel;
176
- var record = tryCatch(method, delegate.iterator, context.arg);
177
- if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
178
- var info = record.arg;
179
- return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel);
180
- }
181
- function pushTryEntry(locs) {
182
- var entry = {
183
- tryLoc: locs[0]
184
- };
185
- 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
186
- }
187
- function resetTryEntry(entry) {
188
- var record = entry.completion || {};
189
- record.type = "normal", delete record.arg, entry.completion = record;
190
- }
191
- function Context(tryLocsList) {
192
- this.tryEntries = [{
193
- tryLoc: "root"
194
- }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
195
- }
196
- function values(iterable) {
197
- if (iterable) {
198
- var iteratorMethod = iterable[iteratorSymbol];
199
- if (iteratorMethod) return iteratorMethod.call(iterable);
200
- if ("function" == typeof iterable.next) return iterable;
201
- if (!isNaN(iterable.length)) {
202
- var i = -1,
203
- next = function next() {
204
- for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
205
- return next.value = undefined, next.done = !0, next;
206
- };
207
- return next.next = next;
208
- }
158
+ if (scriptStatus === ScriptStatus.READY && !(children || config)) {
159
+ fetchConfig();
209
160
  }
210
- return {
211
- next: doneResult
212
- };
213
- }
214
- function doneResult() {
215
- return {
216
- value: undefined,
217
- done: !0
218
- };
219
- }
220
- return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", {
221
- value: GeneratorFunctionPrototype,
222
- configurable: !0
223
- }), defineProperty(GeneratorFunctionPrototype, "constructor", {
224
- value: GeneratorFunction,
225
- configurable: !0
226
- }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
227
- var ctor = "function" == typeof genFun && genFun.constructor;
228
- return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
229
- }, exports.mark = function (genFun) {
230
- return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
231
- }, exports.awrap = function (arg) {
232
- return {
233
- __await: arg
234
- };
235
- }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
236
- return this;
237
- }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
238
- void 0 === PromiseImpl && (PromiseImpl = Promise);
239
- var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
240
- return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
241
- return result.done ? result.value : iter.next();
242
- });
243
- }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
244
- return this;
245
- }), define(Gp, "toString", function () {
246
- return "[object Generator]";
247
- }), exports.keys = function (val) {
248
- var object = Object(val),
249
- keys = [];
250
- for (var key in object) keys.push(key);
251
- return keys.reverse(), function next() {
252
- for (; keys.length;) {
253
- var key = keys.pop();
254
- if (key in object) return next.value = key, next.done = !1, next;
161
+ }, [scriptStatus]);
162
+ useEffect(() => {
163
+ const submitSelected = async () => {
164
+ try {
165
+ // TODO: Switch to window.__twpdeu.push
166
+ // TODO: Log to GA
167
+ const result = await ingest({
168
+ submitData: {
169
+ fieldName,
170
+ value: [selected]
171
+ },
172
+ type: config !== null && config !== void 0 && config.explicit ? IngestType.EXPLICIT : IngestType.IMPLICIT,
173
+ source
174
+ });
175
+ const isError = result ? result.status !== ResponseStatus.SUCCESS : true;
176
+ onFinished({
177
+ isFinished: true,
178
+ isError
179
+ });
180
+ } catch (e) {
181
+ onFinished({
182
+ isFinished: false,
183
+ isError: true
184
+ });
255
185
  }
256
- return next.done = !0, next;
257
186
  };
258
- }, exports.values = values, Context.prototype = {
259
- constructor: Context,
260
- reset: function (skipTempReset) {
261
- if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
262
- },
263
- stop: function () {
264
- this.done = !0;
265
- var rootRecord = this.tryEntries[0].completion;
266
- if ("throw" === rootRecord.type) throw rootRecord.arg;
267
- return this.rval;
268
- },
269
- dispatchException: function (exception) {
270
- if (this.done) throw exception;
271
- var context = this;
272
- function handle(loc, caught) {
273
- return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
274
- }
275
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
276
- var entry = this.tryEntries[i],
277
- record = entry.completion;
278
- if ("root" === entry.tryLoc) return handle("end");
279
- if (entry.tryLoc <= this.prev) {
280
- var hasCatch = hasOwn.call(entry, "catchLoc"),
281
- hasFinally = hasOwn.call(entry, "finallyLoc");
282
- if (hasCatch && hasFinally) {
283
- if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
284
- if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
285
- } else if (hasCatch) {
286
- if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
287
- } else {
288
- if (!hasFinally) throw new Error("try statement without catch or finally");
289
- if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
290
- }
291
- }
292
- }
293
- },
294
- abrupt: function (type, arg) {
295
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
296
- var entry = this.tryEntries[i];
297
- if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
298
- var finallyEntry = entry;
299
- break;
300
- }
301
- }
302
- finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
303
- var record = finallyEntry ? finallyEntry.completion : {};
304
- return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
305
- },
306
- complete: function (record, afterLoc) {
307
- if ("throw" === record.type) throw record.arg;
308
- return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel;
309
- },
310
- finish: function (finallyLoc) {
311
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
312
- var entry = this.tryEntries[i];
313
- if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
314
- }
315
- },
316
- catch: function (tryLoc) {
317
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
318
- var entry = this.tryEntries[i];
319
- if (entry.tryLoc === tryLoc) {
320
- var record = entry.completion;
321
- if ("throw" === record.type) {
322
- var thrown = record.arg;
323
- resetTryEntry(entry);
324
- }
325
- return thrown;
326
- }
327
- }
328
- throw new Error("illegal catch attempt");
329
- },
330
- delegateYield: function (iterable, resultName, nextLoc) {
331
- return this.delegate = {
332
- iterator: values(iterable),
333
- resultName: resultName,
334
- nextLoc: nextLoc
335
- }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
187
+ if (submit && selected) {
188
+ submitSelected();
336
189
  }
337
- }, exports;
338
- }
339
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
340
- try {
341
- var info = gen[key](arg);
342
- var value = info.value;
343
- } catch (error) {
344
- reject(error);
345
- return;
346
- }
347
- if (info.done) {
348
- resolve(value);
349
- } else {
350
- Promise.resolve(value).then(_next, _throw);
351
- }
352
- }
353
- function _asyncToGenerator(fn) {
354
- return function () {
355
- var self = this,
356
- args = arguments;
357
- return new Promise(function (resolve, reject) {
358
- var gen = fn.apply(self, args);
359
- function _next(value) {
360
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
361
- }
362
- function _throw(err) {
363
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
364
- }
365
- _next(undefined);
366
- });
367
- };
368
- }
369
- function _extends() {
370
- _extends = Object.assign ? Object.assign.bind() : function (target) {
371
- for (var i = 1; i < arguments.length; i++) {
372
- var source = arguments[i];
373
- for (var key in source) {
374
- if (Object.prototype.hasOwnProperty.call(source, key)) {
375
- target[key] = source[key];
376
- }
377
- }
378
- }
379
- return target;
380
- };
381
- return _extends.apply(this, arguments);
382
- }
383
-
384
- var base = ENDPOINTS.base + "/de/v1";
385
- var attributesCache = {};
386
- var getAttributes = /*#__PURE__*/function () {
387
- var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
388
- var fieldName, fieldNames, url, data, json, attributes;
389
- return _regeneratorRuntime().wrap(function _callee$(_context) {
390
- while (1) switch (_context.prev = _context.next) {
391
- case 0:
392
- fieldName = _ref.fieldName;
393
- if (!attributesCache[fieldName]) {
394
- _context.next = 3;
395
- break;
396
- }
397
- return _context.abrupt("return", attributesCache[fieldName]);
398
- case 3:
399
- fieldNames = [fieldName];
400
- _context.prev = 4;
401
- url = new URL(base + "/attributes");
402
- url.searchParams.set('attributes', fieldNames.join(','));
403
- _context.next = 9;
404
- return fetch(url.toString(), {
405
- credentials: 'include',
406
- headers: JSON_HEADERS
407
- });
408
- case 9:
409
- data = _context.sent;
410
- _context.next = 12;
411
- return data.json();
412
- case 12:
413
- json = _context.sent;
414
- if (!(data.ok && json.status === ResponseStatus.SUCCESS)) {
415
- _context.next = 19;
416
- break;
417
- }
418
- attributes = json.attributes || [];
419
- attributesCache[fieldName] = attributes;
420
- return _context.abrupt("return", attributes);
421
- case 19:
422
- return _context.abrupt("return", []);
423
- case 20:
424
- _context.next = 26;
425
- break;
426
- case 22:
427
- _context.prev = 22;
428
- _context.t0 = _context["catch"](4);
429
- console.debug(_context.t0);
430
- return _context.abrupt("return", []);
431
- case 26:
432
- case "end":
433
- return _context.stop();
434
- }
435
- }, _callee, null, [[4, 22]]);
436
- }));
437
- return function getAttributes(_x) {
438
- return _ref2.apply(this, arguments);
439
- };
440
- }();
441
- var ingest = /*#__PURE__*/function () {
442
- var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref3) {
443
- var _ref3$submitData, fieldName, value, source, url, wapo_login_id, attributeInfo, jucid, ga, payload, response, json;
444
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
445
- while (1) switch (_context2.prev = _context2.next) {
446
- case 0:
447
- _ref3$submitData = _ref3.submitData, fieldName = _ref3$submitData.fieldName, value = _ref3$submitData.value, source = _ref3.source;
448
- url = base + "/ingest";
449
- wapo_login_id = getCookie('wapo_login_id');
450
- if (hasRequiredPrivacyCookies()) {
451
- _context2.next = 5;
452
- break;
453
- }
454
- throw new Error('does not satisfy cookie check');
455
- case 5:
456
- attributeInfo = attributesCache[fieldName];
457
- if (attributeInfo) {
458
- _context2.next = 10;
459
- break;
460
- }
461
- _context2.next = 9;
462
- return getAttributes({
463
- fieldName: fieldName
464
- });
465
- case 9:
466
- attributeInfo = _context2.sent;
467
- case 10:
468
- if (!(attributeInfo[0] && attributeInfo[0].name === fieldName && attributeInfo[0].collection_behavior === CollectionBehaviors.DO_NOT_COLLECT)) {
469
- _context2.next = 12;
470
- break;
471
- }
472
- throw new Error('do not collect');
473
- case 12:
474
- jucid = localStorage.getItem('uuid');
475
- ga = getCookie('_ga');
476
- payload = {
477
- jucid: jucid,
478
- ga: ga,
479
- type: IngestType.EXPLICIT,
480
- wapo_login_id: wapo_login_id,
481
- data: {
482
- fieldName: fieldName,
483
- value: value
484
- },
485
- metadata: {
486
- source: source
487
- }
488
- };
489
- _context2.prev = 15;
490
- _context2.next = 18;
491
- return fetch(url, {
492
- method: 'POST',
493
- credentials: 'include',
494
- headers: JSON_HEADERS,
495
- body: JSON.stringify(payload)
496
- });
497
- case 18:
498
- response = _context2.sent;
499
- _context2.next = 21;
500
- return response.json();
501
- case 21:
502
- json = _context2.sent;
503
- return _context2.abrupt("return", json);
504
- case 25:
505
- _context2.prev = 25;
506
- _context2.t0 = _context2["catch"](15);
507
- console.debug(_context2.t0);
508
- return _context2.abrupt("return", null);
509
- case 29:
510
- case "end":
511
- return _context2.stop();
512
- }
513
- }, _callee2, null, [[15, 25]]);
514
- }));
515
- return function ingest(_x2) {
516
- return _ref4.apply(this, arguments);
517
- };
518
- }();
519
-
520
- var DESelect = function DESelect(_ref) {
521
- var source = _ref.source,
522
- fieldName = _ref.fieldName,
523
- label = _ref.label,
524
- dataDictionaryConfig = _ref.dataDictionaryConfig,
525
- defaultValue = _ref.defaultValue,
526
- disabled = _ref.disabled,
527
- submit = _ref.submit,
528
- _ref$onChange = _ref.onChange,
529
- onChange = _ref$onChange === void 0 ? function () {} : _ref$onChange,
530
- _ref$onFinished = _ref.onFinished,
531
- onFinished = _ref$onFinished === void 0 ? function () {} : _ref$onFinished,
532
- _ref$valuesFilter = _ref.valuesFilter,
533
- valuesFilter = _ref$valuesFilter === void 0 ? function () {
534
- return true;
535
- } : _ref$valuesFilter,
536
- children = _ref.children;
537
- var _useState = useState(dataDictionaryConfig),
538
- config = _useState[0],
539
- setConfig = _useState[1];
540
- var _useState2 = useState(''),
541
- selected = _useState2[0],
542
- setSelected = _useState2[1];
543
- useEffect(function () {
544
- if (children) {
545
- if (process.env.NODE_ENV !== "production") {
546
- console.debug('childen props', children);
547
- }
548
- return;
549
- }
550
- if (!config) {
551
- _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
552
- var config;
553
- return _regeneratorRuntime().wrap(function _callee$(_context) {
554
- while (1) switch (_context.prev = _context.next) {
555
- case 0:
556
- _context.next = 2;
557
- return getAttributes({
558
- fieldName: fieldName
559
- });
560
- case 2:
561
- config = _context.sent;
562
- if (process.env.NODE_ENV !== "production") {
563
- console.debug('config from API', config);
564
- }
565
- setConfig(config[0]);
566
- case 5:
567
- case "end":
568
- return _context.stop();
569
- }
570
- }, _callee);
571
- }))();
572
- }
573
- }, []);
574
- useEffect(function () {
575
- _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
576
- var result, isError;
577
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
578
- while (1) switch (_context2.prev = _context2.next) {
579
- case 0:
580
- if (!(submit && selected)) {
581
- _context2.next = 6;
582
- break;
583
- }
584
- _context2.next = 3;
585
- return ingest({
586
- submitData: {
587
- fieldName: fieldName,
588
- value: selected
589
- },
590
- source: source
591
- });
592
- case 3:
593
- result = _context2.sent;
594
- isError = result ? result.status !== ResponseStatus.SUCCESS : true;
595
- onFinished({
596
- isFinished: true,
597
- isError: isError
598
- });
599
- case 6:
600
- case "end":
601
- return _context2.stop();
602
- }
603
- }, _callee2);
604
- }))();
605
190
  }, [submit, selected]);
606
191
  if (!(children || config)) {
607
192
  return React.createElement("span", null, "loading");
608
193
  }
609
- var defaultValueProp = defaultValue ? {
610
- defaultValue: defaultValue
194
+ const defaultValueProp = defaultValue ? {
195
+ defaultValue
611
196
  } : {};
612
- var disabledProp = disabled ? {
197
+ const disabledProp = disabled ? {
613
198
  disabled: true
614
199
  } : {};
615
200
  // sort and filter out archived values
616
- var values = config ? config.values.sort(function (a, b) {
617
- return a.order - b.order;
618
- }).filter(function (value) {
619
- return value.archived !== true;
620
- }).filter(valuesFilter) : [];
621
- return React.createElement(SelectWrapper, null, React.createElement(Select.Root, _extends({
622
- onValueChange: function onValueChange(e) {
201
+ const values = config ? config.values.sort((a, b) => a.order - b.order).filter(value => value.archived !== true).filter(valuesFilter) : [];
202
+ return React.createElement(SelectWrapper, null, React.createElement(Select.Root, {
203
+ onValueChange: e => {
623
204
  onChange({
624
205
  value: e
625
206
  });
626
207
  setSelected(e);
627
- }
628
- }, defaultValueProp, disabledProp), children ? children : null, !children && config && React.createElement(React.Fragment, null, React.createElement(Select.Trigger, {
629
- "data-test-id": config.name + "-select-trigger"
208
+ },
209
+ ...defaultValueProp,
210
+ ...disabledProp
211
+ }, children ? children : null, !children && config && React.createElement(React.Fragment, null, React.createElement(Select.Trigger, {
212
+ "data-test-id": `${config.name}-select-trigger`
630
213
  }, React.createElement(Select.Label, null, label || config.name), React.createElement(Select.Value, null)), React.createElement(Select.Content, {
631
214
  css: {
632
215
  zIndex: theme.zIndices.page
633
216
  },
634
- "data-test-id": config.name + "-select-content"
635
- }, values.map(function (value) {
636
- return React.createElement(Select.Item, {
637
- value: value.name,
638
- key: value.name
639
- }, value.name);
640
- })))));
217
+ "data-test-id": `${config.name}-select-content`
218
+ }, values.map(value => React.createElement(Select.Item, {
219
+ value: value.name,
220
+ key: value.name
221
+ }, value.name))))));
641
222
  };
642
- var SelectWrapper = /*#__PURE__*/styled('div', {
223
+ const SelectWrapper = /*#__PURE__*/styled('div', {
643
224
  boxSizing: 'border-box',
644
225
  display: 'flex',
645
226
  marginBottom: '$100',