Skip to content

Commit

Permalink
Revert "Add better UI for different cases"
Browse files Browse the repository at this point in the history
This reverts commit 4143729.
  • Loading branch information
Eprince-hub committed Nov 4, 2024
1 parent 4143729 commit 9b6e622
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 36 deletions.
28 changes: 8 additions & 20 deletions app/notes/[noteId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cookies } from 'next/headers';
import Link from 'next/link';
import { getNote, selectNoteExists } from '../../../database/notes';
import { getNote } from '../../../database/notes';

type Props = {
params: Promise<{
Expand All @@ -11,29 +11,18 @@ type Props = {
export default async function NotePage(props: Props) {
// Task: Restrict access to the note page only to the user who created the note

const noteId = Number((await props.params).noteId);

// 1. Checking if the sessionToken cookie exists
const sessionTokenCookie = (await cookies()).get('sessionToken');

// 2. Check if the note exists
const noteExists = await selectNoteExists(noteId);

if (!noteExists) {
return (
<div>
<h1>Error loading note {noteId}</h1>
<div>The note does not exist</div>
<Link href="/notes">Back to notes</Link>
</div>
);
}

// 3. Query the note with the session token and noteId
// 2. Query the note with the session token and noteId
const note =
sessionTokenCookie && (await getNote(sessionTokenCookie.value, noteId));
sessionTokenCookie &&
(await getNote(
sessionTokenCookie.value,
Number((await props.params).noteId),
));

// 4. If there is no note for the current user, show restricted access message
// 3. If there is no note for the current user, show restricted access message
if (!note) {
return (
<div>
Expand All @@ -44,7 +33,6 @@ export default async function NotePage(props: Props) {
);
}

// 5. Finally display the notes created by the current user
return (
<div>
<h1>{note.title}</h1>
Expand Down
16 changes: 0 additions & 16 deletions database/notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,6 @@ export const getNote = cache(
},
);

export async function selectNoteExists(noteId: Note['id']) {
const [record] = await sql<{ exists: boolean }[]>`
SELECT
EXISTS (
SELECT
TRUE
FROM
notes
WHERE
id = ${noteId}
)
`;

return Boolean(record?.exists);
}

export const createNote = cache(
async (
sessionToken: Session['token'],
Expand Down

0 comments on commit 9b6e622

Please sign in to comment.