JSON definition · shared/definitions/load-definition-variables-data.jsonView on GitHub
{
"title": "Insurance claim for {user.name} ({user.plan} plan)",
"description": "Tell us what happened; we usually answer within two working days.",
"completeText": "Submit claim",
"elements": [
{
"type": "text",
"name": "q_email",
"valueName": "customer_email",
"title": "Email",
"inputType": "email",
"isRequired": true
},
{
"type": "text",
"name": "q_amount",
"valueName": "amount",
"title": "Amount claimed",
"inputType": "number",
"min": 0
},
{
"type": "dropdown",
"name": "incident",
"title": "What happened?",
"choices": [
{
"value": "collision",
"text": "Collision"
},
{
"value": "theft",
"text": "Theft"
},
{
"value": "weather",
"text": "Storm or flood"
}
]
},
{
"type": "comment",
"name": "details",
"title": "Details"
},
{
"type": "boolean",
"name": "priority_handling",
"title": "Would you like priority handling?",
"description": "Available with your premium plan.",
"visibleIf": "{user.plan} = 'premium'"
}
]
}
Client · src/app/examples/load-definition-variables-data/client.tsx · sjs:load-definition-variables-data.clientView on GitHub
// Option A: Use the schema and user variables embedded in the HTML response
function createSurvey({ definition, variables }: Embedded) {
const survey = new Model(definition);
survey.setVariables(variables); // Variables are available in expressions such as {user.name} and are excluded from survey.data
return survey;
}
// Option B: Load the definition, variables, and previous answers in one request
async function loadSurvey(recordId: string) {
const { definition, variables, data } =
await fetch(`/api/forms/claim-request?record=${encodeURIComponent(recordId)}`).then(r => r.json());
const survey = createSurvey({ definition, variables });
survey.data = data; // Restore previously saved answers for editing
return survey;
}
Server · src/server/routes/load-definition-variables-data.ts · sjs:load-definition-variables-data.serverView on GitHub
// Option A: Embed the schema and user variables in the HTML response (src/app/claim/page.tsx renders them)
export function loadClaimPage(req: Request) {
const definition = readShared("definitions/load-definition-variables-data.json");
const user = currentUser(req);
const variables = { user: { name: user?.name ?? null, plan: user?.plan ?? null } };
return { definition, variables }; // the page escapes both with scriptJson()
}
// Option B: Return the definition, variables, and previous answers in one response
export function loadForm(req: Request, { id }: { id: string }) {
const form = db(req).prepare("SELECT json FROM forms WHERE key = ?").get(id) as { json: string } | undefined;
if (!form) throw httpError(404, "Unknown form");
const user = currentUser(req);
const saved = (id === "claim-request" // only the signed-in customer's own claims
? db(req).prepare("SELECT data FROM responses WHERE id = ? AND form_id = 'claim' AND data ->> 'customer_email' = ?")
.get(query(req, "record") ?? "", user?.email ?? "")
: db(req).prepare("SELECT data FROM responses WHERE id = ?").get(query(req, "record") ?? "")) as { data: string } | undefined;
return Response.json({
definition: JSON.parse(form.json), // the stored form definition
variables: { user: { name: user?.name ?? null, plan: user?.plan ?? null } },
data: saved ? JSON.parse(saved.data) : {}, // previous answers, or {} when there are none
});
}