@micromag/core 0.3.707 → 0.3.709

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 (2) hide show
  1. package/es/contexts.js +39 -23
  2. package/package.json +2 -2
package/es/contexts.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import _objectSpread from '@babel/runtime/helpers/objectSpread2';
2
2
  import isString from 'lodash/isString';
3
3
  import PropTypes from 'prop-types';
4
- import React, { useContext, useMemo, useState, useEffect, useCallback, useRef } from 'react';
4
+ import React, { useContext, useMemo, useState, useCallback, useEffect, useRef } from 'react';
5
5
  import { ComponentsManager, PropTypes as PropTypes$1, FieldsManager, ScreensManager, ColorsParser, Tracking } from '@micromag/core';
6
6
  import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
7
7
  import JSCookie from 'js-cookie';
@@ -321,16 +321,29 @@ var ConsentProvider = function ConsentProvider(_ref) {
321
321
  initialConsented = _ref.consented,
322
322
  expiration = _ref.expiration,
323
323
  children = _ref.children;
324
- var cookieConsented = JSCookie.get('has_consented') === 'true';
325
- var _useState = useState(initialConsented || cookieConsented),
324
+ // Has consented or not to cookies
325
+ var initialCookieConsented = JSCookie.get('has_consented') === 'true';
326
+ var baseConsented = initialConsented || initialCookieConsented;
327
+ var _useState = useState(baseConsented),
326
328
  _useState2 = _slicedToArray(_useState, 2),
327
329
  consented = _useState2[0],
328
- setConsented = _useState2[1];
330
+ setConsentedState = _useState2[1];
331
+ var setConsented = useCallback(function () {
332
+ var accept = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
333
+ var hasConsented = accept || false;
334
+ JSCookie.set('has_consented', hasConsented, {
335
+ secure: true,
336
+ expires: expiration
337
+ });
338
+ setConsentedState(hasConsented);
339
+ }, [expiration, setConsentedState]);
329
340
  useEffect(function () {
330
- if (initialConsented) {
341
+ if (initialConsented === true || initialConsented === false) {
331
342
  setConsented(initialConsented);
332
343
  }
333
344
  }, [initialConsented, setConsented]);
345
+
346
+ // The consent state itself
334
347
  var baseConsent = useMemo(function () {
335
348
  return (providedConsent || consentStates || []).map(function (item) {
336
349
  if (isString(item)) {
@@ -349,47 +362,50 @@ var ConsentProvider = function ConsentProvider(_ref) {
349
362
  });
350
363
  });
351
364
  }, [providedConsent]);
352
- var _useState3 = useState(baseConsent),
365
+ var _useState3 = useState(null),
353
366
  _useState4 = _slicedToArray(_useState3, 2),
354
367
  consent = _useState4[0],
355
368
  setConsentState = _useState4[1];
356
- useEffect(function () {
357
- if (baseConsent) {
358
- setConsentState(baseConsent);
359
- }
360
- }, [baseConsent, setConsentState]);
361
- var setConsent = useCallback(function (values) {
362
- var hasConsented = values !== null && typeof values !== 'undefined';
363
- JSCookie.set('has_consented', hasConsented, {
364
- secure: true,
365
- expires: expiration
366
- });
367
- setConsented(hasConsented);
368
- (values || []).forEach(function (it) {
369
+ var setConsent = useCallback(function () {
370
+ var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
371
+ var initial = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
372
+ var tagManagerConsent = (values || []).reduce(function (acc, it) {
369
373
  if (it.value === true) {
370
374
  JSCookie.set(it.id, 'granted', {
371
375
  secure: true,
372
376
  expires: expiration
373
377
  });
378
+ acc[it.id] = 'granted';
374
379
  } else if (it.value === false) {
375
380
  JSCookie.set(it.id, 'denied', {
376
381
  secure: true,
377
382
  expires: expiration
378
383
  });
384
+ acc[it.id] = 'denied';
379
385
  } else {
380
386
  JSCookie.remove(it.id);
387
+ acc[it.id] = 'denied';
381
388
  }
382
- });
389
+ return acc;
390
+ }, {});
391
+ if (typeof gtag === 'function') {
392
+ gtag('consent', initial === true ? 'default' : 'update', tagManagerConsent);
393
+ }
383
394
  setConsentState(values);
384
- return values;
385
395
  }, [setConsentState, expiration]);
396
+ useEffect(function () {
397
+ if (baseConsent !== null && baseConsent.length > 0) {
398
+ setConsent(baseConsent);
399
+ }
400
+ }, [baseConsent, setConsent]);
386
401
  var value = useMemo(function () {
387
402
  return {
388
403
  consent: consent,
389
404
  setConsent: setConsent,
390
- consented: consented
405
+ consented: consented,
406
+ setConsented: setConsented
391
407
  };
392
- }, [consent, setConsent, consented]);
408
+ }, [consent, setConsent, consented, setConsented]);
393
409
  return /*#__PURE__*/React.createElement(ConsentContext.Provider, {
394
410
  value: value
395
411
  }, children);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micromag/core",
3
- "version": "0.3.707",
3
+ "version": "0.3.709",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "keywords": [
@@ -145,5 +145,5 @@
145
145
  "access": "public",
146
146
  "registry": "https://registry.npmjs.org/"
147
147
  },
148
- "gitHead": "e3afa5fe973a68bf83383fede41590a5e6428259"
148
+ "gitHead": "367f9fd8ecf298da947c017e9a0434e34c90e248"
149
149
  }