|
| 1 | +import { Formik } from "formik"; |
| 2 | +import React, { JSX } from "react"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import * as Yup from "yup"; |
| 5 | +import { getPageProps } from "../utils"; |
| 6 | +import { Dataset, DatasetsInput, TextInput } from "./utils"; |
| 7 | + |
| 8 | +type ProfileEditProps = { |
| 9 | + datasets: Dataset[]; |
| 10 | + is_commercial: boolean; |
| 11 | + csrf_token: string; |
| 12 | + initial_form_data: any; |
| 13 | + initial_errors: any; |
| 14 | +}; |
| 15 | + |
| 16 | +function ProfileEdit({ |
| 17 | + datasets, |
| 18 | + is_commercial, |
| 19 | + csrf_token, |
| 20 | + initial_form_data, |
| 21 | + initial_errors, |
| 22 | +}: ProfileEditProps): JSX.Element { |
| 23 | + return ( |
| 24 | + <> |
| 25 | + <h1 className="page-title">Your Profile</h1> |
| 26 | + <h2>Edit contact information</h2> |
| 27 | + |
| 28 | + <Formik |
| 29 | + initialValues={{ |
| 30 | + datasets: |
| 31 | + initial_form_data.datasets?.map((x: number) => x.toString()) ?? [], |
| 32 | + contact_name: initial_form_data.contact_name ?? "", |
| 33 | + contact_email: initial_form_data.contact_email ?? "", |
| 34 | + csrf_token, |
| 35 | + }} |
| 36 | + initialErrors={initial_errors} |
| 37 | + initialTouched={initial_errors} |
| 38 | + validationSchema={Yup.object({ |
| 39 | + contact_name: Yup.string().required("Contact name is required!"), |
| 40 | + contact_email: Yup.string() |
| 41 | + .email() |
| 42 | + .required("Email address is required!"), |
| 43 | + })} |
| 44 | + onSubmit={() => {}} |
| 45 | + > |
| 46 | + {({ errors }) => ( |
| 47 | + <form method="POST" className="form-horizontal"> |
| 48 | + <div className="form-group"> |
| 49 | + <div className="col-sm-offset-4 col-sm-5"> |
| 50 | + <input |
| 51 | + id="csrf_token" |
| 52 | + name="csrf_token" |
| 53 | + type="hidden" |
| 54 | + value={csrf_token} |
| 55 | + /> |
| 56 | + </div> |
| 57 | + {errors.csrf_token && ( |
| 58 | + <div className="alert alert-danger">{errors.csrf_token}</div> |
| 59 | + )} |
| 60 | + </div> |
| 61 | + |
| 62 | + <TextInput |
| 63 | + type="text" |
| 64 | + id="contact_name" |
| 65 | + name="contact_name" |
| 66 | + label="Name" |
| 67 | + required |
| 68 | + /> |
| 69 | + |
| 70 | + <TextInput |
| 71 | + type="email" |
| 72 | + id="contact_email" |
| 73 | + name="contact_email" |
| 74 | + label="Email" |
| 75 | + required |
| 76 | + /> |
| 77 | + <br /> |
| 78 | + |
| 79 | + {!is_commercial && <DatasetsInput datasets={datasets} />} |
| 80 | + |
| 81 | + <div className="form-group"> |
| 82 | + <div className="col-sm-offset-4 col-sm-10"> |
| 83 | + <button type="submit" className="btn btn-primary"> |
| 84 | + Update |
| 85 | + </button> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + </form> |
| 89 | + )} |
| 90 | + </Formik> |
| 91 | + </> |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +document.addEventListener("DOMContentLoaded", () => { |
| 96 | + const { domContainer, reactProps, globalProps } = getPageProps(); |
| 97 | + const { |
| 98 | + datasets, |
| 99 | + is_commercial, |
| 100 | + csrf_token, |
| 101 | + initial_form_data, |
| 102 | + initial_errors, |
| 103 | + } = reactProps; |
| 104 | + |
| 105 | + const renderRoot = createRoot(domContainer!); |
| 106 | + renderRoot.render( |
| 107 | + <ProfileEdit |
| 108 | + datasets={datasets} |
| 109 | + is_commercial={is_commercial} |
| 110 | + csrf_token={csrf_token} |
| 111 | + initial_form_data={initial_form_data} |
| 112 | + initial_errors={initial_errors} |
| 113 | + /> |
| 114 | + ); |
| 115 | +}); |
0 commit comments