192 lines
6.5 KiB
TypeScript
192 lines
6.5 KiB
TypeScript
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<AcceptDraft>(emptyAcceptDraft);
|
|
const [busy, setBusy] = useState(false);
|
|
const [failure, setFailure] = useState<ResponseFailure | null>(null);
|
|
const [hint, setHint] = useState<string | null>(null);
|
|
const inFlight = useRef(false);
|
|
const keyRef = useRef<KeyBinding | null>(null);
|
|
const alertRef = useRef<HTMLDivElement>(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<HTMLFormElement>) {
|
|
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 (
|
|
<form className={styles.form} onSubmit={submit} noValidate aria-labelledby={`${id}-title`}>
|
|
<h3 id={`${id}-title`} className={styles.title}>
|
|
Принять приглашение
|
|
</h3>
|
|
|
|
<fieldset className={styles.fieldset} aria-describedby={`${id}-fields-hint`}>
|
|
<legend className={styles.legend}>Контакты, которые получит компания</legend>
|
|
<p id={`${id}-fields-hint`} className={styles.hint}>
|
|
Ничего не отмечено заранее. Отметьте только те контакты, которые компания сможет прочитать
|
|
после принятия. Остальные не передаются.
|
|
</p>
|
|
{kinds.map((kind) => (
|
|
<label key={kind} className={styles.option}>
|
|
<input
|
|
type="checkbox"
|
|
name="contact-field"
|
|
value={kind}
|
|
checked={draft.fields.includes(kind)}
|
|
onChange={(event) => {
|
|
const checked = event.currentTarget.checked;
|
|
setDraft((current) => toggleAcceptField(current, kind, checked));
|
|
}}
|
|
/>
|
|
<span>{contactKindLabels[kind]}</span>
|
|
</label>
|
|
))}
|
|
</fieldset>
|
|
|
|
<section className={styles.consent} aria-labelledby={`${id}-consent-title`}>
|
|
<h4 id={`${id}-consent-title`} className={styles.subtitle}>
|
|
Текст согласия C4
|
|
</h4>
|
|
<div className={styles.text}>
|
|
{paragraphs.map((paragraph, index) => (
|
|
// The consent text is static for the lifetime of this form.
|
|
<p key={index}>{paragraph}</p>
|
|
))}
|
|
</div>
|
|
</section>
|
|
|
|
<label className={styles.option}>
|
|
<input
|
|
type="checkbox"
|
|
name="consent-confirmed"
|
|
checked={draft.confirmed}
|
|
onChange={(event) => {
|
|
const confirmed = event.currentTarget.checked;
|
|
setDraft((current) => setAcceptConfirmed(current, confirmed));
|
|
}}
|
|
/>
|
|
<span>
|
|
{`Я прочитал(а) текст согласия и согласен(на) передать отмеченные контакты компании «${organizationName}».`}
|
|
</span>
|
|
</label>
|
|
|
|
<p id={`${id}-ready-hint`} className={styles.hint}>
|
|
Кнопка станет доступна, когда отмечен хотя бы один контакт и подтверждено согласие.
|
|
</p>
|
|
|
|
{hint ? (
|
|
<p className={styles.hint} role="status">
|
|
{hint}
|
|
</p>
|
|
) : null}
|
|
|
|
{failure ? (
|
|
<div
|
|
ref={alertRef}
|
|
tabIndex={-1}
|
|
role="alert"
|
|
className={styles.alert}
|
|
data-failure={failure.kind}
|
|
>
|
|
{failure.message}
|
|
</div>
|
|
) : null}
|
|
|
|
<div className={styles.actions}>
|
|
<Button
|
|
type="submit"
|
|
isBusy={busy}
|
|
aria-disabled={!ready || undefined}
|
|
aria-describedby={`${id}-ready-hint`}
|
|
className={styles.submit}
|
|
>
|
|
{busy ? "Отправляем ответ..." : "Принять и передать контакты"}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|