@ukhomeoffice/cop-react-form-renderer 6.14.10 → 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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "6.14.10",
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.10",
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",