Skip to content

Commit

Permalink
Merge pull request #5 from abhishekHegde2000/test-branch
Browse files Browse the repository at this point in the history
Test branch
  • Loading branch information
abhishekHegde2000 authored Nov 23, 2023
2 parents 79fdc0d + 0df2686 commit f368335
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/app/api/notes/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import prisma from "@/lib/db/prisma";
import { createNoteSchema } from "@/lib/validation/note";
import { auth } from "@clerk/nextjs";

export async function POST(req: Request) {
try {
const body = await req.json();

const parseResult = createNoteSchema.safeParse(body);

if (!parseResult.success) {
console.error(parseResult.error);
return Response.json({ error: "Invalid input" }, { status: 400 });
}

const { title, content } = parseResult.data;

const { userId } = auth();

if (!userId) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}

const note = await prisma.note.create({
data: {
title,
content,
userId,
},
});

return Response.json({ note }, { status: 201 });
} catch (error) {
console.error(error);
return Response.json({ error: "Internal server error" }, { status: 500 });
}
}
103 changes: 103 additions & 0 deletions src/components/AddNoteDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { CreateNoteSchema, createNoteSchema } from "@/lib/validation/note";
import { zodResolver } from "@hookform/resolvers/zod";
import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from "./ui/dialog";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "./ui/form";
import { Input } from "./ui/input";
import LoadingButton from "./ui/loading-button";
import { Textarea } from "./ui/textarea";

interface AddNoteDialogProps {
open: boolean;
setOpen: (open: boolean) => void;
}

export default function AddNoteDialog({ open, setOpen }: AddNoteDialogProps) {
const router = useRouter();

const form = useForm<CreateNoteSchema>({
resolver: zodResolver(createNoteSchema),
defaultValues: {
title: "",
content: "",
},
});

async function onSubmit(input: CreateNoteSchema) {
try {
const response = await fetch("/api/notes", {
method: "POST",
body: JSON.stringify(input),
});
if (!response.ok) throw Error("Status code: " + response.status);
form.reset();
router.refresh();
setOpen(false);
} catch (error) {
console.error(error);
alert("Something went wrong. Please try again.");
}
}

return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Add Note</DialogTitle>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-3">
<FormField
control={form.control}
name="title"
render={({ field }) => (
<FormItem>
<FormLabel>Note title</FormLabel>
<FormControl>
<Input placeholder="Note title" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="content"
render={({ field }) => (
<FormItem>
<FormLabel>Note content</FormLabel>
<FormControl>
<Textarea placeholder="Note content" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<DialogFooter>
<LoadingButton
type="submit"
loading={form.formState.isSubmitting}
>
Submit
</LoadingButton>
</DialogFooter>
</form>
</Form>
</DialogContent>
</Dialog>
);
}
8 changes: 8 additions & 0 deletions src/lib/validation/note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from "zod";

export const createNoteSchema = z.object({
title: z.string().min(1, { message: "Title is required" }),
content: z.string().optional(),
});

export type CreateNoteSchema = z.infer<typeof createNoteSchema>;

0 comments on commit f368335

Please sign in to comment.