|
| 1 | +import { DialogProps } from "components/Dialogs/Dialog" |
| 2 | +import { FC } from "react" |
| 3 | +import { getFormHelpers, nameValidator } from "util/formUtils" |
| 4 | +import { FormFields } from "components/Form/Form" |
| 5 | +import { useFormik } from "formik" |
| 6 | +import * as Yup from "yup" |
| 7 | +import { PublishVersionData } from "pages/TemplateVersionPage/TemplateVersionEditorPage/types" |
| 8 | +import TextField from "@material-ui/core/TextField" |
| 9 | +import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog" |
| 10 | +import Checkbox from "@material-ui/core/Checkbox" |
| 11 | +import FormControlLabel from "@material-ui/core/FormControlLabel" |
| 12 | +import { Stack } from "components/Stack/Stack" |
| 13 | + |
| 14 | +export type PublishTemplateVersionDialogProps = DialogProps & { |
| 15 | + isPublishing: boolean |
| 16 | + onClose: () => void |
| 17 | + onConfirm: (data: PublishVersionData) => void |
| 18 | +} |
| 19 | + |
| 20 | +export const PublishTemplateVersionDialog: FC< |
| 21 | + PublishTemplateVersionDialogProps |
| 22 | +> = ({ onConfirm, isPublishing, onClose, ...dialogProps }) => { |
| 23 | + const form = useFormik({ |
| 24 | + initialValues: { |
| 25 | + name: "", |
| 26 | + isActiveVersion: false, |
| 27 | + }, |
| 28 | + validationSchema: Yup.object({ |
| 29 | + name: nameValidator("name"), |
| 30 | + isActiveVersion: Yup.boolean(), |
| 31 | + }), |
| 32 | + onSubmit: onConfirm, |
| 33 | + }) |
| 34 | + const getFieldHelpers = getFormHelpers(form) |
| 35 | + const handleClose = () => { |
| 36 | + form.resetForm() |
| 37 | + onClose() |
| 38 | + } |
| 39 | + |
| 40 | + return ( |
| 41 | + <ConfirmDialog |
| 42 | + {...dialogProps} |
| 43 | + confirmLoading={isPublishing} |
| 44 | + onClose={handleClose} |
| 45 | + onConfirm={async () => { |
| 46 | + await form.submitForm() |
| 47 | + }} |
| 48 | + hideCancel={false} |
| 49 | + type="success" |
| 50 | + cancelText="Cancel" |
| 51 | + confirmText="Publish" |
| 52 | + title="Publish new version" |
| 53 | + description={ |
| 54 | + <Stack> |
| 55 | + <p>You are about to publish a new version of this template.</p> |
| 56 | + <FormFields> |
| 57 | + <TextField |
| 58 | + {...getFieldHelpers("name")} |
| 59 | + label="Version name" |
| 60 | + autoFocus |
| 61 | + disabled={isPublishing} |
| 62 | + InputLabelProps={{ |
| 63 | + shrink: true, |
| 64 | + }} |
| 65 | + /> |
| 66 | + |
| 67 | + <FormControlLabel |
| 68 | + label="Promote to default version" |
| 69 | + control={ |
| 70 | + <Checkbox |
| 71 | + size="small" |
| 72 | + checked={form.values.isActiveVersion} |
| 73 | + onChange={async (e) => { |
| 74 | + await form.setFieldValue( |
| 75 | + "isActiveVersion", |
| 76 | + e.target.checked, |
| 77 | + ) |
| 78 | + }} |
| 79 | + name="isActiveVersion" |
| 80 | + color="primary" |
| 81 | + /> |
| 82 | + } |
| 83 | + /> |
| 84 | + </FormFields> |
| 85 | + </Stack> |
| 86 | + } |
| 87 | + /> |
| 88 | + ) |
| 89 | +} |
0 commit comments