-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf8f813
commit 7367df7
Showing
12 changed files
with
1,331 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { fetchRequestHandler } from "@trpc/server/adapters/fetch"; | ||
import { type NextRequest } from "next/server"; | ||
|
||
import { env } from "@/env.mjs"; | ||
import { appRouter } from "@/server/api/root"; | ||
import { createTRPCContext } from "@/server/api/trpc"; | ||
|
||
/** | ||
* This wraps the `createTRPCContext` helper and provides the required context for the tRPC API when | ||
* handling a HTTP request (e.g. when you make requests from Client Components). | ||
*/ | ||
const createContext = async (req: NextRequest) => { | ||
return createTRPCContext({ | ||
headers: req.headers, | ||
}); | ||
}; | ||
|
||
const handler = (req: NextRequest) => | ||
fetchRequestHandler({ | ||
endpoint: "/api/trpc", | ||
req, | ||
router: appRouter, | ||
createContext: () => createContext(req), | ||
onError: | ||
env.NODE_ENV === "development" | ||
? ({ path, error }) => { | ||
console.error(`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`); | ||
} | ||
: undefined, | ||
}); | ||
|
||
export { handler as GET, handler as POST }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"use client"; | ||
import { useState } from "react"; | ||
import { Input } from "@/components/shadcn/ui/input"; | ||
import { Label } from "@/components/shadcn/ui/label"; | ||
import { Textarea } from "@/components/shadcn/ui/textarea"; | ||
import { Button } from "@/components/shadcn/ui/button"; | ||
import { api } from "@/trpc/react"; | ||
import { toast } from "sonner"; | ||
|
||
export default function Page() { | ||
const [title, setTitle] = useState(""); | ||
const [description, setDescription] = useState(""); | ||
|
||
const createTicket = api.tickets.create.useMutation(); | ||
|
||
async function runCreateTicket() { | ||
if (title.length > 3 && description.length > 10) { | ||
const result = await createTicket.mutateAsync({ title, description }); | ||
if (result.success) { | ||
toast.success("Ticket created successfully!"); | ||
console.log( | ||
"created ticket with ID " + | ||
result.ticketID + | ||
" and chat with ID " + | ||
result.chatID | ||
); | ||
} | ||
} else { | ||
toast.error("Your title or description is too short! Please try again."); | ||
} | ||
} | ||
|
||
return ( | ||
<div className="h-full pt-20"> | ||
<div className="mx-auto max-w-3xl"> | ||
<h1 className="font-black text-3xl">New Ticket</h1> | ||
<div className="flex items-start flex-col gap-y-5 pt-10"> | ||
<div className="w-full"> | ||
<Label className="pb-2">Title</Label> | ||
<Input onChange={(e) => setTitle(e.target.value)} /> | ||
</div> | ||
<div className="w-full"> | ||
<Label className="mb-1">Description</Label> | ||
<Textarea onChange={(e) => setDescription(e.target.value)} /> | ||
</div> | ||
<Button onClick={() => runCreateTicket()}>Create Ticket</Button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { z } from "zod"; | ||
|
||
import { createTRPCRouter, authedProcedure } from "@/server/api/trpc"; | ||
import { chats, tickets } from "db/schema"; | ||
import { nanoid } from "nanoid"; | ||
|
||
export const ticketsRouter = createTRPCRouter({ | ||
// hello: publicProcedure.input(z.object({ text: z.string() })).query(({ input }) => { | ||
// return { | ||
// greeting: `Hello ${input.text}`, | ||
// }; | ||
// }), | ||
// create: publicProcedure | ||
// .input(z.object({ name: z.string().min(1) })) | ||
// .mutation(async ({ ctx, input }) => { | ||
// // simulate a slow db call | ||
// await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
// // await ctx.db.insert(posts).values({ | ||
// // name: input.name, | ||
// // }); | ||
// }), | ||
// getLatest: publicProcedure.query(({ ctx }) => { | ||
// // return ctx.db.query.posts.findFirst({ | ||
// // orderBy: (posts, { desc }) => [desc(posts.createdAt)], | ||
// // }); | ||
// return null; | ||
// }), | ||
|
||
// TODO: needs error handling | ||
create: authedProcedure | ||
.input(z.object({ title: z.string().min(1), description: z.string().min(1) })) | ||
.mutation(async ({ ctx, input }) => { | ||
const ticketID = nanoid(); | ||
|
||
const ticket = await ctx.db.insert(tickets).values({ | ||
id: ticketID, | ||
title: input.title, | ||
description: input.description, | ||
status: "awaiting", | ||
}); | ||
|
||
const chatID = nanoid(); | ||
|
||
const chat = await ctx.db.insert(chats).values({ | ||
id: chatID, | ||
type: "ticket", | ||
ticketID: ticketID, | ||
author: ctx.userId, | ||
createdAt: new Date(), | ||
}); | ||
|
||
return { | ||
success: true, | ||
ticketID: ticketID, | ||
chatID: chatID, | ||
}; | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
CREATE TABLE IF NOT EXISTS "chats_to_users" ( | ||
"chat_id" text NOT NULL, | ||
"user_id" text NOT NULL, | ||
CONSTRAINT "chats_to_users_user_id_chat_id_pk" PRIMARY KEY("user_id","chat_id") | ||
); | ||
--> statement-breakpoint | ||
CREATE TABLE IF NOT EXISTS "tickets_to_users" ( | ||
"ticket_id" text NOT NULL, | ||
"user_id" text NOT NULL, | ||
CONSTRAINT "tickets_to_users_user_id_ticket_id_pk" PRIMARY KEY("user_id","ticket_id") | ||
); | ||
--> statement-breakpoint | ||
ALTER TABLE "chat_messages" RENAME COLUMN "author" TO "author_id";--> statement-breakpoint | ||
DO $$ BEGIN | ||
ALTER TABLE "chats_to_users" ADD CONSTRAINT "chats_to_users_chat_id_chats_id_fk" FOREIGN KEY ("chat_id") REFERENCES "public"."chats"("id") ON DELETE no action ON UPDATE no action; | ||
EXCEPTION | ||
WHEN duplicate_object THEN null; | ||
END $$; | ||
--> statement-breakpoint | ||
DO $$ BEGIN | ||
ALTER TABLE "chats_to_users" ADD CONSTRAINT "chats_to_users_user_id_users_clerk_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("clerk_id") ON DELETE no action ON UPDATE no action; | ||
EXCEPTION | ||
WHEN duplicate_object THEN null; | ||
END $$; | ||
--> statement-breakpoint | ||
DO $$ BEGIN | ||
ALTER TABLE "tickets_to_users" ADD CONSTRAINT "tickets_to_users_ticket_id_tickets_id_fk" FOREIGN KEY ("ticket_id") REFERENCES "public"."tickets"("id") ON DELETE no action ON UPDATE no action; | ||
EXCEPTION | ||
WHEN duplicate_object THEN null; | ||
END $$; | ||
--> statement-breakpoint | ||
DO $$ BEGIN | ||
ALTER TABLE "tickets_to_users" ADD CONSTRAINT "tickets_to_users_user_id_users_clerk_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("clerk_id") ON DELETE no action ON UPDATE no action; | ||
EXCEPTION | ||
WHEN duplicate_object THEN null; | ||
END $$; |
Oops, something went wrong.