refactor code

This commit is contained in:
Divyam 2023-10-03 17:56:09 +05:30
parent 43f115fa7e
commit 2003888f2b
4 changed files with 48 additions and 63 deletions

10
lib/fauna-client.ts Normal file
View file

@ -0,0 +1,10 @@
import faunadb from "faunadb";
const client = new faunadb.Client({
secret: process.env.FAUNA_ADMIN_KEY || "",
domain: "db.fauna.com",
port: 443,
scheme: "https",
});
export default client;

View file

@ -1,5 +1,6 @@
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import faunadb, { Collection, Get, Ref, Time } from "faunadb"; import { Collection, Get, Ref, Time } from "faunadb";
import client from "../../../lib/fauna-client";
type Data = { type Data = {
code: string; code: string;
@ -11,13 +12,6 @@ type FaunaQueryResponse = {
data?: Data; data?: Data;
}; };
const client = new faunadb.Client({
secret: process.env.FAUNA_ADMIN_KEY || "",
domain: "db.fauna.com",
port: 443,
scheme: "https",
});
export default async function handler( export default async function handler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse<Data> res: NextApiResponse<Data>

View file

@ -1,64 +1,27 @@
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import faunadb from 'faunadb' import faunadb from "faunadb";
import client from "../../lib/fauna-client";
let q = faunadb.query const q = faunadb.query;
type Data = { type Data = {
id: string; id: string;
}; };
const client = new faunadb.Client({
secret: process.env.FAUNA_ADMIN_KEY || "",
domain: 'db.fauna.com',
port: 443,
scheme: 'https'
})
export default async function handler( export default async function handler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse<Data> res: NextApiResponse<Data>
) { ) {
if (req.method !== 'POST') res.status(404).send({id: ""}); if (req.method !== "POST") res.status(404).send({ id: "" });
const code = req.body.data; const code = req.body.data;
client.query<any>( client
q.Create( .query<any>(q.Create(q.Collection("data"), { data: { code: code } }))
q.Collection('data'), .then((response) => {
{ data: { 'code': code } } res.status(200).json({ id: response?.ref?.id });
) })
).then((response) => { .catch((_error) => {
res.status(200).json({id: response?.ref?.id}) res.status(404).send({ id: "" });
}).catch((error) => { });
res.status(404).send({id: ""})
})
/* // Mongodb
let data = JSON.stringify({
collection: "data",
database: "fastbin",
dataSource: "Cluster0",
document: {
"code": code
},
})
fetch("https://data.mongodb-api.com/app/data-gizgg/endpoint/data/beta/action/insertOne", {
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({id: data['insertedId']})
})
.catch((error) => {
res.status(404).send({id: ""})
})
*/
} }

View file

@ -1,4 +1,4 @@
import { useCallback, useEffect, useRef, useState } from 'react' import { KeyboardEventHandler, useCallback, useEffect, useRef, useState } from 'react'
import type { NextPage } from 'next' import type { NextPage } from 'next'
import Head from 'next/head' import Head from 'next/head'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
@ -32,7 +32,7 @@ const Home: NextPage = () => {
}, [router]) }, [router])
useEffect(() => { useEffect(() => {
const listener = (event : KeyboardEvent) => { const listener = (event: KeyboardEvent) => {
if (event.code === "KeyS" && event.ctrlKey === true) { if (event.code === "KeyS" && event.ctrlKey === true) {
event.preventDefault() event.preventDefault()
save() save()
@ -50,6 +50,23 @@ const Home: NextPage = () => {
} }
}, [save, router]) }, [save, router])
const keyDownHandler: KeyboardEventHandler<HTMLTextAreaElement> = (e) => {
if (e.key === "Tab") {
e.preventDefault()
e.currentTarget.setRangeText(
'\t',
e.currentTarget.selectionStart,
e.currentTarget.selectionStart,
'end'
)
}
}
useEffect(() => {
if (codeRef.current)
codeRef.current.focus();
}, [codeRef])
return ( return (
<div className={styles.container}> <div className={styles.container}>
<Head> <Head>
@ -81,14 +98,15 @@ const Home: NextPage = () => {
{">"} {">"}
</span> </span>
<textarea <textarea
autoFocus onKeyDown={keyDownHandler}
spellCheck={false}
wrap="off" wrap="off"
ref={codeRef} ref={codeRef}
placeholder={"Type Someting Here...\nCtrl + S to Save Document\nShift + N for New Document\n:)"} placeholder={"Type Someting Here...\nCtrl + S to Save Document\nShift + N for New Document\n:)"}
className={styles["code-editor"]}> className={styles["code-editor"]}>
</textarea> </textarea>
</div> </div>
<Snackbar open={uploading}><div className={styles.toast}>Uploading document...</div></Snackbar> <Snackbar open={uploading}><div className={styles.toast}>Uploading Document ...</div></Snackbar>
</div> </div>
) )
} }