1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-30 10:19:37 +02:00

Refactor adventure page layout and add dynamic content

This commit is contained in:
Sean Morley 2024-04-28 16:03:38 +00:00
parent c09028ae40
commit d4f94b436d
9 changed files with 165 additions and 78 deletions

View file

@ -4,38 +4,31 @@
import calendar from "$lib/assets/calendar.svg"; import calendar from "$lib/assets/calendar.svg";
import { goto } from "$app/navigation"; import { goto } from "$app/navigation";
import { desc } from "drizzle-orm"; import { desc } from "drizzle-orm";
import type { Adventure } from "$lib/utils/types";
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
export let type: String; export let type: String;
export let name: String | undefined = undefined; export let adventure: Adventure;
export let location: String | undefined = undefined;
export let date: String | undefined = undefined; // export let name: String | undefined = undefined;
export let id: Number | undefined = undefined; // export let location: String | undefined = undefined;
export let regionId: String | undefined = undefined; // export let date: String | undefined = undefined;
export let visited: Boolean | undefined = undefined; // export let id: Number | undefined = undefined;
function remove() { function remove() {
dispatch("remove", id); dispatch("remove", adventure.id);
} }
function edit() { function edit() {
dispatch("edit", id); dispatch("edit", adventure.id);
} }
function add() { function add() {
dispatch("add", { name, location }); dispatch("add", adventure);
}
function markVisited() {
dispatch("markVisited", regionId);
visited = true;
}
function removeVisit() {
dispatch("removeVisit", regionId);
visited = false;
} }
function moreInfo() { function moreInfo() {
console.log(id); console.log(adventure.id);
goto(`/adventure/${id}`); goto(`/adventure/${adventure.id}`);
} }
</script> </script>
@ -43,17 +36,17 @@
class="card min-w-max lg:w-96 md:w-80 sm:w-60 xs:w-40 bg-primary-content shadow-xl overflow-hidden text-base-content" class="card min-w-max lg:w-96 md:w-80 sm:w-60 xs:w-40 bg-primary-content shadow-xl overflow-hidden text-base-content"
> >
<div class="card-body"> <div class="card-body">
<h2 class="card-title overflow-ellipsis">{name}</h2> <h2 class="card-title overflow-ellipsis">{adventure.name}</h2>
{#if location && location !== ""} {#if adventure.location && adventure.location !== ""}
<div class="inline-flex items-center"> <div class="inline-flex items-center">
<iconify-icon icon="mdi:map-marker" class="text-xl"></iconify-icon> <iconify-icon icon="mdi:map-marker" class="text-xl"></iconify-icon>
<p class="ml-.5">{location}</p> <p class="ml-.5">{adventure.location}</p>
</div> </div>
{/if} {/if}
{#if date && date !== ""} {#if adventure.date && adventure.date !== ""}
<div class="inline-flex items-center"> <div class="inline-flex items-center">
<iconify-icon icon="mdi:calendar" class="text-xl"></iconify-icon> <iconify-icon icon="mdi:calendar" class="text-xl"></iconify-icon>
<p class="ml-1">{date}</p> <p class="ml-1">{adventure.date}</p>
</div> </div>
{/if} {/if}
<div class="card-actions justify-end"> <div class="card-actions justify-end">

View file

@ -61,7 +61,7 @@
/> />
</div> </div>
<div> <div>
<label for="date">date</label> <label for="date">Date</label>
<input <input
type="date" type="date"
id="date" id="date"
@ -69,6 +69,15 @@
class="input input-bordered w-full max-w-xs" class="input input-bordered w-full max-w-xs"
/> />
</div> </div>
<div>
<label for="date">Description</label>
<input
type="text"
id="description"
bind:value={adventureToEdit.description}
class="input input-bordered w-full max-w-xs"
/>
</div>
<button class="btn btn-primary mr-4 mt-4" on:click={submit}>Save</button <button class="btn btn-primary mr-4 mt-4" on:click={submit}>Save</button
> >
<!-- if there is a button in form, it will close the modal --> <!-- if there is a button in form, it will close the modal -->

View file

@ -0,0 +1,95 @@
<script lang="ts">
let newAdventure: Adventure;
newAdventure = {
id: -1,
type: "mylog",
name: "",
location: "",
date: "",
};
import { createEventDispatcher } from "svelte";
import type { Adventure } from "$lib/utils/types";
const dispatch = createEventDispatcher();
import { onMount } from "svelte";
let modal: HTMLDialogElement;
onMount(() => {
modal = document.getElementById("my_modal_1") as HTMLDialogElement;
if (modal) {
modal.showModal();
}
});
function create() {
dispatch("create", newAdventure);
console.log(newAdventure);
}
function close() {
dispatch("close");
}
function handleKeydown(event: KeyboardEvent) {
if (event.key === "Escape") {
close();
}
}
</script>
<dialog id="my_modal_1" class="modal">
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
<div class="modal-box" role="dialog" on:keydown={handleKeydown} tabindex="0">
<h3 class="font-bold text-lg">New Adventure</h3>
<p class="py-4">Press ESC key or click the button below to close</p>
<div
class="modal-action items-center"
style="display: flex; flex-direction: column; align-items: center; width: 100%;"
>
<form method="dialog" style="width: 100%;">
<div>
<label for="name">Name</label>
<input
type="text"
id="name"
bind:value={newAdventure.name}
class="input input-bordered w-full max-w-xs"
/>
</div>
<div>
<label for="location">Location</label>
<input
type="text"
id="location"
bind:value={newAdventure.location}
class="input input-bordered w-full max-w-xs"
/>
</div>
<div>
<label for="date">date</label>
<input
type="date"
id="date"
bind:value={newAdventure.date}
class="input input-bordered w-full max-w-xs"
/>
</div>
<div>
<label for="date">Description</label>
<input
type="text"
id="description"
bind:value={newAdventure.description}
class="input input-bordered w-full max-w-xs"
/>
</div>
<button class="btn btn-primary mr-4 mt-4" on:click={create}>Save</button
>
<!-- if there is a button in form, it will close the modal -->
<button class="btn mt-4" on:click={close}>Close</button>
</form>
</div>
</div>
</dialog>

View file

@ -15,7 +15,7 @@
inject(); inject();
injectSpeedInsights(); injectSpeedInsights();
} else { } else {
console.log("Not using Vercel"); // console.log("Not using Vercel");
} }
let isServerSetup = data.isServerSetup; let isServerSetup = data.isServerSetup;

View file

@ -33,7 +33,7 @@
{/if} {/if}
{#if adventure.date} {#if adventure.date}
<p class="text-center text-lg mt-4 pl-16 pr-16"> <p class="text-center text-lg mt-4 pl-16 pr-16">
Visited on: {new Date(adventure.date).toLocaleDateString()} Visited on: {adventure.date}
</p> </p>
{/if} {/if}
{#if adventure.rating !== undefined && adventure.rating !== null} {#if adventure.rating !== undefined && adventure.rating !== null}

View file

@ -86,7 +86,7 @@ export async function POST(event: RequestEvent): Promise<Response> {
const { newAdventure } = await event.request.json(); const { newAdventure } = await event.request.json();
console.log(newAdventure); console.log(newAdventure);
const { name, location, date } = newAdventure; const { name, location, date, description } = newAdventure;
// insert the adventure to the user's visited list // insert the adventure to the user's visited list
await db await db
@ -97,6 +97,7 @@ export async function POST(event: RequestEvent): Promise<Response> {
name: name, name: name,
location: location, location: location,
date: date, date: date,
description: description,
}) })
.execute(); .execute();
let res = await db let res = await db
@ -107,7 +108,8 @@ export async function POST(event: RequestEvent): Promise<Response> {
eq(adventureTable.userId, event.locals.user.id), eq(adventureTable.userId, event.locals.user.id),
eq(adventureTable.name, name), eq(adventureTable.name, name),
eq(adventureTable.location, location), eq(adventureTable.location, location),
eq(adventureTable.date, date) eq(adventureTable.date, date),
eq(adventureTable.description, description)
) )
) )
.execute(); .execute();
@ -142,7 +144,7 @@ export async function PUT(event: RequestEvent): Promise<Response> {
// get properties from the body // get properties from the body
const { newAdventure } = await event.request.json(); const { newAdventure } = await event.request.json();
console.log(newAdventure); console.log(newAdventure);
const { name, location, date, id } = newAdventure; const { name, location, date, id, description } = newAdventure;
// update the adventure in the user's visited list // update the adventure in the user's visited list
await db await db
@ -151,6 +153,7 @@ export async function PUT(event: RequestEvent): Promise<Response> {
name: name, name: name,
location: location, location: location,
date: date, date: date,
description: description,
}) })
.where( .where(
and( and(
@ -162,7 +165,7 @@ export async function PUT(event: RequestEvent): Promise<Response> {
return new Response( return new Response(
JSON.stringify({ JSON.stringify({
adventure: { id, name, location, date }, adventure: { id, name, location, date, description },
message: { message: "Adventure updated" }, message: { message: "Adventure updated" },
}), }),
{ {

View file

@ -10,11 +10,10 @@
count = value; count = value;
}); });
async function add(event: CustomEvent<{ name: string; location: string }>) { async function add(event: CustomEvent<Adventure>) {
let newAdventure: Adventure = { let newAdventure: Adventure = {
name: event.detail.name, name: event.detail.name,
location: event.detail.location, location: event.detail.location,
date: "",
type: "mylog", type: "mylog",
id: -1, id: -1,
}; };
@ -47,14 +46,7 @@
class="grid xl:grid-cols-3 lg:grid-cols-3 md:grid-cols-2 sm:grid-cols-1 gap-4 mt-4 content-center auto-cols-auto ml-6 mr-6" class="grid xl:grid-cols-3 lg:grid-cols-3 md:grid-cols-2 sm:grid-cols-1 gap-4 mt-4 content-center auto-cols-auto ml-6 mr-6"
> >
{#each data.result as adventure (adventure.id)} {#each data.result as adventure (adventure.id)}
<AdventureCard <AdventureCard type="featured" on:add={add} {adventure} />
type="featured"
on:add={add}
id={adventure.id}
name={adventure.name}
location={adventure.location}
date=""
/>
{/each} {/each}
</div> </div>

View file

@ -13,10 +13,13 @@
import EditModal from "$lib/components/EditModal.svelte"; import EditModal from "$lib/components/EditModal.svelte";
import { generateRandomString } from "$lib"; import { generateRandomString } from "$lib";
import { visitCount } from "$lib/utils/stores/visitCountStore"; import { visitCount } from "$lib/utils/stores/visitCountStore";
import MoreFieldsInput from "$lib/components/MoreFieldsInput.svelte";
let newName = ""; let newName = "";
let newLocation = ""; let newLocation = "";
let isShowingMoreFields = false;
let adventureToEdit: Adventure | undefined; let adventureToEdit: Adventure | undefined;
let isShowingToast: boolean = false; let isShowingToast: boolean = false;
@ -56,16 +59,13 @@
URL.revokeObjectURL(url); URL.revokeObjectURL(url);
} }
const createNewAdventure = () => { const createNewAdventure = (event: { detail: Adventure }) => {
let currentDate = new Date();
let dateString = currentDate.toISOString().slice(0, 10); // Get date in "yyyy-mm-dd" format
// post to /api/visits
let newAdventure: Adventure = { let newAdventure: Adventure = {
type: "mylog", type: "mylog",
name: newName, name: event.detail.name,
location: newLocation, location: event.detail.location,
date: dateString, date: event.detail.date,
description: event.detail.description,
id: -1, id: -1,
}; };
@ -87,9 +87,10 @@
{ {
id: newId, id: newId,
type: "mylog", type: "mylog",
name: newName, name: event.detail.name,
location: newLocation, location: event.detail.location,
date: dateString, date: event.detail.date,
description: event.detail.description,
}, },
]; ];
newName = ""; // Reset newName and newLocation after adding adventure newName = ""; // Reset newName and newLocation after adding adventure
@ -111,6 +112,7 @@
location: event.detail.location, location: event.detail.location,
date: event.detail.date, date: event.detail.date,
id: event.detail.id, id: event.detail.id,
description: event.detail.description,
}; };
// put request to /api/visits with id and advneture data // put request to /api/visits with id and advneture data
@ -171,6 +173,7 @@
function handleClose() { function handleClose() {
adventureToEdit = undefined; adventureToEdit = undefined;
isShowingMoreFields = false;
} }
function deleteData() { function deleteData() {
@ -226,21 +229,13 @@
</div> </div>
<div class="flex flex-row items-center justify-center gap-4"> <div class="flex flex-row items-center justify-center gap-4">
<form on:submit={createNewAdventure} class="flex gap-2"> <button
<input type="button"
type="text" class="btn btn-secondary"
bind:value={newName} on:click={() => (isShowingMoreFields = !isShowingMoreFields)}
placeholder="Adventure Name" >
class="input input-bordered w-full max-w-xs" Show More Fields
/> </button>
<input
type="text"
bind:value={newLocation}
placeholder="Adventure Location"
class="input input-bordered w-full max-w-xs"
/>
<input class="btn btn-primary" type="submit" value="Add Adventure" />
</form>
</div> </div>
{#if adventures.length != 0} {#if adventures.length != 0}
<div class="flex justify-center items-center w-full mt-4 mb-4"> <div class="flex justify-center items-center w-full mt-4 mb-4">
@ -260,6 +255,14 @@
<SucessToast action={toastAction} /> <SucessToast action={toastAction} />
{/if} {/if}
{#if isShowingMoreFields}
<MoreFieldsInput
on:create={createNewAdventure}
on:close={handleClose}
on:submit={saveAdventure}
/>
{/if}
{#if adventureToEdit && adventureToEdit.id != undefined} {#if adventureToEdit && adventureToEdit.id != undefined}
<EditModal <EditModal
bind:adventureToEdit bind:adventureToEdit
@ -273,11 +276,8 @@
> >
{#each adventures as adventure (adventure.id)} {#each adventures as adventure (adventure.id)}
<AdventureCard <AdventureCard
{adventure}
type="mylog" type="mylog"
id={adventure.id}
name={adventure.name}
location={adventure.location}
date={adventure.date}
on:edit={editAdventure} on:edit={editAdventure}
on:remove={removeAdventure} on:remove={removeAdventure}
/> />

View file

@ -2,6 +2,7 @@
import type { Adventure } from "$lib/utils/types"; import type { Adventure } from "$lib/utils/types";
export let data; export let data;
let array = data.adventureArray as Adventure[]; let array = data.adventureArray as Adventure[];
console.log(array);
import AdventureCard from "$lib/components/AdventureCard.svelte"; import AdventureCard from "$lib/components/AdventureCard.svelte";
</script> </script>
@ -20,13 +21,7 @@
class="grid xl:grid-cols-3 lg:grid-cols-3 md:grid-cols-2 sm:grid-cols-1 gap-4 mt-4 content-center auto-cols-auto ml-6 mr-6" class="grid xl:grid-cols-3 lg:grid-cols-3 md:grid-cols-2 sm:grid-cols-1 gap-4 mt-4 content-center auto-cols-auto ml-6 mr-6"
> >
{#each array as adventure (adventure.id)} {#each array as adventure (adventure.id)}
<AdventureCard <AdventureCard type="shared" {adventure} />
type="shared"
id={adventure.id}
name={adventure.name}
location={adventure.location}
date={adventure.date}
/>
{/each} {/each}
</div> </div>