2020-08-08 02:19:42 +02:00
|
|
|
import React, { useState } from 'react';
|
2020-10-01 07:34:16 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2020-08-08 02:19:42 +02:00
|
|
|
import { Formik, Form, Field } from 'formik';
|
2020-08-08 05:36:57 +02:00
|
|
|
import Button from 'components/common/Button';
|
2020-08-08 02:19:42 +02:00
|
|
|
import FormLayout, {
|
|
|
|
FormButtons,
|
|
|
|
FormError,
|
|
|
|
FormMessage,
|
|
|
|
FormRow,
|
|
|
|
} from 'components/layout/FormLayout';
|
2020-11-10 06:01:53 +01:00
|
|
|
import useDelete from 'hooks/useDelete';
|
2020-08-08 02:19:42 +02:00
|
|
|
|
2020-09-13 10:26:54 +02:00
|
|
|
const CONFIRMATION_WORD = 'DELETE';
|
|
|
|
|
2020-08-08 02:19:42 +02:00
|
|
|
const validate = ({ confirmation }) => {
|
|
|
|
const errors = {};
|
|
|
|
|
2020-09-13 10:26:54 +02:00
|
|
|
if (confirmation !== CONFIRMATION_WORD) {
|
2020-09-06 02:27:01 +02:00
|
|
|
errors.confirmation = !confirmation ? (
|
|
|
|
<FormattedMessage id="label.required" defaultMessage="Required" />
|
|
|
|
) : (
|
|
|
|
<FormattedMessage id="label.invalid" defaultMessage="Invalid" />
|
|
|
|
);
|
2020-08-08 02:19:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
};
|
|
|
|
|
2020-08-09 11:03:37 +02:00
|
|
|
export default function DeleteForm({ values, onSave, onClose }) {
|
2020-11-10 06:01:53 +01:00
|
|
|
const del = useDelete();
|
2020-08-08 02:19:42 +02:00
|
|
|
const [message, setMessage] = useState();
|
|
|
|
|
2020-08-09 11:03:37 +02:00
|
|
|
const handleSubmit = async ({ type, id }) => {
|
2020-11-10 06:01:53 +01:00
|
|
|
const { ok, data } = await del(`/api/${type}/${id}`);
|
2020-08-08 02:19:42 +02:00
|
|
|
|
2020-10-01 07:34:16 +02:00
|
|
|
if (ok) {
|
2020-08-08 02:19:42 +02:00
|
|
|
onSave();
|
|
|
|
} else {
|
2020-10-01 07:34:16 +02:00
|
|
|
setMessage(
|
|
|
|
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
|
|
|
|
);
|
2020-08-08 02:19:42 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormLayout>
|
|
|
|
<Formik
|
2020-08-08 05:36:57 +02:00
|
|
|
initialValues={{ confirmation: '', ...values }}
|
2020-08-08 02:19:42 +02:00
|
|
|
validate={validate}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
>
|
2020-09-13 10:26:54 +02:00
|
|
|
{props => (
|
2020-08-08 02:19:42 +02:00
|
|
|
<Form>
|
|
|
|
<div>
|
2020-09-06 02:27:01 +02:00
|
|
|
<FormattedMessage
|
|
|
|
id="message.confirm-delete"
|
|
|
|
defaultMessage="Are your sure you want to delete {target}?"
|
|
|
|
values={{ target: <b>{values.name}</b> }}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<FormattedMessage
|
|
|
|
id="message.delete-warning"
|
|
|
|
defaultMessage="All associated data will be deleted as well."
|
|
|
|
/>
|
2020-08-08 02:19:42 +02:00
|
|
|
</div>
|
|
|
|
<p>
|
2020-09-06 02:27:01 +02:00
|
|
|
<FormattedMessage
|
|
|
|
id="message.type-delete"
|
|
|
|
defaultMessage="Type {delete} in the box below to confirm."
|
2020-09-13 10:26:54 +02:00
|
|
|
values={{ delete: <b>{CONFIRMATION_WORD}</b> }}
|
2020-09-06 02:27:01 +02:00
|
|
|
/>
|
2020-08-08 02:19:42 +02:00
|
|
|
</p>
|
|
|
|
<FormRow>
|
2020-10-22 20:00:58 +02:00
|
|
|
<div>
|
|
|
|
<Field name="confirmation" type="text" />
|
|
|
|
<FormError name="confirmation" />
|
|
|
|
</div>
|
2020-08-08 02:19:42 +02:00
|
|
|
</FormRow>
|
|
|
|
<FormButtons>
|
2020-09-13 10:26:54 +02:00
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
variant="danger"
|
|
|
|
disabled={props.values.confirmation !== CONFIRMATION_WORD}
|
|
|
|
>
|
2020-10-13 07:53:59 +02:00
|
|
|
<FormattedMessage id="label.delete" defaultMessage="Delete" />
|
2020-09-06 02:27:01 +02:00
|
|
|
</Button>
|
|
|
|
<Button onClick={onClose}>
|
2020-10-13 07:53:59 +02:00
|
|
|
<FormattedMessage id="label.cancel" defaultMessage="Cancel" />
|
2020-08-08 02:19:42 +02:00
|
|
|
</Button>
|
|
|
|
</FormButtons>
|
|
|
|
<FormMessage>{message}</FormMessage>
|
|
|
|
</Form>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
</FormLayout>
|
|
|
|
);
|
|
|
|
}
|