diff --git a/package-lock.json b/package-lock.json index c8ff325..eaf38e3 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": { @@ -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 2e72014..61158a0 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": { @@ -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/api/hooks.js b/src/api/hooks.js index da236d0..0429d57 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 = 1000, until) { // delay ms + const [state, setState] = useState({ state: API_STATE.INIT }); + const [getResponse, get] = useAPIGet(endpoint); + + useEffect(() => { + if (getResponse.state !== API_STATE.INIT) { + setState(getResponse); + } + }, [getResponse]); + + useEffect(() => { + if (until) { + const interval = setInterval(get, delay); + return () => clearInterval(interval); + } + }, [until, delay, get]); + + return [state]; +} 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}} + ); 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 ( -