Files
js/pages/api/feedback.ts
T
Jonas Daniels b51b7f9a6d fix v1 projects now showing up for a given address on dashboard (#101)
* fix v1 projects now showing up for a given address on dashboard

* add feedback modal

* fix up feedback form

* amek rating a bit safer
2022-05-02 19:47:30 -07:00

64 lines
1.6 KiB
TypeScript

import { NextApiRequest, NextApiResponse } from "next";
import invariant from "tiny-invariant";
import { shortenAddress } from "utils/usedapp-external";
interface FeedbackPayload {
scope: string;
rating: number;
feedback: string;
address: string;
}
export default async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method !== "POST") {
return res.status(400).json({ error: "invalid method" });
}
const { feedback, rating, scope, address } = req.body as FeedbackPayload;
let _rating = rating;
// rating cannot be > 5
if (_rating > 5) {
_rating = 5;
}
// rating cannot be below 1
if (_rating < 1) {
_rating = 1;
}
const payload = {
embeds: [
{
title: "New Product Feedback",
timestamp: new Date(),
author: {
name: shortenAddress(address),
url: `https://thirdweb.com/${address}`,
},
fields: [
{ name: "Product Scope", value: scope, inline: true },
{
name: "Rating",
value: `${Array(_rating).fill("⭐️").join("")}`,
inline: true,
},
{ name: "Feedback", value: `\`\`\`${feedback || "N/A"}\`\`\`` },
],
color: 5793266,
},
],
};
invariant(process.env.FEEDBACK_WEBHOOK_URL, "missing FEEDBACK_WEBHOOK URL");
const response = await fetch(process.env.FEEDBACK_WEBHOOK_URL, {
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify(payload),
});
return res.status(response.status).json({ status: response.statusText });
};