Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 | 1x 1x 22x 1x 22x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 7x 7x 7x 7x 7x 7x 7x 32x 25x 25x 32x 14x 7x 7x 7x 7x 7x 12x 32x 20x 32x 20x 32x 32x 32x 32x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 32x 20x 32x 20x 2x 20x 20x 20x 20x 20x 4x 16x 16x 16x 2x 14x | "use client";
import { fetchApi } from "@/lib/api";
import { useEffect, useState } from "react";
import {
Gift,
ShieldCheck,
Lock,
CheckCircle2,
XCircle,
Send,
Scale,
} from "lucide-react";
import PageIntro from "@/components/PageIntro";
import { useT } from "@/lib/i18n/translate";
import { useOwnRecord } from "@/lib/patient/use-own-record";
import type {
BonusProgramme,
ProgrammeConsent,
YearEvaluation,
} from "@/lib/patient/bonus-programme";
/** Why the programmes did not load: refused (401, 403) or failed (anything else). */
type LoadError =
| { kind: "refused" }
| { kind: "failed"; status: number | null };
/**
* Bonus programmes from other data spaces (#934, step 3). The catalogue
* crawler finds them as DCAT-AP offerings; this page lists them. A programme
* receives a yes or no per year, never the data behind it (ADR-080), and it
* is neither research nor the Art. 71 opt-out, so it has its own page.
* Joining, seeing and withdrawing are step 4 (ADR-080, decision 6), and so
* is the person's instruction to send a year's answer, which goes out only
* under an open consent. Step 6 (ADR-087): before sending, the member sees
* their own result for the ended year, worked out inside their record.
*/
/** The member's result for a program year, as far as it has loaded. */
type EvaluationState =
| { kind: "loading" }
| { kind: "ready"; evaluation: YearEvaluation }
| { kind: "failed"; status: number | null };
/**
* The record an operator (EDC_ADMIN) acts on here, as on the research page:
* P1, which every seed carries. A patient acts on their own.
*/
const DEMO_PATIENT_ID = "P1";
const day = (iso: string | null) => (iso ? iso.slice(0, 10) : "");
/** The year a rule version names (2025.1 is the rule for 2025). */
const ruleYear = (p: BonusProgramme) =>
Number((p.ruleVersion ?? "").slice(0, 4)) || new Date().getFullYear();
export default function BonusProgrammesPage() {
const t = useT("patient");
const tn = useT("nav");
const [programmes, setProgrammes] = useState<BonusProgramme[]>([]);
const [loading, setLoading] = useState(true);
const [loadError, setLoadError] = useState<LoadError | null>(null);
const [consents, setConsents] = useState<ProgrammeConsent[]>([]);
const [busy, setBusy] = useState<string | null>(null);
const [evaluations, setEvaluations] = useState<
Record<string, EvaluationState>
>({});
const [message, setMessage] = useState<{
programmeId: string;
text: string;
ok: boolean;
} | null>(null);
const subject = useOwnRecord();
const patientId =
subject.kind === "own"
? subject.patientId
: subject.kind === "operator"
? DEMO_PATIENT_ID
: null;
const query = patientId ? `?patientId=${encodeURIComponent(patientId)}` : "";
useEffect(() => {
Iif (subject.kind === "pending") return;
fetchApi(`/api/patient/programmes${query}`)
.then(async (r) => {
Iif (!r.ok) {
setLoadError(
r.status === 401 || r.status === 403
? { kind: "refused" }
: { kind: "failed", status: r.status },
);
return;
}
const d = await r.json();
setProgrammes(Array.isArray(d.programmes) ? d.programmes : []);
setConsents(Array.isArray(d.consents) ? d.consents : []);
})
.catch(() => setLoadError({ kind: "failed", status: null }))
.finally(() => setLoading(false));
}, [subject.kind, query]);
// The member's own result, for each program they joined (ADR-087). Read
// again after a join, so a new member sees it before sending anything.
const joined = programmes
.filter((p) =>
consents.some((c) => c.programmeId === p.programmeId && !c.withdrawnAt),
)
.map((p) => p.programmeId)
.join(" ");
useEffect(() => {
for (const programmeId of joined.split(" ").filter(Boolean)) {
const qs = new URLSearchParams({ programmeId });
Eif (patientId) qs.set("patientId", patientId);
setEvaluations((e) => ({ ...e, [programmeId]: { kind: "loading" } }));
fetchApi(`/api/patient/programmes/evaluation?${qs}`)
.then(async (r) => {
const d = r.ok ? await r.json() : null;
setEvaluations((e) => ({
...e,
[programmeId]: d?.evaluation
? { kind: "ready", evaluation: d.evaluation }
: { kind: "failed", status: r.status },
}));
})
.catch(() =>
setEvaluations((e) => ({
...e,
[programmeId]: { kind: "failed", status: null },
})),
);
}
}, [joined, patientId]);
const openFor = (programmeId: string) =>
consents.find((c) => c.programmeId === programmeId && !c.withdrawnAt) ??
null;
const lastFor = (programmeId: string) =>
consents.find((c) => c.programmeId === programmeId) ?? null;
const failed = (programmeId: string, status: number) =>
setMessage({
programmeId,
ok: false,
text: t("That did not work (HTTP {status}). Please try again.", {
status,
}),
});
const join = async (p: BonusProgramme) => {
setBusy(p.programmeId);
setMessage(null);
try {
const r = await fetchApi("/api/patient/programmes/consent", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ programmeId: p.programmeId, patientId }),
});
if (!r.ok) return failed(p.programmeId, r.status);
const d = await r.json();
// The static build answers { ok: true } without a record; the page
// then shows the consent it asked for.
const consent: ProgrammeConsent = d.consent ?? {
consentId: `local-${Date.now()}`,
programmeId: p.programmeId,
joinedAt: new Date().toISOString(),
withdrawnAt: null,
};
setConsents((cs) => [
consent,
...cs.filter((c) => c.consentId !== consent.consentId),
]);
} finally {
setBusy(null);
}
};
const withdraw = async (p: BonusProgramme) => {
setBusy(p.programmeId);
setMessage(null);
try {
const qs = new URLSearchParams({ programmeId: p.programmeId });
if (patientId) qs.set("patientId", patientId);
const r = await fetchApi(`/api/patient/programmes/consent?${qs}`, {
method: "DELETE",
});
if (!r.ok) return failed(p.programmeId, r.status);
const now = new Date().toISOString();
setConsents((cs) =>
cs.map((c) =>
c.programmeId === p.programmeId && !c.withdrawnAt
? { ...c, withdrawnAt: now }
: c,
),
);
} finally {
setBusy(null);
}
};
const send = async (p: BonusProgramme) => {
const year = ruleYear(p);
setBusy(p.programmeId);
setMessage(null);
try {
const r = await fetchApi("/api/patient/programmes/transfer", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ programmeId: p.programmeId, year, patientId }),
});
Iif (r.status === 409) {
return setMessage({
programmeId: p.programmeId,
ok: false,
text: t(
"{year} is not over yet. Nothing was sent; the hub works out the result once the year has ended.",
{ year },
),
});
}
Iif (!r.ok) return failed(p.programmeId, r.status);
const d = await r.json();
// The static build answers { ok: true } without a transfer; the page
// then shows the result it had already shown the member.
const shown = evaluations[p.programmeId];
const goalMet: boolean | undefined =
d.transfer?.goalMet ??
(shown?.kind === "ready" && shown.evaluation.status === "evaluated"
? shown.evaluation.goalMet
: undefined);
setMessage({
programmeId: p.programmeId,
ok: true,
text: goalMet
? t(
"Recorded for {year}: goal met, yes. Only this answer and your reference go to {publisher}, when it collects its answers from the hub.",
{ year, publisher: p.publisherName },
)
: t(
"Recorded for {year}: goal met, no. Only this answer and your reference go to {publisher}, when it collects its answers from the hub.",
{ year, publisher: p.publisherName },
),
});
} finally {
setBusy(null);
}
};
const dataSpaceName = (id: string | null) =>
id === "insurance-wellbeing"
? t("Insurance and wellbeing data space")
: t("Another data space");
return (
<div className="min-h-screen bg-(--bg)">
<div className="max-w-3xl mx-auto px-6 py-10">
<PageIntro
title={tn("Bonus Programs")}
icon={Gift}
description={t(
"Offers from other data spaces that reward what you do for your health. A program receives a yes or no once a year, never the data behind it. Whether you join is your decision alone (GDPR Art. 9(2)(a)).",
)}
prevStep={{
href: "/patient/research",
label: tn("Research Programs"),
}}
nextStep={{
href: "/patient/insights",
label: tn("Research Insights"),
}}
infoText={t(
"A bonus program is not research and not the opt-out. Joining one never enrolls you in a study, and your opt-out from secondary use (EHDS Art. 71) does not stop sharing you start yourself.",
)}
docLink={{
href: "/docs/ehds/bonus-programme-data-flow",
label: t("Which data goes where"),
}}
/>
{loading ? (
<div className="text-(--text-secondary) text-sm">
{t("Loading bonus programs…")}
</div>
) : loadError ? (
<div
role="alert"
data-testid="programmes-load-error"
className="rounded-lg border border-amber-300 dark:border-amber-700 bg-amber-50 dark:bg-amber-900/20 p-3 text-sm text-amber-900 dark:text-amber-200"
>
{loadError.kind === "refused"
? t(
"You may not see the bonus programs: they are shown to patients. If your session has ended, sign in again.",
)
: loadError.status
? t(
"The bonus programs could not be loaded (HTTP {status}). Please try again later.",
{ status: loadError.status },
)
: t(
"The bonus programs could not be loaded. Please try again later.",
)}
</div>
) : (
<div className="space-y-4">
<h2 className="text-lg font-semibold">
{t("Bonus programs on offer ({n})", { n: programmes.length })}
</h2>
{programmes.length === 0 && (
<div className="text-(--text-secondary) text-sm">
{t(
"No bonus program is on offer. The catalogue crawler has not found one in another data space yet.",
)}
</div>
)}
{programmes.map((p) => (
<article
key={p.programmeId}
data-testid="bonus-programme-card"
aria-labelledby={`programme-${p.programmeId}`}
className="rounded-xl border border-(--border) bg-(--surface-2)/50 p-4 space-y-3"
>
<header className="flex items-start justify-between gap-3">
<div>
<h3
id={`programme-${p.programmeId}`}
className="font-semibold text-sm"
>
{p.title}
</h3>
<div className="text-xs text-(--text-secondary)">
{p.publisherName}
</div>
</div>
<span className="shrink-0 text-xs text-(--text-secondary) border border-gray-600 rounded-sm px-2 py-0.5">
{dataSpaceName(p.dataSpace)}
</span>
</header>
{p.description && (
<p className="text-xs text-(--text-secondary)">
{p.description}
</p>
)}
{p.ruleDescription && (
<div className="text-xs">
<div className="font-medium">
{t("The rule, version {version}", {
version: p.ruleVersion ?? "",
})}
</div>
<p className="text-(--text-secondary)">
{p.ruleDescription}
</p>
</div>
)}
<div className="grid gap-2 sm:grid-cols-2 text-xs">
<div className="rounded-lg border border-teal-700 bg-teal-900/10 p-3">
<div className="flex items-center gap-1.5 font-medium text-teal-900 dark:text-teal-200">
<ShieldCheck size={14} />
{t("What {publisher} receives", {
publisher: p.publisherName,
})}
</div>
<p className="text-(--text-secondary) mt-1">
{t("The year, and goal met: yes or no.")}
</p>
</div>
<div className="rounded-lg border border-(--border) p-3">
<div className="flex items-center gap-1.5 font-medium">
<Lock size={14} />
{t("What never leaves your record")}
</div>
<p className="text-(--text-secondary) mt-1">
{t(
"Steps, heart rate, diagnoses, visit details and any date finer than the year.",
)}
</p>
</div>
</div>
<ProgrammeConsentPanel
programme={p}
evaluation={evaluations[p.programmeId] ?? null}
open={openFor(p.programmeId)}
last={lastFor(p.programmeId)}
busy={busy === p.programmeId}
message={
message?.programmeId === p.programmeId ? message : null
}
onJoin={() => join(p)}
onWithdraw={() => withdraw(p)}
onSend={() => send(p)}
/>
</article>
))}
</div>
)}
</div>
</div>
);
}
function ProgrammeConsentPanel({
programme,
evaluation,
open,
last,
busy,
message,
onJoin,
onWithdraw,
onSend,
}: {
programme: BonusProgramme;
evaluation: EvaluationState | null;
open: ProgrammeConsent | null;
last: ProgrammeConsent | null;
busy: boolean;
message: { text: string; ok: boolean } | null;
onJoin: () => void;
onWithdraw: () => void;
onSend: () => void;
}) {
const t = useT("patient");
const year = ruleYear(programme);
return (
<div
data-testid="bonus-programme-join-state"
className="border-t border-(--border) pt-3 space-y-2 text-xs"
>
<p className="text-(--text-secondary)">
{t(
"Joining is your consent to this program alone (GDPR Art. 9(2)(a)). It enrolls you in no study and is separate from your opt-out.",
)}
</p>
{open ? (
<>
<p
data-testid="bonus-programme-joined"
className="flex items-center gap-1 font-medium text-teal-800 dark:text-teal-400"
>
<CheckCircle2 size={12} />
{t("You joined on {date}.", { date: day(open.joinedAt) })}
</p>
{open.memberReference && (
<p
data-testid="bonus-programme-reference"
className="text-(--text-secondary)"
>
{t(
"Your reference for {publisher}: {reference}. Give it to {publisher} so that it can match your answer to you. The hub sends no name and no insurance number.",
{
publisher: programme.publisherName,
reference: open.memberReference,
},
)}
</p>
)}
{open.collectedAt && (
<p
data-testid="bonus-programme-collected"
className="text-(--text-secondary)"
>
{t("{publisher} collected your answer on {date}.", {
publisher: programme.publisherName,
date: day(open.collectedAt),
})}
</p>
)}
<YearResult
evaluation={evaluation}
year={year}
publisher={programme.publisherName}
/>
<div className="flex flex-wrap gap-3">
<button
onClick={onSend}
disabled={
busy ||
evaluation?.kind !== "ready" ||
evaluation.evaluation.status !== "evaluated"
}
className="flex items-center gap-1.5 px-3 py-1.5 rounded-sm bg-teal-700 hover:bg-teal-600 text-white font-medium transition-colors disabled:opacity-50"
>
<Send size={12} />
{busy ? t("Sending…") : t("Send my answer for {year}", { year })}
</button>
<button
onClick={onWithdraw}
disabled={busy}
className="flex items-center gap-1.5 text-red-800 dark:text-red-400 hover:text-red-900 dark:hover:text-red-300 transition-colors disabled:opacity-50"
>
<XCircle size={12} />
{busy ? t("Withdrawing…") : t("Withdraw (GDPR Art. 7(3))")}
</button>
</div>
</>
) : (
<>
{last?.withdrawnAt && (
<p data-testid="bonus-programme-withdrawn">
{t("You withdrew on {date}. Nothing is sent any more.", {
date: day(last.withdrawnAt),
})}
</p>
)}
<button
onClick={onJoin}
disabled={busy}
className="px-3 py-1.5 rounded-sm bg-teal-700 hover:bg-teal-600 text-white font-medium transition-colors disabled:opacity-50"
>
{busy ? t("Joining…") : t("Join this program")}
</button>
</>
)}
{message && (
<p
role={message.ok ? "status" : "alert"}
className={
message.ok
? "text-teal-800 dark:text-teal-300"
: "text-amber-900 dark:text-amber-200"
}
>
{message.text}
</p>
)}
</div>
);
}
/**
* The member's own result for the program year, before anything is sent
* (ADR-080, decision 3; ADR-087). The breakdown stays on this screen: what
* would go out is the year and goal met, yes or no.
*/
function YearResult({
evaluation,
year,
publisher,
}: {
evaluation: EvaluationState | null;
year: number;
publisher: string;
}) {
const t = useT("patient");
if (!evaluation || evaluation.kind === "loading") {
return (
<p className="text-(--text-secondary)">{t("Working out your result…")}</p>
);
}
Iif (evaluation.kind === "failed") {
return (
<p role="alert" className="text-amber-900 dark:text-amber-200">
{evaluation.status
? t("Your result could not be worked out (HTTP {status}).", {
status: evaluation.status,
})
: t("Your result could not be worked out.")}
</p>
);
}
const e = evaluation.evaluation;
if (e.status === "year-not-over") {
return (
<p data-testid="bonus-programme-year-not-over">
{t(
"{year} is not over. The hub works out your result from {date}; nothing can be sent before.",
{ year: e.year, date: e.evaluableFrom },
)}
</p>
);
}
return (
<div
data-testid="bonus-programme-result"
className="rounded-lg border border-(--border) p-3 space-y-1"
>
<div className="flex items-center gap-1.5 font-medium">
<Scale size={14} />
{t("Your result for {year}", { year })}
</div>
<p
data-testid="bonus-programme-goal"
className={
e.goalMet
? "font-semibold text-teal-800 dark:text-teal-300"
: "font-semibold text-amber-900 dark:text-amber-200"
}
>
{e.goalMet ? t("Goal met: yes") : t("Goal met: no")}
</p>
<p className="text-(--text-secondary)">
{t(
"Exercise: {minutes} minutes a week on average, from {weeks} weeks your tracker shared. The rule asks for {required}.",
{
minutes: e.activity.minutesPerWeek,
weeks: e.activity.weeksWithData,
required: e.activity.required,
},
)}
</p>
<p className="text-(--text-secondary)">
{t("Check-ups in {year}: {count}. The rule asks for one.", {
year: e.year,
count: e.checkUp.count,
})}
</p>
<p className="text-(--text-secondary)">
{t(
"Only the year and goal met, yes or no, would go to {publisher}. This breakdown stays in your record.",
{ publisher },
)}
</p>
</div>
);
}
|