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';
|
2023-01-17 22:21:06 +01:00
|
|
|
import { useConfigStore } from '../../../../config/store';
|
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) => {
|
2023-01-17 22:21:06 +01:00
|
|
|
const { configs } = useConfigStore();
|
2022-12-04 17:36:30 +01:00
|
|
|
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: {
|
2023-01-17 22:21:06 +01:00
|
|
|
configName: (value) => {
|
|
|
|
|
if (!value) {
|
|
|
|
|
return t('modal.copy.form.configName.validation.required');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const configNames = configs.map((x) => x.value.configProperties.name);
|
|
|
|
|
if (configNames.includes(value)) {
|
|
|
|
|
return t('modal.copy.form.configName.validation.notUnique');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
},
|
2022-12-04 17:36:30 +01:00
|
|
|
},
|
2023-01-17 22:21:06 +01:00
|
|
|
validateInputOnChange: true,
|
|
|
|
|
validateInputOnBlur: true,
|
2022-12-04 17:36:30 +01:00
|
|
|
});
|
|
|
|
|
|
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}
|
2023-01-17 22:21:06 +01:00
|
|
|
title={<Title order={4}>{t('modal.copy.title')}</Title>}
|
2022-12-04 17:36:30 +01:00
|
|
|
>
|
|
|
|
|
<form onSubmit={form.onSubmit(handleSubmit)}>
|
|
|
|
|
<TextInput
|
2023-01-17 22:21:06 +01:00
|
|
|
label={t('modal.copy.form.configName.label')}
|
|
|
|
|
placeholder={t('modal.copy.form.configName.placeholder')}
|
2022-12-04 17:36:30 +01:00
|
|
|
{...form.getInputProps('configName')}
|
|
|
|
|
/>
|
|
|
|
|
<Group position="right" mt="md">
|
2023-01-17 22:21:06 +01:00
|
|
|
<Button type="submit" disabled={!form.isValid()}>
|
|
|
|
|
{t('modal.copy.form.submitButton')}
|
|
|
|
|
</Button>
|
2022-12-04 17:36:30 +01:00
|
|
|
</Group>
|
|
|
|
|
</form>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
};
|