From 485e64d5009ee39dc2d153f4570ddccd4dbed5af Mon Sep 17 00:00:00 2001 From: TheDannyG Date: Fri, 21 Jul 2023 17:41:37 -0400 Subject: [PATCH] [CIT-23] Uninterested Button (#44) * Trigger build * CIT-23: add Uninterest button * CIT-23: switch to status api route * CIT-23: add Uninterest button * Trigger build --------- Co-authored-by: Ishaan --- .gitignore | 3 +- citrus/app/api/experiences/[id]/route.ts | 5 +-- citrus/app/api/statuses/route.ts | 51 ++++++++++++++++++++++++ citrus/app/api/users/route.ts | 4 +- citrus/components/EventButton.tsx | 8 +++- citrus/components/UninterestButton.tsx | 37 +++++++++++++++++ 6 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 citrus/components/UninterestButton.tsx diff --git a/.gitignore b/.gitignore index 152e9ff..8b73356 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,5 @@ citrus/next-env.d.ts .vercel #data -cockroach-data/ \ No newline at end of file +cockroach-data/ +node_modules/ \ No newline at end of file diff --git a/citrus/app/api/experiences/[id]/route.ts b/citrus/app/api/experiences/[id]/route.ts index 08dfcb4..5fc7246 100644 --- a/citrus/app/api/experiences/[id]/route.ts +++ b/citrus/app/api/experiences/[id]/route.ts @@ -148,7 +148,7 @@ export async function PUT(request: Request, data: body }); - if (addUser !== null) { + if (addUser === "true") { if (!session?.user) { return NextResponse.json({ status: 400, "error": "User not logged in"}); } @@ -171,8 +171,7 @@ export async function PUT(request: Request, }) // Return the updated event and the created status return NextResponse.json({ updatedEvent: updatedEvent, createdStatus: createdStatus }); - } - else { + } else { return NextResponse.json({ updatedEvent: updatedEvent }); } } catch (e) { diff --git a/citrus/app/api/statuses/route.ts b/citrus/app/api/statuses/route.ts index 8501fe3..9e79c2b 100644 --- a/citrus/app/api/statuses/route.ts +++ b/citrus/app/api/statuses/route.ts @@ -199,3 +199,54 @@ export async function GET(request: Request) { experiences, }); } + +/** + * @api {delete} /statuses/ Delete an event from a user's interested events + * @apiName DeleteInterestedEvent + * @apiGroup Events + * + * @apiParam {String} id The id of the event to delete + * @apiParam {String} [user_id] The id of the user to delete the event from + * + * @apiSuccessExample Success-Response: + * HTTP/1.1 200 OK + * { + * "success": true + * } + * + * @apiErrorExample Error-Response: + * HTTP/1.1 400 Bad Request + * { + * "error": "You must provide either a user_id or be signed in to use this endpoint" + * } + * + **/ +export async function DELETE(request: Request) { + const session = await getServerSession(authOptions); + const { searchParams } = new URL(request.url); + const event_id = searchParams.get('event_id'); + + if (!session) { + return NextResponse.json({ error: "You must be signed in to use this endpoint" }, { status: 400 }); + } + if (!event_id) { + return NextResponse.json({ error: "You must provide an event_id" }, { status: 400 }); + } + + try { + await prisma.user_attending_status.delete({ + where: { + username_event_id: { + username: session.user?.name, + event_id: event_id + } + } + } + ); + } + catch (e) { + return db.handleError(e); + } + + return NextResponse.json({ success: true}); +} \ No newline at end of file diff --git a/citrus/app/api/users/route.ts b/citrus/app/api/users/route.ts index 53c3b87..15688de 100644 --- a/citrus/app/api/users/route.ts +++ b/citrus/app/api/users/route.ts @@ -201,7 +201,7 @@ export async function PUT(request: Request) { const email = body.email; const premium = body.premium; const phone_number = body.phone_number; - const socials = body.socials; + //const socials = body.socials; if (password !== undefined) { if (!schema.validate(password)) { @@ -217,7 +217,7 @@ export async function PUT(request: Request) { email: email, premium: premium, phone_number: phone_number, - socials: socials + //socials: socials } try { diff --git a/citrus/components/EventButton.tsx b/citrus/components/EventButton.tsx index d23c1c8..79f708d 100644 --- a/citrus/components/EventButton.tsx +++ b/citrus/components/EventButton.tsx @@ -1,6 +1,7 @@ import { getServerSession } from 'next-auth/next' import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import InterestButton from '@/components/InterestButton' +import UninterestButton from './UninterestButton'; async function eventInfo(eventID: string) { @@ -18,8 +19,13 @@ export default async function EventButton({eventID}: {eventID: string}){ const data = await eventInfo(eventID); + if (data.user_id === session.user?.name) { + return
+ } + if(data.attendees.includes(session.user?.name)) { - return

You're already interested in this event.

+ //return

You're already interested in this event.

+ return } return ( diff --git a/citrus/components/UninterestButton.tsx b/citrus/components/UninterestButton.tsx new file mode 100644 index 0000000..f42b9a1 --- /dev/null +++ b/citrus/components/UninterestButton.tsx @@ -0,0 +1,37 @@ +"use client"; +import { useRouter } from "next/navigation"; + +async function onUninterestHandler(username: string | null | undefined, eventID: string){ + const res = await fetch(`/api/experiences/${eventID}`); + console.log(res); + const data = await res.json(); + const attendees = data.attendees; + const index = attendees.indexOf(username); + if (index > -1) { + attendees.splice(index, 1); + const res2 = await fetch(`/api/experiences/${eventID}?addUser=false`, { + method: 'PUT', + body: JSON.stringify({"attendees": attendees}), + }); + console.log(res2); + } + + const res3 = await fetch(`/api/statuses?event_id=${eventID}`, { + method: 'DELETE' + }); + console.log(res3); + +} + +export default function UninterestButton({username, eventID}: {username: string | null | undefined, eventID: string}){ + const router = useRouter(); + return ( +
+ +
+ ) + +} \ No newline at end of file