JSON definition · shared/definitions/variable-presets.json
View on GitHub
{
"title": "Support request",
"elements": [
{
"type": "dropdown",
"name": "product",
"title": "Product",
"choices": [
"Form Library",
"Survey Creator",
"Dashboard",
"PDF Generator"
]
},
{
"type": "comment",
"name": "issue",
"title": "Describe the issue",
"isRequired": true
},
{
"type": "boolean",
"name": "callback",
"title": "Call me back within one hour",
"description": "Available to customers on the premium plan",
"visibleIf": "{customerTier} = 'premium'"
}
]
}
Client · shared/client/variable-presets.js · sjs:variable-presets.client
View on GitHub
const creator = new SurveyCreator({
variablePresets: {
definition: {
elements: [{
type: "dropdown", name: "customerTier", title: "Customer plan",
choices: [{ value: "basic", text: "Basic" }, { value: "premium", text: "Premium" }],
isRequired: true
}]
},
presets: []
}
});
creator.onVariablePresetsChanged.add((_, options) => {
// Switching scenarios changes only Preview; editing persists the preset list
if (loaded && options.reason === "edit") { saver.changed(); saver.save(); }
});
Client · shared/client/variable-presets.js · sjs:variable-presets.save
View on GitHub
// Save the list, rather than wrapping it in another variablePresets configuration
const saver = createSchemaSaver(() => creator.variablePresets.presets ?? [], async json => {
const response = await fetch(`/api/variable-presets/${formId}`, {
method: "PUT", headers: { "Content-Type": "application/json" }, body: json
});
if (!response.ok) throw await responseError(response, "Couldn't save the presets. Please try again.");
}, ({ state, error }) => {
if (!loaded) return;
showError(state === "error" ? `${error} Your preset edits remain in this editor.` : "");
retrySave.hidden = state !== "error";
retrySave.disabled = state === "saving";
if (state === "error") creator.notify(error, "error");
});
Server · routes/variable-presets.js · sjs:variable-presets.server
View on GitHub
// GET /api/variable-presets/:formId — return the stored preset list, or [] when none are saved
app.get("/api/variable-presets/:formId", (req, res) => {
const row = db(req).prepare("SELECT json FROM variable_presets WHERE key = ?").get(req.params.formId);
res.type("json").send(row?.json ?? "[]");
});
// PUT /api/variable-presets/:formId — store the variable presets as-is: it's one JSON document
app.put("/api/variable-presets/:formId", (req, res) => {
const denied = can(req.user, "edit-forms"); // editing is an admin action: 403 otherwise
if (denied) throw httpError(403, denied);
if (req.body === undefined) throw httpError(400, "Expected a list of presets"); // not a JSON body
// req.body is the parsed raw body: {} stays {}; store it with JSON.stringify, never a copy
db(req).prepare("INSERT INTO variable_presets (key, json) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET json = excluded.json")
.run(req.params.formId, JSON.stringify(req.body));
res.sendStatus(204);
});