import { useEffect, useId, useRef, useState, type FormEvent } from "react"; import { contactKindLabels, describeResponseFailure, orderedContactKinds, type KeyBinding, type ResponseFailure, } from "../../../entities/invitation"; import type { CandidateInvitation, ContactConsentOffer } from "../../../shared/api/schemas"; import { Button } from "../../../shared/ui"; import { acceptReady, acceptanceBody, consentParagraphs, emptyAcceptDraft, setAcceptConfirmed, toggleAcceptField, type AcceptDraft, } from "../model/accept-draft"; import { runAcceptCommand } from "../model/accept-command"; import styles from "./accept-contact-consent-form.module.css"; type AcceptContactConsentFormProps = { /** The invitation whose current terms version the candidate is deciding on. */ invitation: CandidateInvitation; /** The consent C4 offer of that version: the text and the kinds the profile holds. */ offer: ContactConsentOffer; organizationName: string; csrfToken: string; onAccepted: (invitation: CandidateInvitation) => void; onFailure: (failure: ResponseFailure) => void; }; /** * Accepts the terms with the consent C4. Nothing is preselected: the * candidate ticks each contact kind they agree to disclose and confirms the * text, and only then can the answer be sent. The form is keyed by the terms * version by its parent, so a new version starts with an empty choice. */ export function AcceptContactConsentForm({ invitation, offer, organizationName, csrfToken, onAccepted, onFailure, }: AcceptContactConsentFormProps) { const id = useId(); const [draft, setDraft] = useState(emptyAcceptDraft); const [busy, setBusy] = useState(false); const [failure, setFailure] = useState(null); const [hint, setHint] = useState(null); const inFlight = useRef(false); const keyRef = useRef(null); const alertRef = useRef(null); const ready = acceptReady(draft, offer); const paragraphs = consentParagraphs(offer.text.text); const kinds = orderedContactKinds(offer.available_fields); useEffect(() => { if (failure) alertRef.current?.focus(); }, [failure]); function submit(event: FormEvent) { event.preventDefault(); if (inFlight.current) return; if (!ready) { setHint( "Отметьте хотя бы один контакт и подтвердите согласие: без этого приглашение не принимается.", ); return; } inFlight.current = true; setBusy(true); setFailure(null); setHint(null); const body = acceptanceBody({ expectedVersion: invitation.version, termsVersion: invitation.terms_version, offer, draft, }); void runAcceptCommand({ invitationId: invitation.id, csrfToken, body }, keyRef.current).then( (outcome) => { inFlight.current = false; if (outcome.kind === "accepted") { keyRef.current = null; onAccepted(outcome.invitation); return; } keyRef.current = outcome.key; setBusy(false); const described = describeResponseFailure("accept", outcome.error); setFailure(described); onFailure(described); }, ); } return (

Принять приглашение

Контакты, которые получит компания

Ничего не отмечено заранее. Отметьте только те контакты, которые компания сможет прочитать после принятия. Остальные не передаются.

{kinds.map((kind) => ( ))}
{paragraphs.map((paragraph, index) => ( // The consent text is static for the lifetime of this form.

{paragraph}

))}

Кнопка станет доступна, когда отмечен хотя бы один контакт и подтверждено согласие.

{hint ? (

{hint}

) : null} {failure ? (
{failure.message}
) : null}
); }