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,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<HTMLTextAreaElement>();
|
||||
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: '/',
|
||||
|
|
|
|||
|
|
@ -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<Data>
|
||||
) {
|
||||
const id = req.query['id']
|
||||
|
||||
client.query<FaunaQueryResponse>(
|
||||
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<FaunaQueryResponse>(Get(Ref(Collection("data"), id)));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue