-
-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(console): reset form as soon as JWT customizer is created
- Loading branch information
Showing
4 changed files
with
33 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 18 additions & 35 deletions
53
packages/console/src/pages/CustomizeJwt/use-jwt-customizer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,40 @@ | ||
import { | ||
LogtoJwtTokenPath, | ||
type AccessTokenJwtCustomizer, | ||
type ClientCredentialsJwtCustomizer, | ||
} from '@logto/schemas'; | ||
import { LogtoJwtTokenKey, type JwtCustomizerConfigs } from '@logto/schemas'; | ||
import { type ResponseError } from '@withtyped/client'; | ||
import { useMemo } from 'react'; | ||
import useSWR from 'swr'; | ||
|
||
import useApi from '@/hooks/use-api'; | ||
import useSwrFetcher from '@/hooks/use-swr-fetcher'; | ||
import { shouldRetryOnError } from '@/utils/request'; | ||
|
||
import { getApiPath } from './utils/path'; | ||
|
||
function useJwtCustomizer() { | ||
const fetchApi = useApi({ hideErrorToast: true }); | ||
const accessTokenFetcher = useSwrFetcher<AccessTokenJwtCustomizer>(fetchApi); | ||
const clientCredentialsFetcher = useSwrFetcher<ClientCredentialsJwtCustomizer>(fetchApi); | ||
|
||
const fetcher = useSwrFetcher<JwtCustomizerConfigs[]>(fetchApi); | ||
const { | ||
data: accessTokenJwtCustomizer, | ||
isLoading: isAccessTokenJwtDataLoading, | ||
error: accessTokenError, | ||
} = useSWR<AccessTokenJwtCustomizer, ResponseError>(getApiPath(LogtoJwtTokenPath.AccessToken), { | ||
fetcher: accessTokenFetcher, | ||
shouldRetryOnError: shouldRetryOnError({ ignore: [404] }), | ||
data, | ||
isLoading: isDataLoading, | ||
error, | ||
mutate, | ||
} = useSWR<JwtCustomizerConfigs[], ResponseError>(getApiPath(), { | ||
fetcher, | ||
}); | ||
const isLoading = isDataLoading && !error; | ||
|
||
const { | ||
data: clientCredentialsJwtCustomizer, | ||
isLoading: isClientCredentialsJwtDataLoading, | ||
error: clientCredentialsError, | ||
} = useSWR<ClientCredentialsJwtCustomizer, ResponseError>( | ||
getApiPath(LogtoJwtTokenPath.ClientCredentials), | ||
{ | ||
fetcher: clientCredentialsFetcher, | ||
shouldRetryOnError: shouldRetryOnError({ ignore: [404] }), | ||
} | ||
); | ||
|
||
// Show global loading status only if any of the fetchers are loading and no errors are present | ||
const isLoading = | ||
(isAccessTokenJwtDataLoading && !accessTokenError) || | ||
(isClientCredentialsJwtDataLoading && !clientCredentialsError); | ||
return useMemo(() => { | ||
const { value: accessTokenJwtCustomizer } = | ||
data?.find(({ key }) => key === LogtoJwtTokenKey.AccessToken) ?? {}; | ||
const { value: clientCredentialsJwtCustomizer } = | ||
data?.find(({ key }) => key === LogtoJwtTokenKey.ClientCredentials) ?? {}; | ||
|
||
return useMemo( | ||
() => ({ | ||
return { | ||
accessTokenJwtCustomizer, | ||
clientCredentialsJwtCustomizer, | ||
isLoading, | ||
}), | ||
[accessTokenJwtCustomizer, clientCredentialsJwtCustomizer, isLoading] | ||
); | ||
mutate, | ||
}; | ||
}, [data, isLoading, mutate]); | ||
} | ||
|
||
export default useJwtCustomizer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters