Use tRPC and react-hook-form

This commit is contained in:
Timothy Armes
2023-09-11 10:28:02 +02:00
parent 7111f000e5
commit ac633670ab
42 changed files with 5490 additions and 587 deletions
-54
View File
@@ -1,54 +0,0 @@
import { NextResponse } from "next/server";
import clientPromise from "@/lib/mongodb";
import { errorLog } from "@/utils/logger";
export async function POST(req: Request) {
const {
address,
town,
department,
tournament,
url,
time_control,
norm_tournament,
date,
coordinates,
country,
} = await req.json();
try {
const client = await clientPromise;
const db = client.db("tournamentsFranceDB").collection("tournaments");
const tournamentData = {
address,
town,
department,
tournament,
url,
time_control,
norm_tournament,
date,
coordinates,
country,
entry_method: "manual",
pending: true,
};
const result = await db.insertOne(tournamentData);
if (result.insertedId) {
return new NextResponse(
JSON.stringify({ success: "Tournament added to DB as pending" }),
{ status: 201, headers: { "Content-Type": "application/json" } },
);
}
} catch (error) {
errorLog(error);
return new NextResponse(
JSON.stringify({ error: "Failed to insert tournament data" }),
{ status: 500, headers: { "Content-Type": "application/json" } },
);
}
}
@@ -1,34 +0,0 @@
import { NextRequest, NextResponse } from "next/server";
export async function POST(req: NextRequest) {
const webhookURL = process.env.DISCORD_WEBHOOK_URL;
const { embeds } = await req.json();
if (!webhookURL) {
return NextResponse.json(
{ error: `Discord webhook URL not found` },
{ status: 404 },
);
}
try {
const webhookBody = {
embeds: embeds,
};
const webhookInfo = await fetch(webhookURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(webhookBody),
});
return NextResponse.json(
{ success: `Message delivered to ${webhookInfo}` },
{ status: 200 },
);
} catch (error) {
return NextResponse.json({ error: `Connection refused` }, { status: 404 });
}
}
+21
View File
@@ -0,0 +1,21 @@
import {
FetchCreateContextFnOptions,
fetchRequestHandler,
} from "@trpc/server/adapters/fetch";
import { appRouter } from "@/app/server/appRouter";
const handler = (request: Request) => {
return fetchRequestHandler({
endpoint: "/api/trpc",
req: request,
router: appRouter,
createContext: function (
opts: FetchCreateContextFnOptions,
): object | Promise<object> {
return {};
},
});
};
export { handler as GET, handler as POST };