diff --git a/src/app/ThemeProvider.tsx b/src/app/ThemeProvider.tsx
new file mode 100644
index 0000000..e39e3a5
--- /dev/null
+++ b/src/app/ThemeProvider.tsx
@@ -0,0 +1,3 @@
+"use client";
+
+export { ThemeProvider } from "next-themes";
diff --git a/src/app/api/notes/route.ts b/src/app/api/notes/route.ts
index 06cbee3..260b717 100644
--- a/src/app/api/notes/route.ts
+++ b/src/app/api/notes/route.ts
@@ -1,5 +1,9 @@
import prisma from "@/lib/db/prisma";
-import { createNoteSchema } from "@/lib/validation/note";
+import {
+ createNoteSchema,
+ deleteNoteSchema,
+ updateNoteSchema,
+} from "@/lib/validation/note";
import { auth } from "@clerk/nextjs";
export async function POST(req: Request) {
@@ -35,3 +39,77 @@ export async function POST(req: Request) {
return Response.json({ error: "Internal server error" }, { status: 500 });
}
}
+
+export async function PUT(req: Request) {
+ try {
+ const body = await req.json();
+
+ const parseResult = updateNoteSchema.safeParse(body);
+
+ if (!parseResult.success) {
+ console.error(parseResult.error);
+ return Response.json({ error: "Invalid input" }, { status: 400 });
+ }
+
+ const { id, title, content } = parseResult.data;
+
+ const note = await prisma.note.findUnique({ where: { id } });
+
+ if (!note) {
+ return Response.json({ error: "Note not found" }, { status: 404 });
+ }
+
+ const { userId } = auth();
+
+ if (!userId || userId !== note.userId) {
+ return Response.json({ error: "Unauthorized" }, { status: 401 });
+ }
+
+ const updatedNote = await prisma.note.update({
+ where: { id },
+ data: {
+ title,
+ content,
+ },
+ });
+
+ return Response.json({ updatedNote }, { status: 200 });
+ } catch (error) {
+ console.error(error);
+ return Response.json({ error: "Internal server error" }, { status: 500 });
+ }
+}
+
+export async function DELETE(req: Request) {
+ try {
+ const body = await req.json();
+
+ const parseResult = deleteNoteSchema.safeParse(body);
+
+ if (!parseResult.success) {
+ console.error(parseResult.error);
+ return Response.json({ error: "Invalid input" }, { status: 400 });
+ }
+
+ const { id } = parseResult.data;
+
+ const note = await prisma.note.findUnique({ where: { id } });
+
+ if (!note) {
+ return Response.json({ error: "Note not found" }, { status: 404 });
+ }
+
+ const { userId } = auth();
+
+ if (!userId || userId !== note.userId) {
+ return Response.json({ error: "Unauthorized" }, { status: 401 });
+ }
+
+ await prisma.note.delete({ where: { id } });
+
+ return Response.json({ message: "Note deleted" }, { status: 200 });
+ } catch (error) {
+ console.error(error);
+ return Response.json({ error: "Internal server error" }, { status: 500 });
+ }
+}
diff --git a/src/app/layout.tsx b/src/app/layout.tsx
index bbc7aac..b534e29 100644
--- a/src/app/layout.tsx
+++ b/src/app/layout.tsx
@@ -1,6 +1,7 @@
import { ClerkProvider } from "@clerk/nextjs";
import type { Metadata } from "next";
import { Inter } from "next/font/google";
+import { ThemeProvider } from "./ThemeProvider";
import "./globals.css";
const inter = Inter({ subsets: ["latin"] });
@@ -18,7 +19,9 @@ export default function RootLayout({
return (
- {children}
+
+ {children}
+
);
diff --git a/src/app/notes/NavBar.tsx b/src/app/notes/NavBar.tsx
index 4d3181f..16719ac 100644
--- a/src/app/notes/NavBar.tsx
+++ b/src/app/notes/NavBar.tsx
@@ -1,16 +1,21 @@
"use client";
import logo from "@/assets/logo.png";
-import AddNoteDialog from "@/components/AddNoteDialog";
+import AddEditNoteDialog from "@/components/AddEditNoteDialog";
+import ThemeToggleButton from "@/components/ThemeToggleButton";
import { Button } from "@/components/ui/button";
import { UserButton } from "@clerk/nextjs";
+import { dark } from "@clerk/themes";
import { Plus } from "lucide-react";
+import { useTheme } from "next-themes";
import Image from "next/image";
import Link from "next/link";
import { useState } from "react";
export default function NavBar() {
- const [showAddNoteDialog, setShowAddNoteDialog] = useState(false);
+ const { theme } = useTheme();
+
+ const [showAddEditNoteDialog, setShowAddEditNoteDialog] = useState(false);
return (
<>
@@ -24,17 +29,22 @@ export default function NavBar() {
-