-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from abhishekHegde2000/test-branch
Test branch
- Loading branch information
Showing
3 changed files
with
148 additions
and
0 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,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 }); | ||
} | ||
} |
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,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> | ||
); | ||
} |
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,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>; |