From b30f9f59b4f421769900ce534c6074489419bd10 Mon Sep 17 00:00:00 2001 From: Maciej Lenartowicz Date: Fri, 15 Nov 2019 08:55:24 +0000 Subject: [PATCH 1/8] Fix for withEither --- package-lock.json | 2 +- package.json | 2 +- src/bootstrap/Spinner.js | 6 +++--- src/utils/conditionalHOCs.js | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index c8ff325..3669a8d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "foris", - "version": "1.3.1", + "version": "1.3.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 2e72014..0f1b608 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "foris", - "version": "1.3.1", + "version": "1.3.2", "description": "Set of components and utils for Foris and its plugins.", "author": "CZ.NIC, z.s.p.o.", "repository": { diff --git a/src/bootstrap/Spinner.js b/src/bootstrap/Spinner.js index 43ca59f..33cca31 100644 --- a/src/bootstrap/Spinner.js +++ b/src/bootstrap/Spinner.js @@ -24,18 +24,18 @@ Spinner.defaultProps = { }; export function Spinner({ - fullScreen, children, className, ...props + fullScreen, children, className, }) { if (!fullScreen) { return ( -
+
{children}
); } return ( -
+
{children}
diff --git a/src/utils/conditionalHOCs.js b/src/utils/conditionalHOCs.js index 4c38fb4..fcb4978 100644 --- a/src/utils/conditionalHOCs.js +++ b/src/utils/conditionalHOCs.js @@ -14,7 +14,7 @@ import { ErrorMessage } from "./ErrorMessage"; function withEither(conditionalFn, Either) { return (Component) => (props) => { if (conditionalFn(props)) { - return ; + return ; } return ; }; From 2e68e56e44ef1617b1226d8a2a6727fb0f01f097 Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Fri, 15 Nov 2019 12:54:12 +0100 Subject: [PATCH 2/8] Add hook for API polling. --- src/api/hooks.js | 24 +++++++++++++++++++++++- src/index.js | 7 ++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/api/hooks.js b/src/api/hooks.js index da236d0..f3f5746 100644 --- a/src/api/hooks.js +++ b/src/api/hooks.js @@ -5,7 +5,9 @@ * See /LICENSE for more information. */ -import { useCallback, useReducer } from "react"; +import { + useCallback, useEffect, useReducer, useState, +} from "react"; import { ForisURLs } from "forisUrls"; import { @@ -90,3 +92,23 @@ const useAPIDelete = createAPIHook("DELETE"); export { useAPIGet, useAPIPost, useAPIPatch, useAPIPut, useAPIDelete, }; + +export function useAPIPolling(endpoint, delay) { + const [state, setState] = useState({ state: API_STATE.INIT }); + const [getState, get] = useAPIGet(endpoint); + + useEffect(() => { + if (getState.state === API_STATE.SUCCESS) { + setState(getState); + } + }, [getState]); + + useEffect(() => { + if (delay !== null) { + const interval = setInterval(get, delay); + return () => clearInterval(interval); + } + }, [delay, get]); + + return [state]; +} diff --git a/src/index.js b/src/index.js index 5f755d9..9a51089 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,12 @@ // API export { - useAPIGet, useAPIPost, useAPIPatch, useAPIPut, useAPIDelete, + useAPIGet, + useAPIPost, + useAPIPatch, + useAPIPut, + useAPIDelete, + useAPIPolling, } from "api/hooks"; export { API_STATE } from "api/utils"; From 73e213c46788be024823afa90df88ecc444a028e Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Fri, 15 Nov 2019 14:00:07 +0100 Subject: [PATCH 3/8] Add default delay. --- src/api/hooks.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/api/hooks.js b/src/api/hooks.js index f3f5746..e873ef7 100644 --- a/src/api/hooks.js +++ b/src/api/hooks.js @@ -5,14 +5,10 @@ * See /LICENSE for more information. */ -import { - useCallback, useEffect, useReducer, useState, -} from "react"; +import { useCallback, useEffect, useReducer, useState, } from "react"; import { ForisURLs } from "forisUrls"; -import { - API_ACTIONS, API_METHODS, API_STATE, getErrorPayload, HEADERS, TIMEOUT, -} from "./utils"; +import { API_ACTIONS, API_METHODS, API_STATE, getErrorPayload, HEADERS, TIMEOUT, } from "./utils"; const DATA_METHODS = ["POST", "PATCH", "PUT"]; @@ -93,7 +89,7 @@ export { useAPIGet, useAPIPost, useAPIPatch, useAPIPut, useAPIDelete, }; -export function useAPIPolling(endpoint, delay) { +export function useAPIPolling(endpoint, delay = 1000) { // delay ms const [state, setState] = useState({ state: API_STATE.INIT }); const [getState, get] = useAPIGet(endpoint); From 429814ebb587b76e22072cb3ecab31b3a2d5a0e3 Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Fri, 15 Nov 2019 14:16:37 +0100 Subject: [PATCH 4/8] Add until param to API polling hook. --- src/api/hooks.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/api/hooks.js b/src/api/hooks.js index e873ef7..47cc71d 100644 --- a/src/api/hooks.js +++ b/src/api/hooks.js @@ -5,10 +5,14 @@ * See /LICENSE for more information. */ -import { useCallback, useEffect, useReducer, useState, } from "react"; +import { + useCallback, useEffect, useReducer, useState, +} from "react"; import { ForisURLs } from "forisUrls"; -import { API_ACTIONS, API_METHODS, API_STATE, getErrorPayload, HEADERS, TIMEOUT, } from "./utils"; +import { + API_ACTIONS, API_METHODS, API_STATE, getErrorPayload, HEADERS, TIMEOUT, +} from "./utils"; const DATA_METHODS = ["POST", "PATCH", "PUT"]; @@ -89,7 +93,7 @@ export { useAPIGet, useAPIPost, useAPIPatch, useAPIPut, useAPIDelete, }; -export function useAPIPolling(endpoint, delay = 1000) { // delay ms +export function useAPIPolling(endpoint, delay = 1000, until) { // delay ms const [state, setState] = useState({ state: API_STATE.INIT }); const [getState, get] = useAPIGet(endpoint); @@ -100,11 +104,11 @@ export function useAPIPolling(endpoint, delay = 1000) { // delay ms }, [getState]); useEffect(() => { - if (delay !== null) { + if (until) { const interval = setInterval(get, delay); return () => clearInterval(interval); } - }, [delay, get]); + }, [until, delay, get]); return [state]; } From e54db2c57783ea6cc45f34029a5a50612619d09c Mon Sep 17 00:00:00 2001 From: Maciej Lenartowicz Date: Mon, 18 Nov 2019 12:51:43 +0000 Subject: [PATCH 5/8] Use global AlertContext --- package-lock.json | 2 ++ package.json | 11 ++++++----- src/alertContext/AlertContext.js | 6 +++--- src/index.js | 2 +- src/testUtils/alertContextMock.js | 4 ++-- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3669a8d..eaf38e3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6269,6 +6269,7 @@ "version": "3.0.1", "resolved": "https://registry.npmjs.org/immutability-helper/-/immutability-helper-3.0.1.tgz", "integrity": "sha512-U92ROQQt7XkIwrdqCByUI118TQM1hXdKnRQpvKeA0HRyGSnJipu9IWHe4UD8zCN00O8UnQjQzPCgZ1CC3yBzHA==", + "dev": true, "requires": { "invariant": "^2.2.4" } @@ -6402,6 +6403,7 @@ "version": "2.2.4", "resolved": "https://registry.npmjs.org/invariant/-/invariant-2.2.4.tgz", "integrity": "sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA==", + "dev": true, "requires": { "loose-envify": "^1.0.0" } diff --git a/package.json b/package.json index 0f1b608..61158a0 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,6 @@ "main": "index.js", "dependencies": { "axios": "^0.19.0", - "immutability-helper": "^3.0.0", "jest-transform-css": "^2.0.0", "moment": "^2.24.0", "moment-timezone": "^0.5.25", @@ -25,8 +24,9 @@ "react-uid": "^2.2.0" }, "peerDependencies": { - "react": "^16.9.0", - "react-dom": "^16.9.0" + "immutability-helper": "3.0.1", + "react": "16.9.0", + "react-dom": "16.9.0" }, "devDependencies": { "@babel/cli": "^7.4.4", @@ -49,12 +49,13 @@ "eslint": "^6.1.0", "eslint-config-reforis": "^1.0.0", "file-loader": "^4.2.0", + "immutability-helper": "3.0.1", "jest": "^24.8.0", "jest-mock-axios": "^3.0.0", "moment": "^2.24.0", "moment-timezone": "^0.5.25", - "react": "^16.9.0", - "react-dom": "^16.9.0", + "react": "16.9.0", + "react-dom": "16.9.0", "react-styleguidist": "^9.1.16", "snapshot-diff": "^0.5.1", "style-loader": "^1.0.0", diff --git a/src/alertContext/AlertContext.js b/src/alertContext/AlertContext.js index c262dba..196969a 100644 --- a/src/alertContext/AlertContext.js +++ b/src/alertContext/AlertContext.js @@ -11,8 +11,6 @@ import PropTypes from "prop-types"; import { Alert, ALERT_TYPES } from "bootstrap/Alert"; import { Portal } from "utils/Portal"; -const AlertContext = React.createContext(); - AlertContextProvider.propTypes = { children: PropTypes.oneOfType([ PropTypes.arrayOf(PropTypes.node), @@ -21,6 +19,7 @@ AlertContextProvider.propTypes = { }; function AlertContextProvider({ children }) { + const { AlertContext } = window; const [alert, setAlert] = useState(null); const setAlertWrapper = useCallback((message, type = ALERT_TYPES.DANGER) => { @@ -46,7 +45,8 @@ function AlertContextProvider({ children }) { } function useAlert() { + const { AlertContext } = window; return useContext(AlertContext); } -export { AlertContext, AlertContextProvider, useAlert }; +export { AlertContextProvider, useAlert }; diff --git a/src/index.js b/src/index.js index 9a51089..be99b3b 100644 --- a/src/index.js +++ b/src/index.js @@ -75,4 +75,4 @@ export { } from "validations"; // Alert context -export { AlertContext, AlertContextProvider, useAlert } from "alertContext/AlertContext"; +export { AlertContextProvider, useAlert } from "alertContext/AlertContext"; diff --git a/src/testUtils/alertContextMock.js b/src/testUtils/alertContextMock.js index 22347e3..d93338b 100644 --- a/src/testUtils/alertContextMock.js +++ b/src/testUtils/alertContextMock.js @@ -7,11 +7,11 @@ import React from "react"; -import { AlertContext } from "../alertContext/AlertContext"; - const mockSetAlert = jest.fn(); const mockDismissAlert = jest.fn(); +window.AlertContext = React.createContext(); + function AlertContextMock({ children }) { return ( From 638821d025cacd649d08224aa7ff53b016c5cd2a Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Tue, 19 Nov 2019 13:34:01 +0100 Subject: [PATCH 6/8] Make checkBox help text clickabe. --- src/bootstrap/CheckBox.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/CheckBox.js b/src/bootstrap/CheckBox.js index f97a90f..5983da1 100644 --- a/src/bootstrap/CheckBox.js +++ b/src/bootstrap/CheckBox.js @@ -42,8 +42,10 @@ export function CheckBox({ {...props} /> - - {helpText && {helpText}} +
); From 1a82f8e2255037c6a07364e83f5dc593e65c3fd1 Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Tue, 19 Nov 2019 13:37:43 +0100 Subject: [PATCH 7/8] Update checkbox snapshots. --- .../__snapshots__/CheckBox.test.js.snap | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/bootstrap/__tests__/__snapshots__/CheckBox.test.js.snap b/src/bootstrap/__tests__/__snapshots__/CheckBox.test.js.snap index 31b0d57..ac39900 100644 --- a/src/bootstrap/__tests__/__snapshots__/CheckBox.test.js.snap +++ b/src/bootstrap/__tests__/__snapshots__/CheckBox.test.js.snap @@ -18,12 +18,12 @@ exports[` Render checkbox 1`] = ` for="1" > Test label + + Some help text + - - Some help text -
`; @@ -45,12 +45,12 @@ exports[` Render uncheked checkbox 1`] = ` for="1" > Test label + + Some help text + - - Some help text - `; From fd8acd1cebc7590e06ffc84322ddfcc569d1561c Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Tue, 19 Nov 2019 16:56:38 +0000 Subject: [PATCH 8/8] Catch error and sending state in API polling. --- src/api/hooks.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/hooks.js b/src/api/hooks.js index 47cc71d..0429d57 100644 --- a/src/api/hooks.js +++ b/src/api/hooks.js @@ -95,13 +95,13 @@ export { export function useAPIPolling(endpoint, delay = 1000, until) { // delay ms const [state, setState] = useState({ state: API_STATE.INIT }); - const [getState, get] = useAPIGet(endpoint); + const [getResponse, get] = useAPIGet(endpoint); useEffect(() => { - if (getState.state === API_STATE.SUCCESS) { - setState(getState); + if (getResponse.state !== API_STATE.INIT) { + setState(getResponse); } - }, [getState]); + }, [getResponse]); useEffect(() => { if (until) {