Skip to content

Commit

Permalink
fix(console): reset form as soon as JWT customizer is created
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyYe committed Apr 2, 2024
1 parent 8be5f68 commit 7834552
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LogtoJwtTokenPath } from '@logto/schemas';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { useSWRConfig } from 'swr';

import DeletIcon from '@/assets/icons/delete.svg';
import EditIcon from '@/assets/icons/edit.svg';
Expand All @@ -11,6 +10,8 @@ import { useConfirmModal } from '@/hooks/use-confirm-modal';
import useTenantPathname from '@/hooks/use-tenant-pathname';
import { getApiPath, getPagePath } from '@/pages/CustomizeJwt/utils/path';

import useJwtCustomizer from '../use-jwt-customizer';

import * as styles from './index.module.scss';

type Props = {
Expand All @@ -23,7 +24,7 @@ function CustomizerItem({ tokenType }: Props) {
const editLink = getPagePath(tokenType, 'edit');
const { navigate } = useTenantPathname();
const { show } = useConfirmModal();
const { mutate } = useSWRConfig();
const { mutate } = useJwtCustomizer();

const api = useApi();

Expand All @@ -36,7 +37,7 @@ function CustomizerItem({ tokenType }: Props) {

if (confirm) {
await api.delete(apiLink);
await mutate(apiLink, undefined);
await mutate();
}
}, [api, apiLink, mutate, show, t]);

Expand Down
53 changes: 18 additions & 35 deletions packages/console/src/pages/CustomizeJwt/use-jwt-customizer.ts
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;
9 changes: 7 additions & 2 deletions packages/console/src/pages/CustomizeJwt/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import { type LogtoJwtTokenPath } from '@logto/schemas';

import { type Action } from './type';

export const getApiPath = (tokenType: LogtoJwtTokenPath) =>
`api/configs/jwt-customizer/${tokenType}`;
export const getApiPath = (tokenType?: LogtoJwtTokenPath) => {
if (!tokenType) {
return 'api/configs/jwt-customizer';
}

return `api/configs/jwt-customizer/${tokenType}`;
};

export const getPagePath = (tokenType?: LogtoJwtTokenPath, action?: Action) => {
if (!tokenType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import UnsavedChangesAlertModal from '@/components/UnsavedChangesAlertModal';
import useApi from '@/hooks/use-api';
import { trySubmitSafe } from '@/utils/form';

import useJwtCustomizer from '../../CustomizeJwt/use-jwt-customizer';
import { type Action, type JwtCustomizer, type JwtCustomizerForm } from '../type';
import { formatFormDataToRequestData, formatResponseDataToFormData } from '../utils/format';
import { getApiPath } from '../utils/path';
Expand All @@ -34,6 +35,7 @@ function MainContent<T extends LogtoJwtTokenPath>({
}: Props<T>) {
const api = useApi();
const navigate = useNavigate();
const { mutate: mutateJwtCustomizers } = useJwtCustomizer();

const methods = useForm<JwtCustomizerForm>({
defaultValues: formatResponseDataToFormData(token, data),
Expand Down Expand Up @@ -66,6 +68,8 @@ function MainContent<T extends LogtoJwtTokenPath>({
* is not expected.
*/
if (action === 'create') {
// Refresh the JWT customizers list to reflect the latest changes.
await mutateJwtCustomizers();
navigate(-1);
}
})
Expand Down

0 comments on commit 7834552

Please sign in to comment.