From 7a981a5a5662e7f05fb9384423b0c449642e623a Mon Sep 17 00:00:00 2001 From: Johanne Blikberg Herheim Date: Sat, 29 Mar 2025 21:09:45 +0100 Subject: [PATCH] Add my events --- app/event/[slug]/page.tsx | 37 +++++- app/lib/authOptions.ts | 25 +++-- app/profile/[id]/page.tsx | 29 ++++- components/types/post.ts | 231 ++++++++++++++++++++++++++++++++++---- components/types/user.ts | 43 +++++++ 5 files changed, 328 insertions(+), 37 deletions(-) create mode 100644 components/types/user.ts diff --git a/app/event/[slug]/page.tsx b/app/event/[slug]/page.tsx index c5be566..2100d72 100644 --- a/app/event/[slug]/page.tsx +++ b/app/event/[slug]/page.tsx @@ -3,6 +3,7 @@ import { formatDateWithDay, formatTime } from "@/components/lib/date-utils"; import { Event, getEventById } from "@/components/types/post"; import { Button } from "@/components/ui/button"; import { Calendar, Clock, User } from "lucide-react"; +import { useSession } from "next-auth/react"; import { usePathname } from "next/navigation"; import { useState } from "react"; @@ -74,13 +75,31 @@ function EventButtons() { ); } +type Comment = { + user: string; + text: string; + time: string; +}; + function CommentSection() { - const [comments, setComments] = useState([]); + const [comments, setComments] = useState([]); const [newComment, setNewComment] = useState(""); + const { data: session } = useSession(); + const user = session?.user; const handleAddComment = () => { - if (newComment.trim() === "") return; - setComments((prev) => [...prev, newComment.trim()]); + if (newComment.trim() === "" || !user) return; + + const newCommentObj: Comment = { + user: user.name || "Anonym", + text: newComment.trim(), + time: new Date().toLocaleString("no-NO", { + dateStyle: "short", + timeStyle: "short", + }), + }; + + setComments((prev) => [...prev, newCommentObj]); setNewComment(""); }; @@ -93,7 +112,17 @@ function CommentSection() { key={index} className="bg-primary p-2 rounded-md shadow-md text-sm" > - {comment} +
+
+ +
+
+

{comment.user}

+

{comment.time}

+
+
+ +

{comment.text}

))} diff --git a/app/lib/authOptions.ts b/app/lib/authOptions.ts index f42ed9c..1debbe6 100644 --- a/app/lib/authOptions.ts +++ b/app/lib/authOptions.ts @@ -1,3 +1,4 @@ +import { users } from "@/components/types/user"; import { NextAuthOptions } from "next-auth"; import CredentialsProvider from "next-auth/providers/credentials"; @@ -10,19 +11,23 @@ export const authOptions: NextAuthOptions = { password: { label: "Password", type: "password" }, }, async authorize(credentials) { - const user = { - id: "1", - name: "Ole Brumm", - email: "OleBrumm@example.com", - }; + if (!credentials?.email || !credentials?.password) { + return null; + } + + // Søk etter brukeren i listen basert på e-post og passord + const user = users.find( + (user) => + user.email === credentials.email && + user.password === credentials.password + ); - if ( - credentials?.email === "OleBrumm@example.com" && - credentials?.password === "pass123" - ) { - return user; + // Hvis brukeren finnes, returner brukerobjektet + if (user) { + return { id: user.id, name: user.name, email: user.email }; } + // Hvis ingen match finnes, returner null return null; }, }), diff --git a/app/profile/[id]/page.tsx b/app/profile/[id]/page.tsx index 7631cfe..742d9de 100644 --- a/app/profile/[id]/page.tsx +++ b/app/profile/[id]/page.tsx @@ -1,18 +1,39 @@ "use client"; +import { Post } from "@/components/posts"; +import { posts } from "@/components/types/post"; import { Button } from "@/components/ui/button"; import { signOut, useSession } from "next-auth/react"; +import Link from "next/link"; export default function ProfilePage() { - const user = { id: 1, name: "Ole Brumm", email: "ole@brumm.no" }; const { data: session } = useSession(); + const user = session?.user; + const myEvents = posts.filter((event) => event.user.email === user?.email); + return (
-

{user.name}

-

Navn: {user.name}

-

E-post: {user.email}

+

{user?.name}

+

Navn: {user?.name}

+

E-post: {user?.email}

+ +
+

Mine arrangementer:

+ {myEvents.length > 0 ? ( +
    + {myEvents.map((event) => ( + + + + ))} +
+ ) : ( +

Ingen arrangementer funnet.

+ )} +
+ {session && (