1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-05 05:05:17 +02:00

chore: Add trip selection to AddFromFeatured component

This commit is contained in:
Sean Morley 2024-05-26 23:24:31 +00:00
parent 8888650c56
commit 4fd174530b
3 changed files with 73 additions and 17 deletions

View file

@ -1,24 +1,29 @@
<script lang="ts"> <script lang="ts">
import type { Adventure } from "$lib/utils/types"; import type { Adventure, Trip } from "$lib/utils/types";
import { createEventDispatcher } from "svelte"; import { createEventDispatcher } from "svelte";
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
import { onMount } from "svelte"; import { onMount } from "svelte";
let modal: HTMLDialogElement; let modal: HTMLDialogElement;
let trips: Trip[] = [];
export let adventure: Adventure; export let adventure: Adventure;
onMount(() => { onMount(async () => {
modal = document.getElementById("my_modal_1") as HTMLDialogElement; modal = document.getElementById("my_modal_1") as HTMLDialogElement;
if (modal) { if (modal) {
modal.showModal(); modal.showModal();
} }
let res = await fetch("/api/trips");
trips = await res.json();
console.log(trips);
}); });
function close() { function close() {
dispatch("close"); dispatch("close");
} }
function viisted() { function visited() {
dispatch("visited"); dispatch("visited");
close(); close();
} }
@ -27,6 +32,11 @@
close(); close();
} }
function trip(trip: Trip) {
dispatch("trip", trip);
close();
}
function handleKeydown(event: KeyboardEvent) { function handleKeydown(event: KeyboardEvent) {
if (event.key === "Escape") { if (event.key === "Escape") {
close(); close();
@ -41,10 +51,35 @@
<h3 class="font-bold text-lg"> <h3 class="font-bold text-lg">
Where should Adventure: {adventure.name} be added? Where should Adventure: {adventure.name} be added?
</h3> </h3>
<button class="btn btn-primary mr-2" on:click={viisted} <div class="flex items-center justify-center">
<button class="btn btn-primary mr-2" on:click={visited}
>Visited Adventures</button >Visited Adventures</button
> >
<button class="btn btn-primary" on:click={idea}>Adventure Idea</button> <button class="btn btn-primary mr-2" on:click={idea}
>Adventure Idea</button
>
<details class="dropdown">
<summary class="m-1 btn">Add to Trip</summary>
<ul
class="p-2 shadow menu dropdown-content z-[1] bg-base-100 rounded-box w-52"
>
{#each trips as trip}
<li>
<button
class="btn btn-primary"
on:click={() => {
dispatch("trip", trip);
close();
}}
>
{trip.name}
</button>
</li>
{/each}
</ul>
</details>
<div class="h-32"></div>
<button class="btn btn-neutral" on:click={close}>Close</button> <button class="btn btn-neutral" on:click={close}>Close</button>
</div> </div>
</div>
</dialog> </dialog>

View file

@ -2,6 +2,7 @@ import { db } from "$lib/db/db.server";
import { userPlannedTrips } from "$lib/db/schema"; import { userPlannedTrips } from "$lib/db/schema";
import { error, type RequestEvent } from "@sveltejs/kit"; import { error, type RequestEvent } from "@sveltejs/kit";
import { and, eq } from "drizzle-orm"; import { and, eq } from "drizzle-orm";
import type { Trip } from "$lib/utils/types";
export async function POST(event: RequestEvent): Promise<Response> { export async function POST(event: RequestEvent): Promise<Response> {
if (!event.locals.user) { if (!event.locals.user) {
@ -37,7 +38,6 @@ export async function POST(event: RequestEvent): Promise<Response> {
description: description || null, description: description || null,
startDate: startDate || null, startDate: startDate || null,
endDate: endDate || null, endDate: endDate || null,
adventures: JSON.stringify([]),
}) })
.returning({ insertedId: userPlannedTrips.id }) .returning({ insertedId: userPlannedTrips.id })
.execute(); .execute();
@ -78,13 +78,6 @@ export async function GET(event: RequestEvent): Promise<Response> {
.where(eq(userPlannedTrips.userId, event.locals.user.id)) .where(eq(userPlannedTrips.userId, event.locals.user.id))
.execute(); .execute();
// json parse the adventures into an Adventure array
for (let trip of trips) {
if (trip.adventures) {
trip.adventures = JSON.parse(trip.adventures as unknown as string);
}
}
return new Response(JSON.stringify(trips), { return new Response(JSON.stringify(trips), {
status: 200, status: 200,
headers: { headers: {

View file

@ -2,7 +2,7 @@
export let data; export let data;
import { goto } from "$app/navigation"; import { goto } from "$app/navigation";
import AdventureCard from "$lib/components/AdventureCard.svelte"; import AdventureCard from "$lib/components/AdventureCard.svelte";
import type { Adventure } from "$lib/utils/types.js"; import type { Adventure, Trip } from "$lib/utils/types.js";
import AddFromFeatured from "$lib/components/AddFromFeatured.svelte"; import AddFromFeatured from "$lib/components/AddFromFeatured.svelte";
import { addAdventure } from "../../services/adventureService.js"; import { addAdventure } from "../../services/adventureService.js";
import SucessToast from "$lib/components/SucessToast.svelte"; import SucessToast from "$lib/components/SucessToast.svelte";
@ -26,6 +26,33 @@
adventureToAdd = event.detail; adventureToAdd = event.detail;
} }
const addToTrip = async (event: { detail: Trip }) => {
if (!adventureToAdd) {
showToast("Failed to add adventure");
adventureToAdd = null;
} else {
let detailAdventure = adventureToAdd;
detailAdventure.tripId = event.detail.id;
detailAdventure.type = "planner";
console.log(detailAdventure);
let res = await fetch("/api/planner", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
detailAdventure,
}),
});
if (res.status === 401) {
goto("/login");
} else {
showToast("Adventure added to trip!");
adventureToAdd = null;
}
}
};
async function addToVisted() { async function addToVisted() {
let detailAdventure = adventureToAdd; let detailAdventure = adventureToAdd;
adventureToAdd = null; adventureToAdd = null;
@ -79,6 +106,7 @@
on:close={() => (adventureToAdd = null)} on:close={() => (adventureToAdd = null)}
on:visited={addToVisted} on:visited={addToVisted}
on:idea={addIdea} on:idea={addIdea}
on:trip={addToTrip}
/> />
{/if} {/if}