Skip to content

Commit

Permalink
Work on implementation of comments page, in prog
Browse files Browse the repository at this point in the history
  • Loading branch information
Draikth committed Sep 3, 2024
1 parent 337a2f2 commit b2f8f85
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 13 deletions.
2 changes: 2 additions & 0 deletions app/comments/CommentsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default function CommentsForm(props: Prop) {
<>
<h1>Notes for {props.user.username}</h1>

<div>{props.event.name}</div>

<div>
<div>
{props.comments.length === 0 ? (
Expand Down
84 changes: 71 additions & 13 deletions app/comments/page.tsx
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!} />;
// }

0 comments on commit b2f8f85

Please sign in to comment.