From 5fb3b036ca2410cbcb040ee187dd67f994fc7ff5 Mon Sep 17 00:00:00 2001 From: Fergal Moran Date: Mon, 7 Nov 2022 19:40:26 +0000 Subject: [PATCH] Bring share page back to server components --- app/gifs/[slug]/page.tsx | 43 +++++++++++++++------------------------ app/share/[slug]/page.tsx | 33 ++++++++++++++++++++++++++++++ pages/api/gifs/[slug].ts | 26 +++++++++++++++++++++++ 3 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 app/share/[slug]/page.tsx create mode 100644 pages/api/gifs/[slug].ts diff --git a/app/gifs/[slug]/page.tsx b/app/gifs/[slug]/page.tsx index 5cbccf0..f6e64a7 100644 --- a/app/gifs/[slug]/page.tsx +++ b/app/gifs/[slug]/page.tsx @@ -1,37 +1,25 @@ import React from 'react'; -import { Gif } from '@models'; -import { GifContainer, SharingComponent } from '@components'; +import {Gif} from '@models'; +import {GifContainer, SharingComponent} from '@components'; import client from '@lib/prismadb'; -import { mapGif } from '@lib/mapping/gif'; -import { notFound, useRouter } from 'next/navigation'; +import {mapGif} from '@lib/mapping/gif'; +import {notFound, useRouter} from 'next/navigation'; interface IGifPageProps { params: { slug: string; }; } -const getGif = async (slug: string): Promise => { - const gif = await client.gif.findUnique({ - where: { - slug, - }, - include: { - _count: { - select: { - upVotes: true, - downVotes: true, - }, - }, - }, - }); - if (!gif) { - notFound(); - } - return mapGif(gif); -}; -const GifPage = async ({ params }: IGifPageProps) => { - const { slug } = params; +const getGif = async (slug: string): Promise => { + const res = await fetch(`${process.env.API_URL}/api/gifs/${slug}`); + if (res.status === 200) { + return await res.json(); + } + notFound(); +}; +const GifPage = async ({params}: IGifPageProps) => { + const {slug} = params; const gif = await getGif(slug as string); return (
@@ -47,12 +35,13 @@ const GifPage = async ({ params }: IGifPageProps) => {

{gif.description}

- + {/*
*/}
-
+
The gif => { + const res = await fetch(`${process.env.API_URL}/api/gifs/${slug}`); + if (res.status === 200) { + return await res.json(); + } + notFound(); +}; +const ShareGifPage = async ({params}: IShareGifPageProps) => { + const {slug} = params; + const gif = await getGif(slug as string); + return ( +
+ +
+ ); +}; + +export default ShareGifPage; diff --git a/pages/api/gifs/[slug].ts b/pages/api/gifs/[slug].ts new file mode 100644 index 0000000..8a9143c --- /dev/null +++ b/pages/api/gifs/[slug].ts @@ -0,0 +1,26 @@ +import { NextApiRequest, NextApiResponse } from 'next'; +import { mapGif } from '@lib/mapping/gif'; +import client from '@lib/prismadb'; +const handler = async (req: NextApiRequest, res: NextApiResponse) => { + const { slug } = req.query; + const gif = await client.gif.findUnique({ + where: { + slug, + }, + include: { + _count: { + select: { + upVotes: true, + downVotes: true, + }, + }, + }, + }); + if (!gif) { + return { + notFound: true, + }; + } + return res.status(200).json(mapGif(gif)); +}; +export default handler;