diff --git a/src/lib/components/TripCard.svelte b/src/lib/components/TripCard.svelte new file mode 100644 index 0000000..fa7c652 --- /dev/null +++ b/src/lib/components/TripCard.svelte @@ -0,0 +1,63 @@ + + +
+
+

{trip.name}

+ {#if trip.description && trip.description !== ""} +
+ +

{trip.description}

+
+ {/if} + {#if trip.startDate && trip.startDate !== ""} +
+ +

{trip.startDate}

+
+ {/if} + {#if trip.endDate && trip.endDate !== ""} +
+ +

{trip.endDate}

+
+ {/if} +
+ +
+
+
diff --git a/src/routes/api/trips/+server.ts b/src/routes/api/trips/+server.ts index 4dc6fe3..50b0e40 100644 --- a/src/routes/api/trips/+server.ts +++ b/src/routes/api/trips/+server.ts @@ -1,7 +1,7 @@ import { db } from "$lib/db/db.server"; import { userPlannedTrips } from "$lib/db/schema"; import { error, type RequestEvent } from "@sveltejs/kit"; -import { eq } from "drizzle-orm"; +import { and, eq } from "drizzle-orm"; export async function POST(event: RequestEvent): Promise { if (!event.locals.user) { @@ -92,3 +92,38 @@ export async function GET(event: RequestEvent): Promise { }, }); } + +export async function DELETE(event: RequestEvent): Promise { + if (!event.locals.user) { + return new Response(JSON.stringify({ error: "No user found" }), { + status: 401, + headers: { + "Content-Type": "application/json", + }, + }); + } + + const body = await event.request.json(); + if (!body.id) { + return error(400, { + message: "No trip id provided", + }); + } + + let res = await db + .delete(userPlannedTrips) + .where( + and( + eq(userPlannedTrips.userId, event.locals.user.id), + eq(userPlannedTrips.id, body.id) + ) + ) + .execute(); + + return new Response(JSON.stringify({ message: "Trip deleted" }), { + status: 200, + headers: { + "Content-Type": "application/json", + }, + }); +} diff --git a/src/routes/planner/+page.svelte b/src/routes/planner/+page.svelte index 1a6dafa..53add80 100644 --- a/src/routes/planner/+page.svelte +++ b/src/routes/planner/+page.svelte @@ -13,6 +13,7 @@ import SucessToast from "$lib/components/SucessToast.svelte"; import mapDrawing from "$lib/assets/adventure_map.svg"; import CreateNewTripPlan from "$lib/components/CreateNewTripPlan.svelte"; + import TripCard from "$lib/components/TripCard.svelte"; export let data; let adventuresPlans: Adventure[] = []; @@ -154,6 +155,34 @@ showToast("Failed to get trips"); }); } + + async function removeTrip(event: { detail: number }) { + let initialLength: number = tripPlans.length; + const response = await fetch("/api/trips", { + method: "DELETE", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ id: event.detail }), + }) + .then((response) => response.json()) + .then((data) => { + console.log("Success:", data); + let theTrip = tripPlans.find((trip) => trip.id === event.detail); + if (theTrip) { + let newArray = tripPlans.filter((trip) => trip.id !== event.detail); + if (newArray.length === initialLength - 1) { + tripPlans = newArray; + showToast("Trip removed successfully!"); + } else { + showToast("Failed to remove trip"); + } + } + }) + .catch((error) => { + showToast("Failed to get trips"); + }); + } {#if isShowingToast} @@ -247,19 +276,20 @@ {/if} -{#each tripPlans as trip (trip.id)} -
-
-

{trip.name}

-

{trip.description}

-

- Start Date: - {trip.startDate} End Date: - {trip.endDate} -

-
+
+ {#each tripPlans as trip (trip.id)} + + {/each} +
+ +{#if tripPlans.length == 0 && !isLoadingIdeas && !isLoadingTrips && !isShowingMoreFields && !isShowingNewTrip} +
+

Add some trips!

+ Logo
-{/each} +{/if} My Plans | AdventureLog