|
| 1 | +import { useEffect, useMemo, useState } from "react" |
| 2 | +import clsx from "clsx" |
| 3 | +import type { ModalProps } from "react-bootstrap" |
| 4 | +import { useForm } from "react-hook-form" |
| 5 | +import { |
| 6 | + Alert, |
| 7 | + Button, |
| 8 | + Col, |
| 9 | + Form, |
| 10 | + Modal, |
| 11 | + Row, |
| 12 | + Stack |
| 13 | +} from "../bootstrap" |
| 14 | +import { LoadingButton } from "../buttons" |
| 15 | +import Input from "../forms/Input" |
| 16 | +import PasswordInput from "../forms/PasswordInput" |
| 17 | +import { |
| 18 | + CreateLegislatorWithEmailAndPasswordData, |
| 19 | + useCreateLegislatorWithEmailAndPassword |
| 20 | +} from "./hooks" |
| 21 | +import TermsOfServiceModal from "./TermsOfServiceModal" |
| 22 | +import { useTranslation } from "next-i18next" |
| 23 | +import { Search } from "../legislatorSearch" |
| 24 | +import { useClaimedMemberCodes, useMemberSearch } from "../db/members" |
| 25 | +import { ProfileMember } from "../db" |
| 26 | + |
| 27 | +export default function LegislatorSignUpModal({ |
| 28 | + show, |
| 29 | + onHide, |
| 30 | + onSuccessfulSubmit |
| 31 | +}: Pick<ModalProps, "show" | "onHide"> & { |
| 32 | + onSuccessfulSubmit: () => void |
| 33 | + onHide: () => void |
| 34 | +}) { |
| 35 | + const { |
| 36 | + register, |
| 37 | + handleSubmit, |
| 38 | + reset, |
| 39 | + getValues, |
| 40 | + trigger, |
| 41 | + formState: { errors } |
| 42 | + } = useForm<CreateLegislatorWithEmailAndPasswordData>() |
| 43 | + |
| 44 | + const [tosStep, setTosStep] = useState<"not-agreed" | "reading" | "agreed">( |
| 45 | + "not-agreed" |
| 46 | + ) |
| 47 | + const [selectedMember, setSelectedMember] = useState<ProfileMember | null>( |
| 48 | + null |
| 49 | + ) |
| 50 | + const [memberError, setMemberError] = useState<string | undefined>() |
| 51 | + |
| 52 | + const showTos = tosStep === "reading" |
| 53 | + |
| 54 | + const createLegislatorWithEmailAndPassword = |
| 55 | + useCreateLegislatorWithEmailAndPassword() |
| 56 | + |
| 57 | + const { index } = useMemberSearch() |
| 58 | + const { claimedCodes } = useClaimedMemberCodes() |
| 59 | + |
| 60 | + const memberIndex = useMemo(() => { |
| 61 | + const all = [ |
| 62 | + ...(index?.representatives ?? []), |
| 63 | + ...(index?.senators ?? []) |
| 64 | + ] |
| 65 | + if (!claimedCodes) return all |
| 66 | + return all.filter(m => !claimedCodes.has(m.MemberCode)) |
| 67 | + }, [index, claimedCodes]) |
| 68 | + |
| 69 | + const { t } = useTranslation("auth") |
| 70 | + |
| 71 | + useEffect(() => { |
| 72 | + if (!show) { |
| 73 | + reset() |
| 74 | + setTosStep("not-agreed") |
| 75 | + setSelectedMember(null) |
| 76 | + setMemberError(undefined) |
| 77 | + createLegislatorWithEmailAndPassword.reset() |
| 78 | + } |
| 79 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 80 | + }, [show, reset]) |
| 81 | + |
| 82 | + const onSubmit = handleSubmit(newUser => { |
| 83 | + if (!selectedMember) { |
| 84 | + setMemberError( |
| 85 | + t("legislatorRequired") ?? "Please select your legislator profile." |
| 86 | + ) |
| 87 | + return |
| 88 | + } |
| 89 | + setMemberError(undefined) |
| 90 | + const promise = createLegislatorWithEmailAndPassword.execute({ |
| 91 | + ...newUser, |
| 92 | + memberCode: selectedMember.id |
| 93 | + }) |
| 94 | + promise.then(onSuccessfulSubmit).catch(() => {}) |
| 95 | + }) |
| 96 | + |
| 97 | + async function handleContinueClick() { |
| 98 | + if (!selectedMember) { |
| 99 | + setMemberError( |
| 100 | + t("legislatorRequired") ?? "Please select your legislator profile." |
| 101 | + ) |
| 102 | + return |
| 103 | + } |
| 104 | + setMemberError(undefined) |
| 105 | + const isValid = await trigger() |
| 106 | + if (isValid) { |
| 107 | + setTosStep("reading") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + useEffect(() => { |
| 112 | + if (tosStep === "agreed") { |
| 113 | + const loadingbtn = document.getElementById("legislator-loading-button") |
| 114 | + loadingbtn?.click() |
| 115 | + } |
| 116 | + }, [tosStep]) |
| 117 | + |
| 118 | + return ( |
| 119 | + <> |
| 120 | + <Modal |
| 121 | + show={show} |
| 122 | + onHide={onHide} |
| 123 | + aria-labelledby="legislator-sign-up-modal" |
| 124 | + centered |
| 125 | + size="lg" |
| 126 | + className={clsx(showTos && "opacity-0")} |
| 127 | + > |
| 128 | + <Modal.Header closeButton> |
| 129 | + <Modal.Title id="legislator-sign-up-modal"> |
| 130 | + {t("signUpAsLegislator") ?? "Sign Up as Legislator"} |
| 131 | + </Modal.Title> |
| 132 | + </Modal.Header> |
| 133 | + <Modal.Body> |
| 134 | + <Col md={12} className="mx-auto"> |
| 135 | + {createLegislatorWithEmailAndPassword.error ? ( |
| 136 | + <Alert variant="danger"> |
| 137 | + {createLegislatorWithEmailAndPassword.error.message} |
| 138 | + </Alert> |
| 139 | + ) : null} |
| 140 | + |
| 141 | + <Form noValidate onSubmit={onSubmit}> |
| 142 | + <TermsOfServiceModal |
| 143 | + show={showTos} |
| 144 | + onHide={() => setTosStep("not-agreed")} |
| 145 | + onAgree={() => setTosStep("agreed")} |
| 146 | + /> |
| 147 | + |
| 148 | + <Stack gap={3} className="mb-4"> |
| 149 | + <Input |
| 150 | + label={t("email") ?? "Email"} |
| 151 | + type="email" |
| 152 | + {...register("email", { |
| 153 | + required: t("emailIsRequired") ?? "An email is required." |
| 154 | + })} |
| 155 | + error={errors.email?.message} |
| 156 | + /> |
| 157 | + |
| 158 | + <Input |
| 159 | + label={t("fullName") ?? "Full Name"} |
| 160 | + type="text" |
| 161 | + {...register("fullName", { |
| 162 | + validate: value => |
| 163 | + value.trim().length >= 2 || |
| 164 | + t("errEmptyAndMinLength").toString(), |
| 165 | + required: t("nameIsRequired") ?? "A full name is required." |
| 166 | + })} |
| 167 | + error={errors.fullName?.message} |
| 168 | + /> |
| 169 | + |
| 170 | + <Form.Group controlId="legislatorSearch"> |
| 171 | + <Form.Label> |
| 172 | + {t("selectLegislator") ?? "Select your legislator profile"} |
| 173 | + </Form.Label> |
| 174 | + <Search |
| 175 | + index={memberIndex} |
| 176 | + update={member => { |
| 177 | + setSelectedMember(member) |
| 178 | + if (member) setMemberError(undefined) |
| 179 | + }} |
| 180 | + memberId={selectedMember?.id} |
| 181 | + placeholder={ |
| 182 | + t("searchLegislators") ?? "Search by name or district..." |
| 183 | + } |
| 184 | + menuPortalTarget={document.body} |
| 185 | + styles={{ menuPortal: base => ({ ...base, zIndex: 9999 }) }} |
| 186 | + /> |
| 187 | + {memberError && ( |
| 188 | + <Form.Text className="text-danger">{memberError}</Form.Text> |
| 189 | + )} |
| 190 | + </Form.Group> |
| 191 | + |
| 192 | + <Row className="g-3"> |
| 193 | + <Col md={6}> |
| 194 | + <PasswordInput |
| 195 | + label={t("password") ?? "Password"} |
| 196 | + {...register("password", { |
| 197 | + required: |
| 198 | + t("passwordRequired") ?? "A password is required.", |
| 199 | + minLength: { |
| 200 | + value: 8, |
| 201 | + message: |
| 202 | + t("passwordLength") ?? |
| 203 | + "Your password must be 8 characters or longer." |
| 204 | + }, |
| 205 | + deps: ["confirmedPassword"] |
| 206 | + })} |
| 207 | + error={errors.password?.message} |
| 208 | + /> |
| 209 | + </Col> |
| 210 | + |
| 211 | + <Col md={6}> |
| 212 | + <PasswordInput |
| 213 | + label={t("confirmPassword") ?? "Confirm Password"} |
| 214 | + {...register("confirmedPassword", { |
| 215 | + required: |
| 216 | + t("mustConfirmPassword") ?? |
| 217 | + "You must confirm your password.", |
| 218 | + validate: confirmedPassword => { |
| 219 | + const password = getValues("password") |
| 220 | + return confirmedPassword !== password |
| 221 | + ? t("mustMatch") ?? |
| 222 | + "Confirmed password must match password." |
| 223 | + : undefined |
| 224 | + } |
| 225 | + })} |
| 226 | + error={errors.confirmedPassword?.message} |
| 227 | + /> |
| 228 | + </Col> |
| 229 | + </Row> |
| 230 | + </Stack> |
| 231 | + {tosStep === "agreed" ? ( |
| 232 | + <LoadingButton |
| 233 | + id="legislator-loading-button" |
| 234 | + type="submit" |
| 235 | + className="w-100" |
| 236 | + loading={createLegislatorWithEmailAndPassword.loading} |
| 237 | + > |
| 238 | + {t("signUp") ?? "Sign Up"} |
| 239 | + </LoadingButton> |
| 240 | + ) : ( |
| 241 | + <Button |
| 242 | + className="w-100" |
| 243 | + type="button" |
| 244 | + onClick={handleContinueClick} |
| 245 | + > |
| 246 | + {t("continue") ?? "Continue"} |
| 247 | + </Button> |
| 248 | + )} |
| 249 | + </Form> |
| 250 | + </Col> |
| 251 | + </Modal.Body> |
| 252 | + </Modal> |
| 253 | + </> |
| 254 | + ) |
| 255 | +} |
0 commit comments