diff --git a/app/models/note.server.ts b/app/models/note.server.ts deleted file mode 100644 index f385491..0000000 --- a/app/models/note.server.ts +++ /dev/null @@ -1,52 +0,0 @@ -import type { User, Note } from "@prisma/client"; - -import { prisma } from "~/db.server"; - -export function getNote({ - id, - userId, -}: Pick & { - userId: User["id"]; -}) { - return prisma.note.findFirst({ - select: { id: true, body: true, title: true }, - where: { id, userId }, - }); -} - -export function getNoteListItems({ userId }: { userId: User["id"] }) { - return prisma.note.findMany({ - where: { userId }, - select: { id: true, title: true }, - orderBy: { updatedAt: "desc" }, - }); -} - -export function createNote({ - body, - title, - userId, -}: Pick & { - userId: User["id"]; -}) { - return prisma.note.create({ - data: { - title, - body, - user: { - connect: { - id: userId, - }, - }, - }, - }); -} - -export function deleteNote({ - id, - userId, -}: Pick & { userId: User["id"] }) { - return prisma.note.deleteMany({ - where: { id, userId }, - }); -} diff --git a/app/routes/notes.$noteId.tsx b/app/routes/notes.$noteId.tsx deleted file mode 100644 index 3edd6ff..0000000 --- a/app/routes/notes.$noteId.tsx +++ /dev/null @@ -1,70 +0,0 @@ -import type { ActionFunctionArgs, LoaderFunctionArgs } from "@remix-run/node"; -import { json, redirect } from "@remix-run/node"; -import { - Form, - isRouteErrorResponse, - useLoaderData, - useRouteError, -} from "@remix-run/react"; -import invariant from "tiny-invariant"; - -import { deleteNote, getNote } from "~/models/note.server"; -import { requireUserId } from "~/session.server"; - -export const loader = async ({ params, request }: LoaderFunctionArgs) => { - const userId = await requireUserId(request); - invariant(params.noteId, "noteId not found"); - - const note = await getNote({ id: params.noteId, userId }); - if (!note) { - throw new Response("Not Found", { status: 404 }); - } - return json({ note }); -}; - -export const action = async ({ params, request }: ActionFunctionArgs) => { - const userId = await requireUserId(request); - invariant(params.noteId, "noteId not found"); - - await deleteNote({ id: params.noteId, userId }); - - return redirect("/notes"); -}; - -export default function NoteDetailsPage() { - const data = useLoaderData(); - - return ( -
-

{data.note.title}

-

{data.note.body}

-
-
- -
-
- ); -} - -export function ErrorBoundary() { - const error = useRouteError(); - - if (error instanceof Error) { - return
An unexpected error occurred: {error.message}
; - } - - if (!isRouteErrorResponse(error)) { - return

Unknown Error

; - } - - if (error.status === 404) { - return
Note not found
; - } - - return
An unexpected error occurred: {error.statusText}
; -} diff --git a/app/routes/notes._index.tsx b/app/routes/notes._index.tsx deleted file mode 100644 index aa858a9..0000000 --- a/app/routes/notes._index.tsx +++ /dev/null @@ -1,12 +0,0 @@ -import { Link } from "@remix-run/react"; - -export default function NoteIndexPage() { - return ( -

- No note selected. Select a note on the left, or{" "} - - create a new note. - -

- ); -} diff --git a/app/routes/notes.new.tsx b/app/routes/notes.new.tsx deleted file mode 100644 index 48dd52d..0000000 --- a/app/routes/notes.new.tsx +++ /dev/null @@ -1,109 +0,0 @@ -import type { ActionFunctionArgs } from "@remix-run/node"; -import { json, redirect } from "@remix-run/node"; -import { Form, useActionData } from "@remix-run/react"; -import { useEffect, useRef } from "react"; - -import { createNote } from "~/models/note.server"; -import { requireUserId } from "~/session.server"; - -export const action = async ({ request }: ActionFunctionArgs) => { - const userId = await requireUserId(request); - - const formData = await request.formData(); - const title = formData.get("title"); - const body = formData.get("body"); - - if (typeof title !== "string" || title.length === 0) { - return json( - { errors: { body: null, title: "Title is required" } }, - { status: 400 }, - ); - } - - if (typeof body !== "string" || body.length === 0) { - return json( - { errors: { body: "Body is required", title: null } }, - { status: 400 }, - ); - } - - const note = await createNote({ body, title, userId }); - - return redirect(`/notes/${note.id}`); -}; - -export default function NewNotePage() { - const actionData = useActionData(); - const titleRef = useRef(null); - const bodyRef = useRef(null); - - useEffect(() => { - if (actionData?.errors?.title) { - titleRef.current?.focus(); - } else if (actionData?.errors?.body) { - bodyRef.current?.focus(); - } - }, [actionData]); - - return ( -
-
- - {actionData?.errors?.title ? ( -
- {actionData.errors.title} -
- ) : null} -
- -
-