2022-12-04 17:36:30 +01:00
|
|
|
import { Button, Group, Modal, TextInput, Title } from '@mantine/core';
|
|
|
|
|
import { useForm } from '@mantine/form';
|
|
|
|
|
import { useTranslation } from 'next-i18next';
|
2022-12-30 16:17:22 +01:00
|
|
|
import { useCopyConfigMutation } from '../../../../tools/config/mutations/useCopyConfigMutation';
|
2022-12-04 17:36:30 +01:00
|
|
|
|
|
|
|
|
interface CreateConfigCopyModalProps {
|
|
|
|
|
opened: boolean;
|
|
|
|
|
closeModal: () => void;
|
|
|
|
|
initialConfigName: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const CreateConfigCopyModal = ({
|
|
|
|
|
opened,
|
|
|
|
|
closeModal,
|
|
|
|
|
initialConfigName,
|
|
|
|
|
}: CreateConfigCopyModalProps) => {
|
|
|
|
|
const { t } = useTranslation(['settings/general/config-changer']);
|
2022-12-30 16:17:22 +01:00
|
|
|
|
2022-12-04 17:36:30 +01:00
|
|
|
const form = useForm({
|
|
|
|
|
initialValues: {
|
|
|
|
|
configName: initialConfigName,
|
|
|
|
|
},
|
|
|
|
|
validate: {
|
|
|
|
|
configName: (v) => (!v ? t('modal.form.configName.validation.required') : null),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-30 16:17:22 +01:00
|
|
|
const { mutateAsync } = useCopyConfigMutation(form.values.configName);
|
|
|
|
|
|
2022-12-04 17:36:30 +01:00
|
|
|
const handleClose = () => {
|
|
|
|
|
form.setFieldValue('configName', initialConfigName);
|
|
|
|
|
closeModal();
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-30 16:17:22 +01:00
|
|
|
const handleSubmit = async (values: typeof form.values) => {
|
2022-12-04 17:36:30 +01:00
|
|
|
if (!form.isValid) return;
|
2022-12-30 16:17:22 +01:00
|
|
|
|
|
|
|
|
await mutateAsync();
|
2022-12-04 17:36:30 +01:00
|
|
|
closeModal();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Modal
|
|
|
|
|
radius="md"
|
|
|
|
|
opened={opened}
|
|
|
|
|
onClose={handleClose}
|
|
|
|
|
title={<Title order={4}>{t('modal.title')}</Title>}
|
|
|
|
|
>
|
|
|
|
|
<form onSubmit={form.onSubmit(handleSubmit)}>
|
|
|
|
|
<TextInput
|
|
|
|
|
label={t('modal.form.configName.label')}
|
|
|
|
|
placeholder={t('modal.form.configName.placeholder')}
|
|
|
|
|
{...form.getInputProps('configName')}
|
|
|
|
|
/>
|
|
|
|
|
<Group position="right" mt="md">
|
|
|
|
|
<Button type="submit">{t('modal.form.submitButton')}</Button>
|
|
|
|
|
</Group>
|
|
|
|
|
</form>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
};
|