import { isPermanentDenial, mayHaveBeenWritten, relayServerFailure, UNCURATED_DENIAL_MESSAGE } from '../../../utils/fetchFailureMessage'; export const FAMILY_SAVED = 'Their details are saved.'; // The patient's words for a family member that was not saved. Only an answer from the server can carry // a sentence written for people; a thrown code error has none worth showing. export const familySaveFailure = (error: unknown, isNew: boolean): string => { const fallback = isNew ? "We couldn't add this person. Please try again." : "We couldn't save their details. Please try again."; const answered = typeof (error as { status?: unknown } | null)?.status === 'number'; return answered ? relayServerFailure(error, fallback) : fallback; }; export const FAMILY_INVITE_REFUSED = 'This invitation was refused: you do not have permission to add this person. Nobody was added and nothing was granted.'; export const FAMILY_CHANGE_REFUSED = 'This change was refused: you do not have permission to change their details. Nothing was saved.'; export const FAMILY_INVITE_UNCONFIRMED = 'We did not hear back, so this invitation may or may not have been sent. Check the list before sending it again.'; export const FAMILY_CHANGE_UNCONFIRMED = 'We did not hear back, so their details may or may not have been saved. Check them before saving again.'; /** What the dialog says when a save was not made: a refusal, a save whose answer was lost, or a failure. */ export type FamilySaveProblem = { kind: 'refused' | 'unconfirmed' | 'failed'; message: string }; // Said in the dialog itself, which stays open with what was entered: a toast can sit behind it. A refusal says it was // refused - in the server's own sentence when it wrote one for people, never the gateway's policy text - and is kept // apart from a save that may have landed (no answer reached us) and from one that failed. export const familySaveProblem = (error: unknown, isNew: boolean): FamilySaveProblem => { const status = (error as { status?: unknown } | null)?.status; if (typeof status === 'number' && isPermanentDenial(status)) { const said = familySaveFailure(error, isNew); const plain = said === UNCURATED_DENIAL_MESSAGE ? (isNew ? FAMILY_INVITE_REFUSED : FAMILY_CHANGE_REFUSED) : said; return { kind: 'refused', message: plain }; } if (mayHaveBeenWritten(error)) { return { kind: 'unconfirmed', message: isNew ? FAMILY_INVITE_UNCONFIRMED : FAMILY_CHANGE_UNCONFIRMED }; } return { kind: 'failed', message: familySaveFailure(error, isNew) }; };