0
Fork 0
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:
wangsijie 2023-03-17 15:41:46 +08:00 committed by GitHub
parent 5caa95a6eb
commit 9c62ade9cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 22 deletions

View file

@ -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'),
}}

View file

@ -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}
/>