diff --git a/packages/console/src/pages/ConnectorDetails/components/ConnectorContent.tsx b/packages/console/src/pages/ConnectorDetails/components/ConnectorContent.tsx index 6f4214bcc..4ed55965c 100644 --- a/packages/console/src/pages/ConnectorDetails/components/ConnectorContent.tsx +++ b/packages/console/src/pages/ConnectorDetails/components/ConnectorContent.tsx @@ -1,17 +1,16 @@ -import type { Connector, ConnectorResponse, ConnectorMetadata } from '@logto/schemas'; +import type { ConnectorResponse } from '@logto/schemas'; import { ConnectorType } from '@logto/schemas'; -import { useEffect, useMemo } from 'react'; -import { Controller, useForm } from 'react-hook-form'; +import { useEffect } from 'react'; +import { FormProvider, useForm } from 'react-hook-form'; import { toast } from 'react-hot-toast'; import { useTranslation } from 'react-i18next'; -import { useParams } from 'react-router-dom'; -import CodeEditor from '@/components/CodeEditor'; import DetailsForm from '@/components/DetailsForm'; import FormCard from '@/components/FormCard'; -import FormField from '@/components/FormField'; import UnsavedChangesAlertModal from '@/components/UnsavedChangesAlertModal'; import useApi from '@/hooks/use-api'; +import ConnectorForm from '@/pages/Connectors/components/ConnectorForm'; +import type { ConnectorFormType } from '@/pages/Connectors/types'; import { safeParseJson } from '@/utilities/json'; import * as styles from '../index.module.scss'; @@ -25,34 +24,35 @@ type Props = { const ConnectorContent = ({ isDeleted, connectorData, onConnectorUpdated }: Props) => { const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); - const { connectorId } = useParams(); const api = useApi(); + const methods = useForm({ reValidateMode: 'onBlur' }); const { - control, formState: { isSubmitting, isDirty }, handleSubmit, watch, reset, - } = useForm<{ configJson: string }>({ reValidateMode: 'onBlur' }); - - const defaultConfig = useMemo(() => { - const hasData = Object.keys(connectorData.config).length > 0; - - return hasData ? JSON.stringify(connectorData.config, null, 2) : connectorData.configTemplate; - }, [connectorData]); + } = methods; useEffect(() => { - reset(); - }, [connectorId, reset]); + const { name, logo, logoDark, target } = connectorData.metadata; + const { config } = connectorData; + reset({ + target, + logo, + logoDark: logoDark ?? '', + name: name?.en, + config: JSON.stringify(config, null, 2), + }); + }, [connectorData, reset]); - const onSubmit = handleSubmit(async ({ configJson }) => { - if (!configJson) { + const onSubmit = handleSubmit(async ({ config, ...metadata }) => { + if (!config) { toast.error(t('connector_details.save_error_empty_config')); return; } - const result = safeParseJson(configJson); + const result = safeParseJson(config); if (!result.success) { toast.error(result.error); @@ -60,17 +60,22 @@ const ConnectorContent = ({ isDeleted, connectorData, onConnectorUpdated }: Prop return; } - const { metadata, ...rest } = await api - .patch(`/api/connectors/${connectorData.id}`, { json: { config: result.data } }) - .json(); + const body = connectorData.isStandard + ? { config: result.data, metadata: { ...metadata, name: { en: metadata.name } } } + : { config: result.data }; - onConnectorUpdated({ ...rest, ...metadata }); - reset({ configJson: JSON.stringify(result.data, null, 2) }); + const updatedConnector = await api + .patch(`/api/connectors/${connectorData.id}`, { + json: body, + }) + .json(); + + onConnectorUpdated(updatedConnector); toast.success(t('general.saved')); }); return ( - <> + - ( - - - - )} - /> + {connectorData.type !== ConnectorType.Social && ( )} - + ); }; diff --git a/packages/console/src/pages/Connectors/components/ConnectorForm/index.module.scss b/packages/console/src/pages/Connectors/components/ConnectorForm/index.module.scss new file mode 100644 index 000000000..c01cdf5bd --- /dev/null +++ b/packages/console/src/pages/Connectors/components/ConnectorForm/index.module.scss @@ -0,0 +1,7 @@ +@use '@/scss/underscore' as _; + +.tip { + color: var(--color-text-secondary); + font: var(--font-body-medium); + margin-top: _.unit(0.5); +} diff --git a/packages/console/src/pages/Connectors/components/Guide/Form.tsx b/packages/console/src/pages/Connectors/components/ConnectorForm/index.tsx similarity index 79% rename from packages/console/src/pages/Connectors/components/Guide/Form.tsx rename to packages/console/src/pages/Connectors/components/ConnectorForm/index.tsx index a8d3a7fee..a214f7aa9 100644 --- a/packages/console/src/pages/Connectors/components/Guide/Form.tsx +++ b/packages/console/src/pages/Connectors/components/ConnectorForm/index.tsx @@ -8,17 +8,17 @@ import CodeEditor from '@/components/CodeEditor'; import FormField from '@/components/FormField'; import TextInput from '@/components/TextInput'; +import type { ConnectorFormType } from '../../types'; import * as styles from './index.module.scss'; -import type { CreateConnectorForm } from './types'; type Props = { connector: ConnectorFactoryResponse; }; -const Form = ({ connector }: Props) => { +const ConnectorForm = ({ connector }: Props) => { const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); const { configTemplate, isStandard } = connector; - const { control, register } = useFormContext(); + const { control, register } = useFormContext(); const [darkVisible, setDarkVisible] = useState(false); const toggleDarkVisible = () => { @@ -43,14 +43,6 @@ const Form = ({ connector }: Props) => { />
{t('connectors.guide.logo_tip')}
- {!darkVisible && ( -