-
-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(console): refactor sso detail pages
refactor sso details pages
- Loading branch information
Showing
18 changed files
with
593 additions
and
469 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
85 changes: 0 additions & 85 deletions
85
packages/console/src/pages/EnterpriseSsoDetails/Connection/BasicInfo/index.tsx
This file was deleted.
Oops, something went wrong.
138 changes: 138 additions & 0 deletions
138
packages/console/src/pages/EnterpriseSsoDetails/Connection/OidcConnectorForm.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,138 @@ | ||
import { type RequestErrorBody } from '@logto/schemas'; | ||
import cleanDeep from 'clean-deep'; | ||
import { HTTPError } from 'ky'; | ||
import { useEffect, useMemo } from 'react'; | ||
import { FormProvider, useForm } from 'react-hook-form'; | ||
import { toast } from 'react-hot-toast'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import DetailsForm from '@/components/DetailsForm'; | ||
import FormCard from '@/components/FormCard'; | ||
import UnsavedChangesAlertModal from '@/components/UnsavedChangesAlertModal'; | ||
import useApi from '@/hooks/use-api'; | ||
|
||
import { invalidConfigErrorCode } from '../config'; | ||
import { | ||
type OidcSsoConnectorWithProviderConfig, | ||
type OidcConnectorConfig, | ||
oidcConnectorConfigGuard, | ||
oidcProviderConfigGuard, | ||
} from '../types/oidc'; | ||
|
||
import OidcMetadataForm from './OidcMetadataForm'; | ||
import OidcConnectorSpInfo from './ServiceProviderInfo/OidcConnectorSpInfo'; | ||
|
||
type Props = { | ||
isDeleted: boolean; | ||
data: OidcSsoConnectorWithProviderConfig; | ||
onUpdated: (data: OidcSsoConnectorWithProviderConfig) => void; | ||
}; | ||
|
||
function OidcConnectorForm({ isDeleted, data, onUpdated }: Props) { | ||
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); | ||
const api = useApi({ hideErrorToast: [invalidConfigErrorCode] }); | ||
|
||
const methods = useForm<OidcConnectorConfig>(); | ||
|
||
const { | ||
formState: { isSubmitting, isDirty }, | ||
handleSubmit, | ||
reset, | ||
setError, | ||
} = methods; | ||
|
||
const { config, providerConfig, providerName, id: connectorId } = data; | ||
|
||
// Guard the config data | ||
const oidcConnectorConfig = useMemo(() => { | ||
const result = oidcConnectorConfigGuard.safeParse(config); | ||
const { success } = result; | ||
const guardedConfig = success ? result.data : undefined; | ||
|
||
return guardedConfig; | ||
}, [config]); | ||
|
||
const oidcProviderConfig = useMemo(() => { | ||
const result = oidcProviderConfigGuard.safeParse(providerConfig); | ||
const { success } = result; | ||
const guardedConfig = success ? result.data : undefined; | ||
|
||
return guardedConfig; | ||
}, [providerConfig]); | ||
|
||
useEffect(() => { | ||
reset(oidcConnectorConfig); | ||
}, [oidcConnectorConfig, reset]); | ||
|
||
const onSubmit = handleSubmit(async (formData) => { | ||
if (isSubmitting) { | ||
return; | ||
} | ||
|
||
try { | ||
const result = await api | ||
.patch(`api/sso-connectors/${connectorId}`, { | ||
json: { | ||
config: cleanDeep(formData), | ||
}, | ||
}) | ||
.json<OidcSsoConnectorWithProviderConfig>(); | ||
|
||
toast.success(t('general.saved')); | ||
|
||
onUpdated(result); | ||
|
||
reset(result.config); | ||
} catch (error: unknown) { | ||
if (error instanceof HTTPError) { | ||
const errorBody = await error.response.clone().json<RequestErrorBody>(); | ||
|
||
// Manually handle the error to show the error message in the form. | ||
if (errorBody.code === invalidConfigErrorCode) { | ||
setError('issuer', { | ||
type: 'custom', | ||
message: errorBody.message, | ||
}); | ||
return; | ||
} | ||
} | ||
|
||
throw error; | ||
} | ||
}); | ||
|
||
return ( | ||
<FormProvider {...methods}> | ||
<DetailsForm | ||
isDirty={isDirty} | ||
isSubmitting={isSubmitting} | ||
onDiscard={reset} | ||
onSubmit={onSubmit} | ||
> | ||
<FormCard | ||
title="enterprise_sso_details.upload_idp_metadata_title_oidc" | ||
description="enterprise_sso_details.upload_idp_metadata_description_oidc" | ||
> | ||
{/* Can not infer the type by narrowing down the value of `providerName`, so we need to cast it. */} | ||
<OidcMetadataForm | ||
providerName={providerName} | ||
config={oidcConnectorConfig} | ||
providerConfig={oidcProviderConfig} | ||
/> | ||
</FormCard> | ||
<FormCard | ||
title="enterprise_sso_details.service_provider_property_title" | ||
description="enterprise_sso_details.service_provider_property_description" | ||
descriptionInterpolation={{ | ||
protocol: 'OIDC', | ||
}} | ||
> | ||
<OidcConnectorSpInfo ssoConnectorId={connectorId} /> | ||
</FormCard> | ||
</DetailsForm> | ||
<UnsavedChangesAlertModal hasUnsavedChanges={!isDeleted && isDirty} /> | ||
</FormProvider> | ||
); | ||
} | ||
|
||
export default OidcConnectorForm; |
5 changes: 2 additions & 3 deletions
5
.../src/pages/EnterpriseSsoDetails/Connection/OidcMetadataForm/ParsedConfigPreview/index.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
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
Oops, something went wrong.