-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work on implementation of comments page, in prog
- Loading branch information
Showing
2 changed files
with
73 additions
and
13 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
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 |
---|---|---|
@@ -1,27 +1,85 @@ | ||
import { cookies } from 'next/headers'; | ||
import { redirect } from 'next/navigation'; | ||
import { getComments } from '../../database/comments'; | ||
import { getEvent } from '../../database/events'; | ||
import { getUser } from '../../database/users'; | ||
import CommentsForm from './CommentsForm'; | ||
|
||
// import CommentsForm from './CommentsForm'; | ||
type SearchParams = { eventId?: string }; | ||
|
||
export default async function CommentsPage() { | ||
// Task: Restrict access to the notes page and only display notes belonging to the current logged in user | ||
// 1. Check if the sessionToken cookie exists | ||
export default async function CommentsPage({ | ||
searchParams, | ||
}: { | ||
searchParams: SearchParams; // Use the defined type here | ||
}) { | ||
const sessionCookie = cookies().get('sessionToken'); | ||
|
||
// 2. Query user with the sessionToken | ||
const user = sessionCookie && (await getUser(sessionCookie.value)); | ||
if (!sessionCookie) { | ||
redirect('/login?returnTo=/comments'); | ||
return null; | ||
} | ||
|
||
// 3. If the user does not exist, redirect to the login with the returnTo query parameter | ||
if (!user) redirect(`/login?returnTo=/${user}`); | ||
const user = await getUser(sessionCookie.value); | ||
|
||
// 4. Display the notes for the current logged in user | ||
if (!user) { | ||
redirect('/login?returnTo=/comments'); | ||
return null; | ||
} | ||
|
||
const comments = await getComments(sessionCookie.value); | ||
console.log(comments); | ||
const eventId = searchParams.eventId; | ||
|
||
if (!eventId) { | ||
return <p>Event ID is missing in the query parameters.</p>; | ||
} | ||
|
||
return <h1>posted comments</h1>; | ||
const siteEvent = await getEvent(sessionCookie.value, parseInt(eventId)); | ||
|
||
// return <CommentsForm comments={comments} user={user} event={event} />; | ||
if (!siteEvent) { | ||
return ( | ||
<p>Event not found or you do not have permission to view this event.</p> | ||
); | ||
} | ||
|
||
const comments = await getComments(sessionCookie.value); | ||
|
||
return <CommentsForm comments={comments} user={user} event={siteEvent} />; | ||
} | ||
|
||
// import { cookies } from 'next/headers'; | ||
// import { redirect } from 'next/navigation'; | ||
// import { getComments } from '../../database/comments'; | ||
// import { getEvent } from '../../database/events'; | ||
// import { getUser } from '../../database/users'; | ||
// import CommentsForm from './CommentsForm'; | ||
|
||
// // import CommentsForm from './CommentsForm'; | ||
|
||
// export default async function CommentsPage() { | ||
// // Task: Restrict access to the notes page and only display notes belonging to the current logged in user | ||
// // 1. Check if the sessionToken cookie exists | ||
// const sessionCookie = cookies().get('sessionToken'); | ||
|
||
// // 2. Query user with the sessionToken | ||
// const user = sessionCookie && (await getUser(sessionCookie.value)); | ||
|
||
// // 3. If the user does not exist, redirect to the login with the returnTo query parameter | ||
// if (!user) redirect(`/login?returnTo=/${user}`); | ||
|
||
// // 4. Display the notes for the current logged in user | ||
|
||
// const comments = await getComments(sessionCookie.value); | ||
// console.log(comments); | ||
|
||
// return <h1>posted comments</h1>; | ||
|
||
// const siteEvent = await getEvent( | ||
// sessionCookie.value, | ||
// parseInt(params.eventId), | ||
// ); | ||
|
||
// if (!siteEvent) { | ||
// return <p>Event not found or you do not have permission to view this.</p>; | ||
// } | ||
|
||
// return <CommentsForm comments={comments} user={user!} event={siteEvent!} />; | ||
// } |