Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: store feedback response

Marc Seitz133ec157b320
diff --git a/components/view/question.tsx b/components/view/question.tsx
index 5aaae0b1..cf8db4f6 100644
--- a/components/view/question.tsx
+++ b/components/view/question.tsx
@@ -18,6 +18,22 @@ export default function Question({
const [answer, setAnswer] = useState<"yes" | "no" | "">("");
const handleQuestionSubmit = async (answer: string) => {
+ if (answer === "") return;
+
+ const response = await fetch(`/api/feedback`, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ answer: answer,
+ feedbackId: feedback.id,
+ viewId: viewId,
+ }),
+ });
+
+ if (response.status === 200) {
+ setAnswer(answer as "yes" | "no");
+ setSubmittedFeedback(true);
+ }
};
if (submittedFeedback) {
diff --git a/pages/api/feedback/index.ts b/pages/api/feedback/index.ts
new file mode 100644
index 00000000..4d2f93c9
--- /dev/null
+++ b/pages/api/feedback/index.ts
@@ -0,0 +1,69 @@
+import { NextApiRequest, NextApiResponse } from "next";
+import prisma from "@/lib/prisma";
+import { getFile } from "@/lib/files/get-file";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method === "POST") {
+ // POST /api/feedback
+ const { answer, feedbackId, viewId } = req.body as {
+ answer: string;
+ feedbackId: string;
+ viewId: string;
+ };
+
+ try {
+ const feedback = await prisma.feedback.findUnique({
+ where: {
+ id: feedbackId,
+ },
+ select: {
+ documentId: true,
+ data: true,
+ },
+ });
+
+ // if feedback does not exist, we should not record any response
+ if (!feedback) {
+ return res.status(404).json({ error: "Feedback not found" });
+ }
+
+ const view = await prisma.view.findUnique({
+ where: {
+ id: viewId,
+ documentId: feedback.documentId,
+ },
+ });
+
+ // if view does not exist, we should not record any response
+ if (!view) {
+ return res.status(404).json({ error: "View not found" });
+ }
+
+ // create a feedback response
+ await prisma.feedbackResponse.create({
+ data: {
+ feedbackId: feedbackId,
+ viewId: viewId,
+ data: {
+ ...(feedback.data as { question: string; type: string }),
+ answer: answer,
+ },
+ },
+ });
+
+ return res.status(200).json({ message: "Feedback response recorded" });
+ } catch (error) {
+ return res.status(500).json({
+ message: "Internal Server Error",
+ error: (error as Error).message,
+ });
+ }
+ }
+
+ // We only allow POST requests
+ res.setHeader("Allow", ["POST"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+}