refactor index.tsx and [id].tsx

This commit is contained in:
Divyam 2023-10-03 13:46:20 +05:30
parent a99be0c1d8
commit 0dcd361e12
2 changed files with 31 additions and 53 deletions

View file

@ -9,9 +9,10 @@ import 'highlight.js/styles/atom-one-dark.css';
import NoteAdd from '@material-ui/icons/NoteAdd' import NoteAdd from '@material-ui/icons/NoteAdd'
import { GetServerSidePropsContext } from 'next' import { GetServerSidePropsContext } from 'next'
import { getData } from './api/get/[id]'
const Viewer = ({ code }: {code: string}) => { const Viewer = ({ code }: { code: string }) => {
const codeRef = createRef<HTMLTextAreaElement>(); const codeRef = createRef<HTMLTextAreaElement>();
const router = useRouter() const router = useRouter()
@ -58,10 +59,17 @@ const Viewer = ({ code }: {code: string}) => {
export const getServerSideProps = async (context: GetServerSidePropsContext) => { export const getServerSideProps = async (context: GetServerSidePropsContext) => {
const id = context.params?.id const id = context.params?.id
// const setCode = (data) => {}; if (!id)
return {
redirect: {
destination: '/',
permanent: false,
}
}
const data = await fetch(`/api/get/${id}`).then(res => res.json()) let data = (await getData(id.toString())).data
if (!data.code) {
if (!data) {
return { return {
redirect: { redirect: {
destination: '/', destination: '/',

View file

@ -1,63 +1,33 @@
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from "next";
import faunadb, { Collection, Get, Ref, Time } from 'faunadb' import faunadb, { Collection, Get, Ref, Time } from "faunadb";
type Data = { type Data = {
code: string code: string;
} };
type FaunaQueryResponse = { type FaunaQueryResponse = {
ref?: typeof Ref ref?: typeof Ref;
ts?: typeof Time ts?: typeof Time;
data?: Data data?: Data;
} };
const client = new faunadb.Client({ const client = new faunadb.Client({
secret: process.env.FAUNA_ADMIN_KEY || "", secret: process.env.FAUNA_ADMIN_KEY || "",
domain: 'db.fauna.com', domain: "db.fauna.com",
port: 443, port: 443,
scheme: 'https' scheme: "https",
}) });
export default async function handler( export default async function handler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse<Data> res: NextApiResponse<Data>
) { ) {
const id = req.query['id'] const id = req.query["id"];
getData(id.toString())
client.query<FaunaQueryResponse>( .then((ret) => res.status(200).json({ code: ret?.data?.code || "" }))
Get(Ref(Collection('data'), id)) .catch(() => res.status(404).send({ code: "" }));
) }
.then((ret) => res.status(200).json({code: ret?.data?.code || ""}))
.catch((error) => { export async function getData(id: string) {
res.status(404).send({code: ""}) return client.query<FaunaQueryResponse>(Get(Ref(Collection("data"), id)));
})
/* // 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: ""})
})
*/
} }