/** * Joining a proposal to the form it opens into. * * The overlay wants three bands of fields; a proposal carries a kind and whatever the note filled in. This * is the one line between them, kept apart from both so the catalogue stays a description of the forms and * the composer stays ignorant of what a proposal is. */ import { composeRecordForm, noteMayFill, type ComposedField } from './composeRecordForm'; import { fieldsForKind } from './recordFieldCatalogue.generated'; import type { RecordProposal } from './recordProposals'; /** * The composed form for a proposal, or nothing when its kind has no form to compose from. * * NOTHING IS NOT AN EMPTY FORM, and the difference is the whole reason this returns `undefined` rather * than `[]`. A referral has no resource form yet - falling back to an empty list would open an overlay * with a working Save button and no fields, which files an item nobody described. The caller has to say * what happens instead. */ export const composeProposalForm = (proposal: RecordProposal): ComposedField[] | undefined => { const fields = fieldsForKind(proposal.kind); if (!fields) return undefined; // What the note may not fill (a vaccine's source) is not taken from it, so it is owed rather than answered. const fromNote = Object.fromEntries(Object.entries(proposal.prefilled) .filter(([name]) => fields.find((field) => field.name === name && !noteMayFill(field)) === undefined)); return composeRecordForm(fields, fromNote); };