-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(console): implement new jwt customizer delete modal
implement new jwt customizer delete modal
- Loading branch information
Showing
3 changed files
with
87 additions
and
28 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
58 changes: 58 additions & 0 deletions
58
packages/console/src/pages/CustomizeJwt/DeleteConfirmModal.tsx
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { type LogtoJwtTokenKeyType } from '@logto/schemas'; | ||
import { useCallback, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useSWRConfig } from 'swr'; | ||
|
||
import ConfirmModal from '@/ds-components/ConfirmModal'; | ||
import useApi from '@/hooks/use-api'; | ||
import { getApiPath } from '@/pages/CustomizeJwt/utils/path'; | ||
|
||
type Props = { | ||
readonly isOpen: boolean; | ||
readonly tokenType?: LogtoJwtTokenKeyType; | ||
readonly onCancel: () => void; | ||
}; | ||
|
||
function DeleteConfirmModal({ isOpen, tokenType, onCancel }: Props) { | ||
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); | ||
const [loading, setLoading] = useState(false); | ||
const { mutate } = useSWRConfig(); | ||
|
||
const api = useApi(); | ||
const apiLink = tokenType && getApiPath(tokenType); | ||
|
||
const onDelete = useCallback(async () => { | ||
// If no token type is provided, dismiss the modal | ||
if (!apiLink) { | ||
onCancel(); | ||
return; | ||
} | ||
|
||
setLoading(true); | ||
|
||
try { | ||
// Delete the JWT customizer | ||
await api.delete(apiLink); | ||
// Mutate the SWR cache | ||
await mutate(getApiPath()); | ||
} finally { | ||
setLoading(false); | ||
onCancel(); | ||
} | ||
}, [api, apiLink, mutate, onCancel]); | ||
|
||
return ( | ||
<ConfirmModal | ||
title="jwt_claims.delete_modal_title" | ||
confirmButtonText="general.delete" | ||
isOpen={isOpen} | ||
isLoading={loading} | ||
onConfirm={onDelete} | ||
onCancel={onCancel} | ||
> | ||
{t('jwt_claims.delete_modal_content')} | ||
</ConfirmModal> | ||
); | ||
} | ||
|
||
export default DeleteConfirmModal; |
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