mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
fix(console): reset form values when creating connector (#3461)
This commit is contained in:
parent
5caa95a6eb
commit
9c62ade9cc
2 changed files with 11 additions and 22 deletions
|
@ -1,6 +1,5 @@
|
|||
import type { ConnectorConfigFormItem } from '@logto/connector-kit';
|
||||
import { ConnectorType } from '@logto/connector-kit';
|
||||
import type { ConnectorFactoryResponse } from '@logto/schemas';
|
||||
import { useContext } from 'react';
|
||||
import { Controller, useFormContext } from 'react-hook-form';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
@ -16,20 +15,13 @@ import ConfigFormItems from '../ConfigForm';
|
|||
import * as styles from './ConfigForm.module.scss';
|
||||
|
||||
type Props = {
|
||||
configTemplate?: ConnectorFactoryResponse['configTemplate'];
|
||||
formItems?: ConnectorConfigFormItem[];
|
||||
className?: string;
|
||||
connectorId: string;
|
||||
connectorType?: ConnectorType;
|
||||
};
|
||||
|
||||
const ConfigForm = ({
|
||||
configTemplate,
|
||||
formItems,
|
||||
className,
|
||||
connectorId,
|
||||
connectorType,
|
||||
}: Props) => {
|
||||
const ConfigForm = ({ formItems, className, connectorId, connectorType }: Props) => {
|
||||
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
|
||||
const {
|
||||
control,
|
||||
|
@ -56,7 +48,6 @@ const ConfigForm = ({
|
|||
<Controller
|
||||
name="config"
|
||||
control={control}
|
||||
defaultValue={configTemplate}
|
||||
rules={{
|
||||
validate: (value) => jsonValidator(value) || t('errors.invalid_json_format'),
|
||||
}}
|
||||
|
|
|
@ -48,7 +48,7 @@ const Guide = ({ connector, onClose }: Props) => {
|
|||
const parseJsonConfig = useConfigParser();
|
||||
const [conflictConnectorName, setConflictConnectorName] = useState<Record<string, string>>();
|
||||
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
|
||||
const { type: connectorType, formItems, target, isStandard } = connector ?? {};
|
||||
const { type: connectorType, formItems, target, isStandard, configTemplate } = connector ?? {};
|
||||
|
||||
const { language } = i18next;
|
||||
|
||||
|
@ -56,30 +56,29 @@ const Guide = ({ connector, onClose }: Props) => {
|
|||
connectorType !== ConnectorType.Sms && connectorType !== ConnectorType.Email;
|
||||
const methods = useForm<ConnectorFormType>({
|
||||
reValidateMode: 'onBlur',
|
||||
defaultValues: {
|
||||
...(formItems ? initFormData(formItems) : {}),
|
||||
syncProfile: SyncProfileMode.OnlyAtRegister,
|
||||
},
|
||||
});
|
||||
const {
|
||||
formState: { isSubmitting },
|
||||
handleSubmit,
|
||||
watch,
|
||||
setValue,
|
||||
setError,
|
||||
reset,
|
||||
} = methods;
|
||||
|
||||
useEffect(() => {
|
||||
if (isSocialConnector && !isStandard && target) {
|
||||
setValue('target', target);
|
||||
}
|
||||
}, [isSocialConnector, target, isStandard, setValue]);
|
||||
reset({
|
||||
...(formItems ? initFormData(formItems) : {}),
|
||||
...(configTemplate ? { config: configTemplate } : {}),
|
||||
...(isSocialConnector && !isStandard && target ? { target } : {}),
|
||||
syncProfile: SyncProfileMode.OnlyAtRegister,
|
||||
});
|
||||
}, [formItems, reset, configTemplate, target, isSocialConnector, isStandard]);
|
||||
|
||||
if (!connector) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { id: connectorId, name, readme, configTemplate } = connector;
|
||||
const { id: connectorId, name, readme } = connector;
|
||||
const { title, content } = splitMarkdownByTitle(readme);
|
||||
const connectorName = conditional(isLanguageTag(language) && name[language]) ?? name.en;
|
||||
|
||||
|
@ -203,7 +202,6 @@ const Guide = ({ connector, onClose }: Props) => {
|
|||
</div>
|
||||
<ConfigForm
|
||||
connectorId={callbackConnectorId.current}
|
||||
configTemplate={configTemplate}
|
||||
connectorType={connectorType}
|
||||
formItems={formItems}
|
||||
/>
|
||||
|
|
Loading…
Reference in a new issue