refactor index.tsx and [id].tsx
This commit is contained in:
parent
a99be0c1d8
commit
0dcd361e12
2 changed files with 31 additions and 53 deletions
|
|
@ -9,6 +9,7 @@ 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 }) => {
|
||||||
|
|
@ -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: '/',
|
||||||
|
|
|
||||||
|
|
@ -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>(
|
|
||||||
Get(Ref(Collection('data'), id))
|
|
||||||
)
|
|
||||||
.then((ret) => res.status(200).json({ code: ret?.data?.code || "" }))
|
.then((ret) => res.status(200).json({ code: ret?.data?.code || "" }))
|
||||||
.catch((error) => {
|
.catch(() => res.status(404).send({ code: "" }));
|
||||||
res.status(404).send({code: ""})
|
}
|
||||||
})
|
|
||||||
|
export async function getData(id: string) {
|
||||||
|
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: ""})
|
|
||||||
})
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue