|
| 1 | +import { Field, Form, Formik } from "formik"; |
| 2 | +import { generate } from "generate-password-browser"; |
| 3 | +import { useState } from "react"; |
| 4 | +import { Alert } from "react-bootstrap"; |
| 5 | +import Modal from "react-bootstrap/Modal"; |
| 6 | +import { updateAuth } from "src/api/backend"; |
| 7 | +import { Button } from "src/components"; |
| 8 | +import { intl } from "src/locale"; |
| 9 | +import { validateString } from "src/modules/Validations"; |
| 10 | + |
| 11 | +interface Props { |
| 12 | + userId: number; |
| 13 | + onClose: () => void; |
| 14 | +} |
| 15 | +export function SetPasswordModal({ userId, onClose }: Props) { |
| 16 | + const [error, setError] = useState<string | null>(null); |
| 17 | + const [showPassword, setShowPassword] = useState(false); |
| 18 | + |
| 19 | + const onSubmit = async (values: any, { setSubmitting }: any) => { |
| 20 | + setError(null); |
| 21 | + try { |
| 22 | + await updateAuth(userId, values.new); |
| 23 | + onClose(); |
| 24 | + } catch (err: any) { |
| 25 | + setError(intl.formatMessage({ id: err.message })); |
| 26 | + } |
| 27 | + setSubmitting(false); |
| 28 | + }; |
| 29 | + |
| 30 | + return ( |
| 31 | + <Modal show onHide={onClose} animation={false}> |
| 32 | + <Formik |
| 33 | + initialValues={ |
| 34 | + { |
| 35 | + new: "", |
| 36 | + } as any |
| 37 | + } |
| 38 | + onSubmit={onSubmit} |
| 39 | + > |
| 40 | + {({ isSubmitting }) => ( |
| 41 | + <Form> |
| 42 | + <Modal.Header closeButton> |
| 43 | + <Modal.Title>{intl.formatMessage({ id: "user.set-password" })}</Modal.Title> |
| 44 | + </Modal.Header> |
| 45 | + <Modal.Body> |
| 46 | + <Alert variant="danger" show={!!error} onClose={() => setError(null)} dismissible> |
| 47 | + {error} |
| 48 | + </Alert> |
| 49 | + <div className="mb-3"> |
| 50 | + <Field name="new" validate={validateString(8, 100)}> |
| 51 | + {({ field, form }: any) => ( |
| 52 | + <> |
| 53 | + <p className="text-end"> |
| 54 | + <small> |
| 55 | + <a |
| 56 | + href="#" |
| 57 | + onClick={(e) => { |
| 58 | + e.preventDefault(); |
| 59 | + form.setFieldValue( |
| 60 | + field.name, |
| 61 | + generate({ |
| 62 | + length: 12, |
| 63 | + numbers: true, |
| 64 | + }), |
| 65 | + ); |
| 66 | + setShowPassword(true); |
| 67 | + }} |
| 68 | + > |
| 69 | + {intl.formatMessage({ |
| 70 | + id: "password.generate", |
| 71 | + })} |
| 72 | + </a>{" "} |
| 73 | + —{" "} |
| 74 | + <a |
| 75 | + href="#" |
| 76 | + className="text-xs" |
| 77 | + onClick={(e) => { |
| 78 | + e.preventDefault(); |
| 79 | + setShowPassword(!showPassword); |
| 80 | + }} |
| 81 | + > |
| 82 | + {intl.formatMessage({ |
| 83 | + id: showPassword ? "password.hide" : "password.show", |
| 84 | + })} |
| 85 | + </a> |
| 86 | + </small> |
| 87 | + </p> |
| 88 | + <div className="form-floating mb-3"> |
| 89 | + <input |
| 90 | + id="new" |
| 91 | + type={showPassword ? "text" : "password"} |
| 92 | + required |
| 93 | + className={`form-control ${form.errors.new && form.touched.new ? "is-invalid" : ""}`} |
| 94 | + placeholder={intl.formatMessage({ id: "user.new-password" })} |
| 95 | + {...field} |
| 96 | + /> |
| 97 | + <label htmlFor="new"> |
| 98 | + {intl.formatMessage({ id: "user.new-password" })} |
| 99 | + </label> |
| 100 | + |
| 101 | + {form.errors.new ? ( |
| 102 | + <div className="invalid-feedback"> |
| 103 | + {form.errors.new && form.touched.new ? form.errors.new : null} |
| 104 | + </div> |
| 105 | + ) : null} |
| 106 | + </div> |
| 107 | + </> |
| 108 | + )} |
| 109 | + </Field> |
| 110 | + </div> |
| 111 | + </Modal.Body> |
| 112 | + <Modal.Footer> |
| 113 | + <Button data-bs-dismiss="modal" onClick={onClose} disabled={isSubmitting}> |
| 114 | + {intl.formatMessage({ id: "cancel" })} |
| 115 | + </Button> |
| 116 | + <Button |
| 117 | + type="submit" |
| 118 | + actionType="primary" |
| 119 | + className="ms-auto" |
| 120 | + data-bs-dismiss="modal" |
| 121 | + isLoading={isSubmitting} |
| 122 | + disabled={isSubmitting} |
| 123 | + > |
| 124 | + {intl.formatMessage({ id: "save" })} |
| 125 | + </Button> |
| 126 | + </Modal.Footer> |
| 127 | + </Form> |
| 128 | + )} |
| 129 | + </Formik> |
| 130 | + </Modal> |
| 131 | + ); |
| 132 | +} |
0 commit comments