27cbbbf8 by Adam Heath

Add RefreshToken(astro+jsx)

1 parent 54053655
1 ---
2 import ReduxIsland from './ReduxIsland.astro'
3 import RefreshToken from './RefreshToken.jsx'
4 ---
5 <ReduxIsland>{props => <RefreshToken client:load {...props}/>}</ReduxIsland>
1 import React from 'react'
2
3 import { ReduxAstroProvider } from 'astro-redux/react'
4 import { useGetTokenQuery } from 'astro-redux/slices'
5
6 const RefreshToken = ReduxAstroProvider((props) => {
7 const [ retry, setRetry ] = React.useState(props.retry)
8 React.useEffect(() => {
9 if (retry !== props.retry) setRetry(props.retry)
10 }, [ props.retry ])
11
12 const getTokenQueryResult = useGetTokenQuery(undefined, { pollingInterval: retry })
13 const { data: { expires_at, access_token } = {} } = getTokenQueryResult
14 React.useEffect(() => {
15 if (!access_token) setRetry(0)
16 }, [ access_token ])
17
18 return null
19 })
20 RefreshToken.defaultProps = {
21 retry: 60000,
22 }
23
24 export default RefreshToken
25
...@@ -39,3 +39,5 @@ export const createAstroApiWrappers = (apiWrappers) => Object.entries(apiWrapper ...@@ -39,3 +39,5 @@ export const createAstroApiWrappers = (apiWrappers) => Object.entries(apiWrapper
39 }, {}) 39 }, {})
40 40
41 export { default as AstroReduxProvider } from './Provider.astro' 41 export { default as AstroReduxProvider } from './Provider.astro'
42 export { default as RefreshToken } from './RefreshToken.astro'
43 export { default as RefreshTokenReact } from './RefreshToken.jsx'
......