Loading responses...
JSON definition · shared/definitions/dashboard.json
View on GitHub
{
"title": "Product feedback",
"elements": [
{
"type": "rating",
"name": "satisfaction",
"title": "How satisfied are you with the product?",
"rateMin": 1,
"rateMax": 5
},
{
"type": "rating",
"name": "recommend",
"title": "How likely are you to recommend us?",
"rateMin": 0,
"rateMax": 10
},
{
"type": "radiogroup",
"name": "channel",
"title": "Where did you hear about us?",
"choices": [
"Search",
"Social media",
"A colleague",
"Conference"
]
},
{
"type": "checkbox",
"name": "features",
"title": "Which features do you use?",
"choices": [
"Forms",
"Dashboard",
"Creator",
"PDF export"
]
},
{
"type": "comment",
"name": "improve",
"title": "What should we improve?"
}
]
}
Client · shared/client/dashboard.js · sjs:dashboard.client
View on GitHub
// Load answers with their submission dates for Dashboard's built-in date filter
async function loadResults(signal) {
const query = new URLSearchParams({ formId: "feedback", includeCreatedAt: "1", limit: "5000" });
const [definition, response] = await Promise.all([
loadSchema(), fetch(`/api/responses?${query}`, { signal })
]);
if (!response.ok) throw new Error("Responses load failed");
const data = await response.json();
if (!Array.isArray(data)) throw new Error("Invalid responses");
return { definition, data: data.map(row => ({ ...row.data, createdAt: row.createdAt })) };
}
function renderResults(definition, data) {
if (!dashboard) {
const survey = new Model(definition);
dashboard = new Dashboard({
questions: survey.getAllQuestions(), data,
dateFieldName: "createdAt", datePeriod: "last30days"
});
dashboard.render(container);
} else {
dashboard.updateData(data);
}
}
Server · routes/dashboard.js · sjs:dashboard.server
View on GitHub
// GET /api/responses?formId=&includeCreatedAt=&from=&to=&limit=&offset=
app.get("/api/responses", (req, res) => {
const query = (name, fallback) => (typeof req.query[name] === "string" ? req.query[name] : fallback);
const to = query("to");
const limit = Math.min(Math.max(Number.parseInt(query("limit", "500"), 10) || 0, 1), 5000); // page on the server
const offset = Math.max(Number.parseInt(query("offset", "0"), 10) || 0, 0);
const rows = db(req).prepare(`SELECT data, created_at FROM responses
WHERE form_id = ? AND created_at >= ? ${to ? "AND substr(created_at, 1, 10) <= ?" : ""}
ORDER BY created_at, id LIMIT ? OFFSET ?`) // ISO 8601 UTC strings compare as dates
.all(query("formId", ""), query("from", "1970-01-01"), ...(to ? [to] : []), limit, offset);
// Include submission dates separately when requested by Dashboard
if (/^(1|true|on|yes)$/.test(query("includeCreatedAt", ""))) {
return res.json(rows.map((row) => ({ data: JSON.parse(row.data), createdAt: row.created_at })));
}
res.type("json").send(`[${rows.map((row) => row.data).join(",")}]`); // documents as stored, not re-encoded
});