diff --git a/package.json b/package.json index 486eaa2..4945dcb 100644 --- a/package.json +++ b/package.json @@ -225,7 +225,7 @@ "@babel/core": "^7.17.10", "@babel/eslint-parser": "^7.17.0", "@folio/eslint-config-stripes": "^7.0.0", - "@folio/stripes": "^9.1.0", + "@folio/stripes": "^9.2.0", "@folio/stripes-cli": "^3.0.0", "@formatjs/cli": "^6.1.3", "eslint": "^7.32.0", diff --git a/src/settings/RefreshTokenRotation.js b/src/settings/RefreshTokenRotation.js index bf7b91a..f40b6e1 100644 --- a/src/settings/RefreshTokenRotation.js +++ b/src/settings/RefreshTokenRotation.js @@ -4,34 +4,33 @@ import { useCallback, useEffect, useState } from 'react'; import { Field, Form } from 'react-final-form'; import { FormattedMessage } from 'react-intl'; -import { getTokenExpiry } from '@folio/stripes/core'; import { Button, LoadingPane, Pane, PaneHeader, TextField } from '@folio/stripes/components'; +import { getTokenExpiry, RTR_CONSTANTS } from '@folio/stripes/core'; /** * manipulate AT/RT expiration dates in storage in order to test RTR. - * @returns */ const RefreshTokenRotation = ({ stripes }) => { - // why WHY copy this string here instead of importing it from stripes-core? - // - // RTR_FORCE_REFRESH_EVENT will be present in stripes-core 10.2.0 (stripes - // 9.2.0). Importing it would force the stripes peer depedency to bump from - // ^9.1.0 to ^9.2.0.If we copy the string instead of importing it, we can - // remain compatible with 9.1.0. - // - // OK, compatibility is nice. But it's still gross, right? Yep, super gross. - // Aren't you nauseated? Yes, yes I am. πŸ€’πŸ§ΌπŸ› - const RTR_FORCE_REFRESH_EVENT = '@folio/stripes/core::RTRForceRefresh'; - const [isLoading, setIsLoading] = useState(true); - const [tokenExpiration, setTokenExpiration] = useState({}); + const [tokenExpiration, setTokenExpiration] = useState({ atExpires: -1, rtExpires: -1 }); useEffect(() => { - setIsLoading(true); - getTokenExpiry().then((te) => { - setTokenExpiration(te ?? { atExpires: -1, rtExpires: -1 }); - setIsLoading(false); - }); + const callback = () => { + setIsLoading(true); + getTokenExpiry().then((te) => { + setTokenExpiration({ + atExpires: te.atExpires ?? te.accessTokenExpiration ?? -1, + rtExpires: te.rtExpires ?? te.refreshTokenExpiration ?? -1, + }); + setIsLoading(false); + }); + }; + + callback(); + + window.addEventListener(RTR_CONSTANTS.RTR_SUCCESS_EVENT, callback); + + return () => window.removeEventListener(RTR_CONSTANTS.RTR_SUCCESS_EVENT, callback); }, []); /** @@ -39,7 +38,7 @@ const RefreshTokenRotation = ({ stripes }) => { * dispatch an event to force a token rotation */ const forceRefresh = useCallback( - () => window.dispatchEvent(new Event(RTR_FORCE_REFRESH_EVENT)), + () => window.dispatchEvent(new Event(RTR_CONSTANTS.RTR_FORCE_REFRESH_EVENT)), [], );