@ukhomeoffice/cop-react-form-renderer 6.14.9 → 6.14.11

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.
@@ -8,10 +8,6 @@ var _axios = _interopRequireDefault(require("axios"));
8
8
  var _react = require("react");
9
9
  var _useAxios = _interopRequireDefault(require("./useAxios"));
10
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
- // Global imports
12
-
13
- // Local imports
14
-
15
11
  // Caches for responses and errors.
16
12
  const cache = {};
17
13
  const errorCache = {};
@@ -31,15 +27,18 @@ const STATUS_IDLE = exports.STATUS_IDLE = 'idle';
31
27
  const STATUS_FETCHING = exports.STATUS_FETCHING = 'fetching';
32
28
  const STATUS_FETCHED = exports.STATUS_FETCHED = 'fetched';
33
29
  const STATUS_ERROR = exports.STATUS_ERROR = 'error';
34
- const useGetRequest = url => {
30
+ const useGetRequest = function (url) {
31
+ let caching = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
35
32
  const axiosInstance = (0, _useAxios.default)();
36
- const cancelToken = _axios.default.CancelToken.source();
37
- const cancelRequests = () => {
38
- if (cancelToken) cancelToken.cancel();
39
- };
33
+ const cancelTokenRef = (0, _react.useRef)(_axios.default.CancelToken.source());
40
34
  const [status, setStatus] = (0, _react.useState)(STATUS_IDLE);
41
35
  const [error, setError] = (0, _react.useState)(null);
42
36
  const [data, setData] = (0, _react.useState)(null);
37
+ const cancelRequests = () => {
38
+ if (cancelTokenRef.current) {
39
+ cancelTokenRef.current.cancel();
40
+ }
41
+ };
43
42
  (0, _react.useEffect)(() => {
44
43
  if (!url || !axiosInstance) return;
45
44
  const fetchData = async () => {
@@ -47,9 +46,9 @@ const useGetRequest = url => {
47
46
  setError(null);
48
47
  setStatus(STATUS_FETCHING);
49
48
  let fetchedData;
50
- if (cache[url]) {
49
+ if (caching && cache[url]) {
51
50
  fetchedData = cache[url];
52
- } else if (errorCache[url]) {
51
+ } else if (caching && errorCache[url]) {
53
52
  /**
54
53
  * This logic is intended to stop multiple requests being made in succession
55
54
  * that all fail. Presently, this will only allow the first request to be
@@ -60,24 +59,25 @@ const useGetRequest = url => {
60
59
  throw errorCache[url];
61
60
  } else {
62
61
  const response = await axiosInstance.get(url, {
63
- cancelToken: cancelToken.token
62
+ cancelToken: cancelTokenRef.current.token
64
63
  }).catch(e => {
64
+ setError(e);
65
65
  throw e;
66
66
  });
67
67
  fetchedData = response.data;
68
- cache[url] = fetchedData;
68
+ if (caching) cache[url] = fetchedData;
69
69
  }
70
70
  setData(fetchedData);
71
71
  setStatus(STATUS_FETCHED);
72
72
  } catch (e) {
73
- errorCache[url] = e;
73
+ if (caching) errorCache[url] = e;
74
74
  setError(e);
75
75
  setData(null);
76
76
  setStatus(STATUS_ERROR);
77
77
  }
78
78
  };
79
79
  fetchData();
80
- }, [axiosInstance, url, cancelToken.token]);
80
+ }, [axiosInstance, url, caching]);
81
81
  return {
82
82
  status,
83
83
  error,
@@ -28,13 +28,14 @@ const getRefDataUrl = component => {
28
28
  return undefined;
29
29
  };
30
30
  const useRefData = (component, formData) => {
31
+ var _component$data;
31
32
  const url = getRefDataUrl(_objectSpread(_objectSpread({}, component), {}, {
32
33
  formData
33
34
  }));
34
35
  const {
35
36
  status: _status,
36
37
  data: _data
37
- } = (0, _useGetRequest.default)(url);
38
+ } = (0, _useGetRequest.default)(url, component === null || component === void 0 || (_component$data = component.data) === null || _component$data === void 0 ? void 0 : _component$data.useCache);
38
39
  const [data, setData] = (0, _react.useState)([]);
39
40
  const [status, setStatus] = (0, _react.useState)(STATUS_LOADING);
40
41
  (0, _react.useEffect)(() => {
@@ -52,7 +53,7 @@ const useRefData = (component, formData) => {
52
53
  setData([]);
53
54
  setStatus(STATUS_COMPLETE);
54
55
  }
55
- }, [component, _status, _data, url, setData, setStatus]);
56
+ }, [component.id, _status, _data, url]);
56
57
  return {
57
58
  data,
58
59
  status
@@ -23,7 +23,8 @@ describe('utils.Component.get', () => {
23
23
  fieldId: FIELD_ID,
24
24
  label: LABEL,
25
25
  onChange: ON_CHANGE,
26
- 'data-testid': ID
26
+ 'data-testid': ID,
27
+ allowedTypes: []
27
28
  };
28
29
  const {
29
30
  container
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _copReactComponents = require("@ukhomeoffice/cop-react-components");
8
+ var _getSourceData = _interopRequireDefault(require("../Data/getSourceData"));
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ // Global imports.
11
+
12
+ // Local imports.
13
+
14
+ /**
15
+ * Returns if it can find a value in the form data. supports searching in arrays and objects
16
+ * @param {object} config The config of the operation.
17
+ * @param {string} config.target the JPath to the object/array that contains the data we want to checkon
18
+ * @param {string} config.key the JPath to the field we want to check
19
+ * @param {string} config.value the value that we want to check the the data against
20
+ * @param {object} data A page's formData.
21
+ * @returns The index of the matching value if one exists, or null.
22
+ */
23
+ const doesContainValue = (config, data) => {
24
+ const targetPath = _copReactComponents.Utils.interpolateString(config.target, data);
25
+ const target = (0, _getSourceData.default)(data, targetPath);
26
+ if (target && Array.isArray(target)) {
27
+ return target.some(entry => config.key ? (0, _getSourceData.default)(entry, config.key) === config.value : entry === config.value);
28
+ }
29
+ if (target && typeof target === "object") {
30
+ return (0, _getSourceData.default)(target, config.key) === config.value;
31
+ }
32
+ return false;
33
+ };
34
+ var _default = exports.default = doesContainValue;
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+
3
+ var _doesContainValue = _interopRequireDefault(require("./doesContainValue"));
4
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
5
+ describe('doesContainValue', () => {
6
+ const mockData = {
7
+ arrayOfValues: ['abc', 'bcd', 'cde', 'def', {
8
+ root: {
9
+ branch: 'leaf'
10
+ }
11
+ }],
12
+ cutoffIndex: 1,
13
+ ignoreIndex: 2,
14
+ valueToSearchFor: 'cde',
15
+ targetName: 'arrayOfValues',
16
+ fieldName: 'valueToSearchFor',
17
+ cutoffName: 'cutoffIndex',
18
+ ignoreName: 'ignoreIndex'
19
+ };
20
+ test('returns true if value exists in array', () => {
21
+ const config = {
22
+ target: 'arrayOfValues',
23
+ value: 'cde'
24
+ };
25
+ const result = (0, _doesContainValue.default)(config, mockData);
26
+ expect(result).toBe(true);
27
+ });
28
+ test('returns false if value does not exist in array', () => {
29
+ const config = {
30
+ target: 'arrayOfValues',
31
+ value: 'xyz'
32
+ };
33
+ const result = (0, _doesContainValue.default)(config, mockData);
34
+ expect(result).toBe(false);
35
+ });
36
+ test('returns true if key-value pair matches in array object', () => {
37
+ const config = {
38
+ target: 'arrayOfValues',
39
+ key: 'root.branch',
40
+ value: 'leaf'
41
+ };
42
+ const result = (0, _doesContainValue.default)(config, mockData);
43
+ expect(result).toBe(true);
44
+ });
45
+ test('returns false if key-value pair does not match in array object', () => {
46
+ const config = {
47
+ target: 'arrayOfValues',
48
+ key: 'root.branch',
49
+ value: 'notLeaf'
50
+ };
51
+ const result = (0, _doesContainValue.default)(config, mockData);
52
+ expect(result).toBe(false);
53
+ });
54
+ test('handles non-array targets gracefully', () => {
55
+ const config = {
56
+ target: 'nonExistentTarget',
57
+ value: 'value'
58
+ };
59
+ const result = (0, _doesContainValue.default)(config, mockData);
60
+ expect(result).toBe(false);
61
+ });
62
+ test('handles objects as target correctly', () => {
63
+ const config = {
64
+ target: 'objectTarget',
65
+ key: 'key',
66
+ value: 'value'
67
+ };
68
+ const result = (0, _doesContainValue.default)(config, {
69
+ objectTarget: {
70
+ key: 'value'
71
+ }
72
+ });
73
+ expect(result).toBe(true);
74
+ });
75
+ });
@@ -14,6 +14,7 @@ var _shouldRun = _interopRequireDefault(require("./shouldRun"));
14
14
  var _setDataItem = _interopRequireDefault(require("../Data/setDataItem"));
15
15
  var _getFirstOf = _interopRequireDefault(require("./getFirstOf"));
16
16
  var _getLength = _interopRequireDefault(require("./getLength"));
17
+ var _doesContainValue = _interopRequireDefault(require("./doesContainValue"));
17
18
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
18
19
  // Global imports.
19
20
 
@@ -22,6 +23,7 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
22
23
  const functions = {
23
24
  checkValueIsTruthy: _checkValueIsTruthy.default,
24
25
  getIndexOfMatchingValueIn: _getIndexOfMatchingValueIn.default,
26
+ doesContainValue: _doesContainValue.default,
25
27
  persistValueInFormData: _persistValueInFormData.default,
26
28
  setValueInFormData: _setValueInFormData.default,
27
29
  deleteValueInFormData: _deleteValueInFormData.default,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "6.14.9",
3
+ "version": "6.14.11",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",
@@ -16,12 +16,12 @@
16
16
  "yalc-publish": "yarn compile-with-maps && cp -r src dist/src && yalc publish --push"
17
17
  },
18
18
  "dependencies": {
19
- "@ukhomeoffice/cop-react-components": "4.7.11",
19
+ "@ukhomeoffice/cop-react-components": "4.7.13",
20
20
  "axios": "^0.23.0",
21
21
  "dayjs": "^1.11.0",
22
22
  "govuk-frontend": "^5.0.0",
23
- "web-vitals": "^1.0.1",
24
- "uuid": "^8.1.0"
23
+ "uuid": "^8.1.0",
24
+ "web-vitals": "^1.0.1"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@babel/cli": "^7.15.4",