From 44643a49f9358553ec3f836502efb6956a1f5ee0 Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung Date: Fri, 20 May 2022 15:00:20 -0400 Subject: [PATCH] Post a review --- apps/web/components/App.tsx | 32 +++++++++++++++++-------- apps/web/pages/api/app-store/reviews.ts | 31 ++++++++++++++++++++++++ apps/web/pages/apps/[slug]/index.tsx | 1 + packages/prisma/schema.prisma | 3 ++- 4 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 apps/web/pages/api/app-store/reviews.ts diff --git a/apps/web/components/App.tsx b/apps/web/components/App.tsx index d9baebc956..b6374e52a1 100644 --- a/apps/web/components/App.tsx +++ b/apps/web/components/App.tsx @@ -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"> - + diff --git a/apps/web/pages/api/app-store/reviews.ts b/apps/web/pages/api/app-store/reviews.ts new file mode 100644 index 0000000000..a6386f0dbe --- /dev/null +++ b/apps/web/pages/api/app-store/reviews.ts @@ -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(); +} diff --git a/apps/web/pages/apps/[slug]/index.tsx b/apps/web/pages/apps/[slug]/index.tsx index cfc20626a0..fbaf9b9bdc 100644 --- a/apps/web/pages/apps/[slug]/index.tsx +++ b/apps/web/pages/apps/[slug]/index.tsx @@ -61,6 +61,7 @@ function SingleAppPage({ data, source }: inferSSRProps) { docs={data.docsUrl} website={data.url} email={data.email} + slug={data.slug} // tos="https://zoom.us/terms" // privacy="https://zoom.us/privacy" body={} diff --git a/packages/prisma/schema.prisma b/packages/prisma/schema.prisma index 8c9fcdadde..4352cfd249 100644 --- a/packages/prisma/schema.prisma +++ b/packages/prisma/schema.prisma @@ -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? }