From 0dcd361e1270c1b4d2e3babe44d0f064cf17c21e Mon Sep 17 00:00:00 2001 From: Divyam Date: Tue, 3 Oct 2023 13:46:20 +0530 Subject: [PATCH] refactor index.tsx and [id].tsx --- pages/[id].tsx | 16 +++++++--- pages/api/get/[id].ts | 68 ++++++++++++------------------------------- 2 files changed, 31 insertions(+), 53 deletions(-) diff --git a/pages/[id].tsx b/pages/[id].tsx index 6890d62..f62f706 100644 --- a/pages/[id].tsx +++ b/pages/[id].tsx @@ -9,9 +9,10 @@ import 'highlight.js/styles/atom-one-dark.css'; import NoteAdd from '@material-ui/icons/NoteAdd' import { GetServerSidePropsContext } from 'next' +import { getData } from './api/get/[id]' -const Viewer = ({ code }: {code: string}) => { +const Viewer = ({ code }: { code: string }) => { const codeRef = createRef(); const router = useRouter() @@ -58,10 +59,17 @@ const Viewer = ({ code }: {code: string}) => { export const getServerSideProps = async (context: GetServerSidePropsContext) => { const id = context.params?.id - // const setCode = (data) => {}; + if (!id) + return { + redirect: { + destination: '/', + permanent: false, + } + } + + let data = (await getData(id.toString())).data - const data = await fetch(`/api/get/${id}`).then(res => res.json()) - if (!data.code) { + if (!data) { return { redirect: { destination: '/', diff --git a/pages/api/get/[id].ts b/pages/api/get/[id].ts index b6d9a7c..aa63b0a 100644 --- a/pages/api/get/[id].ts +++ b/pages/api/get/[id].ts @@ -1,63 +1,33 @@ -import type { NextApiRequest, NextApiResponse } from 'next' -import faunadb, { Collection, Get, Ref, Time } from 'faunadb' +import type { NextApiRequest, NextApiResponse } from "next"; +import faunadb, { Collection, Get, Ref, Time } from "faunadb"; type Data = { - code: string -} + code: string; +}; type FaunaQueryResponse = { - ref?: typeof Ref - ts?: typeof Time - data?: Data -} + ref?: typeof Ref; + ts?: typeof Time; + data?: Data; +}; const client = new faunadb.Client({ secret: process.env.FAUNA_ADMIN_KEY || "", - domain: 'db.fauna.com', + domain: "db.fauna.com", port: 443, - scheme: 'https' -}) + scheme: "https", +}); export default async function handler( req: NextApiRequest, res: NextApiResponse ) { - const id = req.query['id'] - - client.query( - Get(Ref(Collection('data'), id)) - ) - .then((ret) => res.status(200).json({code: ret?.data?.code || ""})) - .catch((error) => { - res.status(404).send({code: ""}) - }) - - - /* // MongoDB - let data = JSON.stringify({ - collection: "data", - database: "fastbin", - dataSource: "Cluster0", - filter: { - "_id": { "$oid": id} - }, - }) - - fetch("https://data.mongodb-api.com/app/data-gizgg/endpoint/data/beta/action/findOne", { - method: 'POST', - headers: { - "Content-Type": "application/json", - "Access-Control-Request-Headers": "*", - "api-key": process.env.MONGO_API_KEY || "", - }, - body: data - }) - .then((response) => response.json()) - .then((data) => { - res.status(200).json({code: data.document.code}) - }) - .catch((error) => { - res.status(404).send({code: ""}) - }) - */ + const id = req.query["id"]; + getData(id.toString()) + .then((ret) => res.status(200).json({ code: ret?.data?.code || "" })) + .catch(() => res.status(404).send({ code: "" })); +} + +export async function getData(id: string) { + return client.query(Get(Ref(Collection("data"), id))); }