|
| 1 | +import React, { FC } from "react"; |
| 2 | +import { Button, Input, Modal } from "@canonical/react-components"; |
| 3 | +import { useFormik } from "formik"; |
| 4 | +import * as Yup from "yup"; |
| 5 | + |
| 6 | +interface Props { |
| 7 | + onConfirm: (password: string) => void; |
| 8 | + onClose: () => void; |
| 9 | +} |
| 10 | + |
| 11 | +const PasswordModal: FC<Props> = ({ onConfirm, onClose }) => { |
| 12 | + const PasswordSchema = Yup.object().shape({ |
| 13 | + password: Yup.string(), |
| 14 | + passwordConfirm: Yup.string().oneOf( |
| 15 | + [Yup.ref("password"), null], |
| 16 | + "Passwords must match" |
| 17 | + ), |
| 18 | + }); |
| 19 | + |
| 20 | + const formik = useFormik({ |
| 21 | + initialValues: { |
| 22 | + password: "", |
| 23 | + passwordConfirm: "", |
| 24 | + }, |
| 25 | + validationSchema: PasswordSchema, |
| 26 | + onSubmit: (values) => { |
| 27 | + onConfirm(values.password); |
| 28 | + }, |
| 29 | + }); |
| 30 | + |
| 31 | + const handleSkip = () => { |
| 32 | + onConfirm(""); |
| 33 | + }; |
| 34 | + |
| 35 | + return ( |
| 36 | + <Modal |
| 37 | + close={onClose} |
| 38 | + title="Add a password" |
| 39 | + buttonRow={ |
| 40 | + <> |
| 41 | + <Button className="u-no-margin--bottom" onClick={handleSkip}> |
| 42 | + Skip |
| 43 | + </Button> |
| 44 | + <Button |
| 45 | + appearance="positive" |
| 46 | + className="u-no-margin--bottom" |
| 47 | + onClick={() => formik.submitForm()} |
| 48 | + disabled={ |
| 49 | + formik.values.password !== formik.values.passwordConfirm || |
| 50 | + formik.values.password.length === 0 |
| 51 | + } |
| 52 | + > |
| 53 | + Generate certificate |
| 54 | + </Button> |
| 55 | + </> |
| 56 | + } |
| 57 | + > |
| 58 | + <p>Protect your certificate by adding a password.</p> |
| 59 | + <Input |
| 60 | + id="password" |
| 61 | + type="password" |
| 62 | + label="Password" |
| 63 | + onBlur={formik.handleBlur} |
| 64 | + onChange={formik.handleChange} |
| 65 | + value={formik.values.password} |
| 66 | + error={formik.touched.password ? formik.errors.password : null} |
| 67 | + /> |
| 68 | + <Input |
| 69 | + id="passwordConfirm" |
| 70 | + type="password" |
| 71 | + label="Password confirmation" |
| 72 | + onBlur={formik.handleBlur} |
| 73 | + onChange={formik.handleChange} |
| 74 | + value={formik.values.passwordConfirm} |
| 75 | + error={ |
| 76 | + formik.touched.passwordConfirm ? formik.errors.passwordConfirm : null |
| 77 | + } |
| 78 | + /> |
| 79 | + </Modal> |
| 80 | + ); |
| 81 | +}; |
| 82 | + |
| 83 | +export default PasswordModal; |
0 commit comments