|
| 1 | +import { useCallback, useState } from "react"; |
| 2 | + |
| 3 | +import { useOrgVerification } from "@/common/api/indexer/hooks"; |
| 4 | +import { taxVerificationApi } from "@/common/api/indexer/tax-verification"; |
| 5 | +import { TextField } from "@/common/ui/form/components"; |
| 6 | +import { Button, FormLabel } from "@/common/ui/layout/components"; |
| 7 | +import { cn } from "@/common/ui/layout/utils"; |
| 8 | + |
| 9 | +import { Row } from "./editor-elements"; |
| 10 | + |
| 11 | +const STATUS_STYLES: Record<string, { bg: string; text: string; label: string }> = { |
| 12 | + Pending: { bg: "bg-[#FFF0E1]", text: "text-[#EA6A25]", label: "Verifying..." }, |
| 13 | + Approved: { bg: "bg-[#FFF0E1]", text: "text-[#EA6A25]", label: "Unverified 501(c)(3)" }, |
| 14 | + Rejected: { bg: "bg-[#FFE1E1]", text: "text-[#ED464F]", label: "Not Verified" }, |
| 15 | +}; |
| 16 | + |
| 17 | +const FieldDisplay = ({ label, value }: { label: string; value: string | null }) => ( |
| 18 | + <div className="flex w-full flex-col gap-[0.45em] text-[14px]"> |
| 19 | + <FormLabel className="m-0">{label}</FormLabel> |
| 20 | + <div className="rounded-md border border-neutral-200 bg-neutral-50 px-3 py-2 text-neutral-600"> |
| 21 | + {value || "—"} |
| 22 | + </div> |
| 23 | + </div> |
| 24 | +); |
| 25 | + |
| 26 | +type OrgVerificationSectionProps = { |
| 27 | + accountId?: string; |
| 28 | +}; |
| 29 | + |
| 30 | +export const OrgVerificationSection: React.FC<OrgVerificationSectionProps> = ({ accountId }) => { |
| 31 | + const { data: verification, mutate } = useOrgVerification({ |
| 32 | + accountId: accountId ?? "", |
| 33 | + enabled: !!accountId, |
| 34 | + }); |
| 35 | + |
| 36 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 37 | + const [submitError, setSubmitError] = useState<string | null>(null); |
| 38 | + const [ein, setEin] = useState(""); |
| 39 | + |
| 40 | + const handleSubmit = useCallback(async () => { |
| 41 | + if (!accountId) return; |
| 42 | + |
| 43 | + const trimmedEin = ein.trim(); |
| 44 | + |
| 45 | + // EIN format validation |
| 46 | + if (!/^\d{2}-?\d{7}$/.test(trimmedEin)) { |
| 47 | + setSubmitError("EIN must be in XX-XXXXXXX format (9 digits)"); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + setIsSubmitting(true); |
| 52 | + setSubmitError(null); |
| 53 | + |
| 54 | + const result = await taxVerificationApi.submitOrgVerification({ |
| 55 | + account_id: accountId, |
| 56 | + ein: trimmedEin, |
| 57 | + }); |
| 58 | + |
| 59 | + setIsSubmitting(false); |
| 60 | + |
| 61 | + if (result.success) { |
| 62 | + mutate(); |
| 63 | + } else { |
| 64 | + setSubmitError(result.message ?? "Verification failed"); |
| 65 | + } |
| 66 | + }, [accountId, ein, mutate]); |
| 67 | + |
| 68 | + if (!accountId) return null; |
| 69 | + |
| 70 | + const statusStyle = verification ? STATUS_STYLES[verification.status] : null; |
| 71 | + |
| 72 | + // Approved view — show IRS data (unverified mode) |
| 73 | + if (verification?.status === "Approved") { |
| 74 | + return ( |
| 75 | + <div className="mt-6 flex flex-col gap-4"> |
| 76 | + {statusStyle && ( |
| 77 | + <div |
| 78 | + className={cn( |
| 79 | + "inline-flex w-fit items-center gap-2 rounded-full px-3 py-1 text-xs font-semibold uppercase tracking-wider", |
| 80 | + statusStyle.bg, |
| 81 | + statusStyle.text, |
| 82 | + )} |
| 83 | + > |
| 84 | + {statusStyle.label} |
| 85 | + </div> |
| 86 | + )} |
| 87 | + |
| 88 | + <div className="rounded-md border border-amber-200 bg-amber-50 p-3 text-sm text-amber-800"> |
| 89 | + This data is auto-populated from IRS records. It confirms this EIN belongs to a registered |
| 90 | + 501(c)(3) organization, but does not verify that the submitter is an authorized |
| 91 | + representative. |
| 92 | + </div> |
| 93 | + |
| 94 | + <Row> |
| 95 | + <FieldDisplay label="EIN" value={verification.ein} /> |
| 96 | + <FieldDisplay label="Organization Name (IRS)" value={verification.legal_name} /> |
| 97 | + </Row> |
| 98 | + |
| 99 | + <Row> |
| 100 | + <FieldDisplay label="Address" value={verification.address} /> |
| 101 | + <FieldDisplay |
| 102 | + label="City / State / Zip" |
| 103 | + value={ |
| 104 | + [verification.city, verification.state, verification.zip_code] |
| 105 | + .filter(Boolean) |
| 106 | + .join(", ") || null |
| 107 | + } |
| 108 | + /> |
| 109 | + </Row> |
| 110 | + |
| 111 | + <Row> |
| 112 | + <FieldDisplay label="NTEE Code" value={verification.ntee_code} /> |
| 113 | + <FieldDisplay label="IRS Ruling Date" value={verification.ruling_date} /> |
| 114 | + </Row> |
| 115 | + </div> |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + // Rejected view — show reason and allow retry |
| 120 | + if (verification?.status === "Rejected") { |
| 121 | + return ( |
| 122 | + <div className="mt-6 flex flex-col gap-4"> |
| 123 | + <div |
| 124 | + className={cn( |
| 125 | + "inline-flex w-fit items-center gap-2 rounded-full px-3 py-1 text-xs font-semibold uppercase tracking-wider", |
| 126 | + STATUS_STYLES.Rejected.bg, |
| 127 | + STATUS_STYLES.Rejected.text, |
| 128 | + )} |
| 129 | + > |
| 130 | + {STATUS_STYLES.Rejected.label} |
| 131 | + </div> |
| 132 | + |
| 133 | + {verification.rejection_reason && ( |
| 134 | + <div className="rounded-md border border-red-200 bg-red-50 p-3 text-sm text-red-700"> |
| 135 | + {verification.rejection_reason} |
| 136 | + </div> |
| 137 | + )} |
| 138 | + |
| 139 | + <p className="text-sm text-neutral-600"> |
| 140 | + The EIN <strong>{verification.ein}</strong> could not be verified as a 501(c)(3). You can |
| 141 | + try again with a different EIN. |
| 142 | + </p> |
| 143 | + |
| 144 | + <Row> |
| 145 | + <TextField |
| 146 | + label="EIN (Employer Identification Number)" |
| 147 | + required |
| 148 | + type="text" |
| 149 | + value={ein} |
| 150 | + onChange={(e) => { |
| 151 | + setEin(e.target.value); |
| 152 | + setSubmitError(null); |
| 153 | + }} |
| 154 | + placeholder="XX-XXXXXXX" |
| 155 | + maxLength={10} |
| 156 | + classNames={{ root: "w-full" }} |
| 157 | + /> |
| 158 | + </Row> |
| 159 | + |
| 160 | + {submitError && ( |
| 161 | + <div className="rounded-md border border-red-200 bg-red-50 p-3 text-sm text-red-700"> |
| 162 | + {submitError} |
| 163 | + </div> |
| 164 | + )} |
| 165 | + |
| 166 | + <div className="mt-2"> |
| 167 | + <Button |
| 168 | + variant="standard-filled" |
| 169 | + onClick={handleSubmit} |
| 170 | + disabled={isSubmitting} |
| 171 | + type="button" |
| 172 | + > |
| 173 | + {isSubmitting ? "Verifying..." : "Try Again"} |
| 174 | + </Button> |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + // Default — no verification yet, show EIN input |
| 181 | + return ( |
| 182 | + <div className="mt-6 flex flex-col gap-4"> |
| 183 | + <Row> |
| 184 | + <TextField |
| 185 | + label="EIN (Employer Identification Number)" |
| 186 | + required |
| 187 | + type="text" |
| 188 | + value={ein} |
| 189 | + onChange={(e) => { |
| 190 | + setEin(e.target.value); |
| 191 | + setSubmitError(null); |
| 192 | + }} |
| 193 | + placeholder="XX-XXXXXXX" |
| 194 | + maxLength={10} |
| 195 | + classNames={{ root: "w-full" }} |
| 196 | + /> |
| 197 | + </Row> |
| 198 | + |
| 199 | + {submitError && ( |
| 200 | + <div className="rounded-md border border-red-200 bg-red-50 p-3 text-sm text-red-700"> |
| 201 | + {submitError} |
| 202 | + </div> |
| 203 | + )} |
| 204 | + |
| 205 | + <div className="mt-2"> |
| 206 | + <Button |
| 207 | + variant="standard-filled" |
| 208 | + onClick={handleSubmit} |
| 209 | + disabled={isSubmitting} |
| 210 | + type="button" |
| 211 | + > |
| 212 | + {isSubmitting ? "Verifying..." : "Verify 501(c)(3) Status"} |
| 213 | + </Button> |
| 214 | + </div> |
| 215 | + </div> |
| 216 | + ); |
| 217 | +}; |
0 commit comments