Post a review

This commit is contained in:
Joe Au-Yeung 2022-05-20 15:00:20 -04:00
parent 81c84ee5ed
commit 44643a49f9
4 changed files with 56 additions and 11 deletions

View File

@ -37,6 +37,7 @@ export default function App({
email,
tos,
privacy,
slug,
}: {
name: string;
type: AppType["type"];
@ -54,6 +55,7 @@ export default function App({
email: string; // required
tos?: string;
privacy?: string;
slug: string;
}) {
const { t } = useLocale();
@ -62,16 +64,26 @@ export default function App({
const [comment, setComment] = useState("");
const totalStars = 5;
// const onSubmitReview = async (rating: number, comment: string) => {
// try {
// const body = {
// rating: rating,
// comment: comment
// }
const onSubmitReview = async (slug: string, rating: number, comment: string) => {
try {
const body = {
slug: slug,
rating: rating,
comment: comment,
};
// const
// }
// }
const res = await fetch("/api/app-store/reviews", {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
},
});
console.log("🚀 ~ file: App.tsx ~ line 82 ~ onSubmitReview ~ res", res);
} catch (error) {
console.log("🚀 ~ file: App.tsx ~ line 82 ~ onSubmitReview ~ error", error);
}
};
const priceInDollar = Intl.NumberFormat("en-US", {
style: "currency",
@ -283,7 +295,7 @@ export default function App({
rows={3}
onChange={(event) => setComment(event.target.value)}
className="my-1 block rounded-sm border-gray-300 py-2 pb-2 shadow-sm sm:text-sm"></textarea>
<Button>{t("submit")}</Button>
<Button onClick={() => onSubmitReview(slug, rating, comment)}>{t("submit")}</Button>
</div>
</div>
</Shell>

View File

@ -0,0 +1,31 @@
import dayjs from "dayjs";
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { getSession } from "@lib/auth";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
console.log("This triggers ");
if (req.method === "POST") {
const session = await getSession({ req });
if (!session?.user?.id) {
return res.status(401).json({ message: "Not authenticated" });
}
await prisma.appReview.create({
data: {
slug: req.body.slug,
date: dayjs().toISOString(),
user: {
connect: {
id: session.user.id,
},
},
rating: req.body.rating,
comment: req.body.comment,
},
});
}
res.end();
}

View File

@ -61,6 +61,7 @@ function SingleAppPage({ data, source }: inferSSRProps<typeof getStaticProps>) {
docs={data.docsUrl}
website={data.url}
email={data.email}
slug={data.slug}
// tos="https://zoom.us/terms"
// privacy="https://zoom.us/privacy"
body={<MDXRemote {...source} components={components} />}

View File

@ -483,6 +483,7 @@ model AppReview {
slug String @unique
userId Int
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
date DateTime
rating Int
review String?
comment String?
}