add schema to submission display if available

This commit is contained in:
Matthias Nannt
2022-11-30 16:15:40 +01:00
parent 47dd1d04de
commit 5bd09c13b6
5 changed files with 99 additions and 4 deletions
@@ -5,7 +5,25 @@ import { useForm } from "@/lib/forms";
import clsx from "clsx";
export default function SubmissionDisplay({ params, submission }) {
const { form, isLoadingForm } = useForm(params.formId, params.formId);
const { form, isLoadingForm } = useForm(params.formId, params.teamId);
const MergeWithSchema = (submissionData, schema) => {
if (!schema) {
return submissionData;
}
const mergedData = {};
for (const elem of schema.children) {
if (["submit"].includes(elem.type)) {
continue;
}
if (elem.name in submissionData) {
mergedData[elem.label] = submissionData[elem.name];
} else {
mergedData[elem.label] = "not provided";
}
}
return mergedData;
};
if (isLoadingForm) {
return <LoadingSpinner />;
@@ -14,7 +32,7 @@ export default function SubmissionDisplay({ params, submission }) {
return (
<div className="flow-root">
<ul role="list" className="divide-ui-gray-light divide-y">
{Object.entries(submission.data).map(([key, value]) => (
{Object.entries(MergeWithSchema(submission.data, form.schema)).map(([key, value]) => (
<li key={key} className="py-5">
<p className="text-sm font-semibold text-gray-800">{key}</p>
@@ -39,6 +39,17 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse)
return res.json(forms);
}
// POST /api/teams[teamId]/forms/[formId]
// Replace a specific form
else if (req.method === "POST") {
const data = { ...req.body, updatedAt: new Date() };
const prismaRes = await prisma.form.update({
where: { id: formId },
data,
});
return res.json(prismaRes);
}
// Delete /api/teams[teamId]/forms/[formId]
// Deletes a single form
else if (req.method === "DELETE") {
@@ -55,7 +55,7 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse)
// Deletes a single form
else if (req.method === "DELETE") {
const prismaRes = await prisma.pipeline.delete({
where: { id: pipelineId, formId },
where: { id: pipelineId },
});
return res.json(prismaRes);
}
@@ -0,0 +1,67 @@
import { getSessionOrUser } from "@/lib/apiHelper";
import { prisma } from "@formbricks/database";
import type { NextApiRequest, NextApiResponse } from "next";
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
// Check Authentication
const user: any = await getSessionOrUser(req, res);
if (!user) {
return res.status(401).json({ message: "Not authenticated" });
}
const teamId = req.query.teamId.toString();
const formId = req.query.formId.toString();
const submissionId = req.query.submissionId.toString();
// check team permission
const membership = await prisma.membership.findUnique({
where: {
userId_teamId: {
userId: user.id,
teamId,
},
},
});
if (membership === null) {
return res.status(403).json({ message: "You don't have access to this team or this team doesn't exist" });
}
// GET /api/teams[teamId]/forms/[formId]/submissions/[submissionId]
// Get a specific submission
if (req.method === "GET") {
console.log("call2");
const submission = await prisma.submission.findFirst({
where: {
id: submissionId,
formId: formId,
},
});
return res.json(submission);
}
// POST /api/teams[teamId]/forms/[formId]/submissions/[submissionId]
// Replace a specific submission
else if (req.method === "POST") {
const data = { ...req.body, updatedAt: new Date() };
const prismaRes = await prisma.submission.update({
where: { id: submissionId },
data,
});
return res.json(prismaRes);
}
// Delete /api/teams[teamId]/forms/[formId]/submissions/[submissionId]
// Deletes a single form
else if (req.method === "DELETE") {
const prismaRes = await prisma.submission.delete({
where: { id: submissionId },
});
return res.json(prismaRes);
}
// Unknown HTTP Method
else {
throw new Error(`The HTTP ${req.method} method is not supported by this route.`);
}
}
@@ -10,7 +10,6 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse)
}
const teamId = req.query.teamId.toString();
const formId = req.query.formId.toString();
// check team permission