1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-19 04:49:37 +02:00
AdventureLog/frontend/src/routes/adventures/+page.svelte

379 lines
11 KiB
Svelte
Raw Normal View History

2024-07-11 09:33:52 -04:00
<script lang="ts">
2024-07-11 20:09:55 -04:00
import { enhance, deserialize } from '$app/forms';
2024-08-05 09:33:21 -04:00
import { goto } from '$app/navigation';
import { page } from '$app/stores';
2024-07-11 09:33:52 -04:00
import AdventureCard from '$lib/components/AdventureCard.svelte';
2024-08-17 22:40:27 -04:00
import AdventureModal from '$lib/components/AdventureModal.svelte';
import CategoryFilterDropdown from '$lib/components/CategoryFilterDropdown.svelte';
2024-07-11 09:33:52 -04:00
import NotFound from '$lib/components/NotFound.svelte';
import type { Adventure, Category } from '$lib/types';
2024-10-28 15:10:14 -04:00
import { t } from 'svelte-i18n';
2024-07-11 15:37:04 -04:00
import Plus from '~icons/mdi/plus';
2024-07-11 09:33:52 -04:00
export let data: any;
console.log(data);
2024-07-11 19:27:03 -04:00
let adventures: Adventure[] = data.props.adventures || [];
let categories: Category[] = data.props.categories || [];
2024-07-11 15:37:04 -04:00
2024-08-05 09:59:34 -04:00
let currentSort = {
order_by: '',
order: '',
visited: true,
planned: true,
includeCollections: true,
is_visited: 'all'
2024-08-05 09:59:34 -04:00
};
2024-07-11 15:37:04 -04:00
let resultsPerPage: number = 25;
2024-07-12 09:11:00 -04:00
2024-07-11 20:09:55 -04:00
let count = data.props.count || 0;
2024-10-28 13:56:57 -04:00
2024-07-12 09:11:00 -04:00
let totalPages = Math.ceil(count / resultsPerPage);
2024-07-13 10:28:45 -04:00
let currentPage: number = 1;
2024-07-11 20:09:55 -04:00
let typeString: string = '';
$: {
console.log(typeString);
if (typeof window !== 'undefined' && typeString) {
let url = new URL(window.location.href);
url.searchParams.set('types', typeString);
goto(url.toString(), { invalidateAll: true, replaceState: true });
} else if (typeof window !== 'undefined' && !typeString) {
let url = new URL(window.location.href);
url.searchParams.set('types', 'all');
goto(url.toString(), { invalidateAll: true, replaceState: true });
}
}
// sets typeString if present in the URL
$: {
// check to make sure its running on the client side
if (typeof window !== 'undefined') {
let url = new URL(window.location.href);
let types = url.searchParams.get('types');
if (types) {
typeString = types;
}
}
}
2024-08-05 09:33:21 -04:00
function handleChangePage(pageNumber: number) {
// let query = new URLSearchParams($page.url.searchParams.toString());
// query.set('page', pageNumber.toString());
// console.log(query.toString());
currentPage = pageNumber;
let url = new URL(window.location.href);
url.searchParams.set('page', pageNumber.toString());
adventures = [];
adventures = data.props.adventures;
2024-08-05 09:59:34 -04:00
2024-08-05 09:33:21 -04:00
goto(url.toString(), { invalidateAll: true, replaceState: true });
// goto(`?${query.toString()}`, { invalidateAll: true });
2024-07-11 19:27:03 -04:00
}
2024-08-05 09:33:21 -04:00
$: {
let url = new URL($page.url);
let page = url.searchParams.get('page');
if (page) {
currentPage = parseInt(page);
}
}
$: {
if (data.props.adventures) {
adventures = data.props.adventures;
}
if (data.props.count) {
count = data.props.count;
totalPages = Math.ceil(count / resultsPerPage);
}
2024-07-11 15:37:04 -04:00
}
2024-08-05 09:59:34 -04:00
$: {
let url = new URL($page.url);
currentSort.order_by = url.searchParams.get('order_by') || 'updated_at';
currentSort.order = url.searchParams.get('order_direction') || 'asc';
if (url.searchParams.get('planned') === 'on') {
currentSort.planned = true;
} else {
currentSort.planned = false;
}
if (url.searchParams.get('visited') === 'on') {
currentSort.visited = true;
} else {
currentSort.visited = false;
}
if (url.searchParams.get('include_collections') === 'on') {
currentSort.includeCollections = true;
} else {
currentSort.includeCollections = false;
}
if (!currentSort.visited && !currentSort.planned) {
currentSort.visited = true;
currentSort.planned = true;
2024-07-11 15:37:04 -04:00
}
if (url.searchParams.get('is_visited')) {
currentSort.is_visited = url.searchParams.get('is_visited') || 'all';
}
2024-07-11 15:37:04 -04:00
}
2024-08-17 22:40:27 -04:00
let adventureToEdit: Adventure | null = null;
let isAdventureModalOpen: boolean = false;
2024-07-11 15:37:04 -04:00
2024-08-13 12:03:18 -04:00
function deleteAdventure(event: CustomEvent<string>) {
2024-07-11 15:37:04 -04:00
adventures = adventures.filter((adventure) => adventure.id !== event.detail);
}
2024-08-17 22:40:27 -04:00
// function that save changes to an existing adventure or creates a new one if it doesn't exist
function saveOrCreate(event: CustomEvent<Adventure>) {
if (adventures.find((adventure) => adventure.id === event.detail.id)) {
adventures = adventures.map((adventure) => {
if (adventure.id === event.detail.id) {
return event.detail;
}
return adventure;
});
} else {
adventures = [event.detail, ...adventures];
}
isAdventureModalOpen = false;
2024-07-11 15:37:04 -04:00
}
function editAdventure(event: CustomEvent<Adventure>) {
adventureToEdit = event.detail;
2024-08-17 22:40:27 -04:00
isAdventureModalOpen = true;
2024-07-11 15:37:04 -04:00
}
2024-07-11 09:33:52 -04:00
let sidebarOpen = false;
function toggleSidebar() {
sidebarOpen = !sidebarOpen;
}
</script>
2024-08-17 22:40:27 -04:00
{#if isAdventureModalOpen}
<AdventureModal
2024-07-11 15:37:04 -04:00
{adventureToEdit}
2024-08-17 22:40:27 -04:00
on:close={() => (isAdventureModalOpen = false)}
on:save={saveOrCreate}
2024-07-11 15:37:04 -04:00
/>
{/if}
<div class="fixed bottom-4 right-4 z-[999]">
<div class="flex flex-row items-center justify-center gap-4">
<div class="dropdown dropdown-top dropdown-end">
<div tabindex="0" role="button" class="btn m-1 size-16 btn-primary">
<Plus class="w-8 h-8" />
</div>
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
<ul
tabindex="0"
class="dropdown-content z-[1] menu p-4 shadow bg-base-300 text-base-content rounded-box w-52 gap-4"
>
2024-10-28 15:10:14 -04:00
<p class="text-center font-bold text-lg">{$t('adventures.create_new')}</p>
2024-07-11 15:37:04 -04:00
<button
class="btn btn-primary"
on:click={() => {
2024-08-17 22:40:27 -04:00
isAdventureModalOpen = true;
adventureToEdit = null;
2024-07-11 15:37:04 -04:00
}}
>
2024-10-28 15:10:14 -04:00
{$t('adventures.adventure')}</button
2024-07-11 15:42:36 -04:00
>
2024-08-17 22:40:27 -04:00
2024-07-11 15:37:04 -04:00
<!-- <button
class="btn btn-primary"
on:click={() => (isShowingNewTrip = true)}>Trip Planner</button
> -->
</ul>
</div>
</div>
</div>
2024-07-11 09:33:52 -04:00
<div class="drawer lg:drawer-open">
<input id="my-drawer" type="checkbox" class="drawer-toggle" bind:checked={sidebarOpen} />
<div class="drawer-content">
<!-- Page content -->
2024-10-28 15:10:14 -04:00
<h1 class="text-center font-bold text-4xl mb-6">{$t('navbar.my_adventures')}</h1>
<p class="text-center">{count} {$t('adventures.count_txt')}</p>
2024-07-11 09:33:52 -04:00
{#if adventures.length === 0}
2024-07-18 14:55:23 -04:00
<NotFound error={undefined} />
2024-07-11 09:33:52 -04:00
{/if}
<div class="p-4">
<button
class="btn btn-primary drawer-button lg:hidden mb-4 fixed bottom-0 left-0 ml-2 z-[999]"
on:click={toggleSidebar}
>
2024-10-28 15:10:14 -04:00
{sidebarOpen ? $t(`adventures.close_filters`) : $t(`adventures.open_filters`)}
2024-07-11 09:33:52 -04:00
</button>
2024-07-18 22:12:47 -04:00
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
{#each adventures as adventure}
<AdventureCard
user={data.user}
type={adventure.type}
{adventure}
on:delete={deleteAdventure}
on:edit={editAdventure}
/>
{/each}
</div>
2024-07-13 10:28:45 -04:00
<div class="join flex items-center justify-center mt-4">
2024-08-05 09:33:21 -04:00
{#if totalPages > 1}
2024-07-13 10:28:45 -04:00
<div class="join">
{#each Array.from({ length: totalPages }, (_, i) => i + 1) as page}
2024-08-05 09:33:21 -04:00
{#if currentPage != page}
<button class="join-item btn btn-lg" on:click={() => handleChangePage(page)}
>{page}</button
>
{:else}
<button class="join-item btn btn-lg btn-active">{page}</button>
{/if}
2024-07-13 10:28:45 -04:00
{/each}
</div>
{/if}
2024-07-11 19:27:03 -04:00
</div>
2024-07-11 09:33:52 -04:00
</div>
</div>
<div class="drawer-side">
<label for="my-drawer" class="drawer-overlay"></label>
2024-07-18 22:12:47 -04:00
2024-07-11 09:33:52 -04:00
<ul class="menu p-4 w-80 h-full bg-base-200 text-base-content rounded-lg">
<!-- Sidebar content here -->
<div class="form-control">
2024-09-30 18:03:10 -04:00
<!-- <h3 class="text-center font-bold text-lg mb-4">Adventure Types</h3> -->
2024-08-05 09:33:21 -04:00
<form method="get">
<CategoryFilterDropdown bind:types={typeString} />
<div class="divider"></div>
2024-10-28 15:10:14 -04:00
<h3 class="text-center font-bold text-lg mb-4">{$t('adventures.sort')}</h3>
<p class="text-lg font-semibold mb-2">{$t('adventures.order_direction')}</p>
2024-07-18 22:12:47 -04:00
<div class="join">
<input
class="join-item btn btn-neutral"
type="radio"
name="order_direction"
id="asc"
value="asc"
2024-10-28 15:10:14 -04:00
aria-label={$t('adventures.ascending')}
2024-08-05 09:59:34 -04:00
checked={currentSort.order === 'asc'}
2024-07-18 22:12:47 -04:00
/>
<input
class="join-item btn btn-neutral"
type="radio"
name="order_direction"
id="desc"
value="desc"
2024-10-28 15:10:14 -04:00
aria-label={$t('adventures.descending')}
2024-08-05 09:59:34 -04:00
checked={currentSort.order === 'desc'}
2024-07-18 22:12:47 -04:00
/>
</div>
2024-07-13 10:42:42 -04:00
<br />
2024-10-28 15:10:14 -04:00
<p class="text-lg font-semibold mt-2 mb-2">{$t('adventures.order_by')}</p>
<div class="flex flex-wrap gap-2">
2024-07-18 22:12:47 -04:00
<input
2024-10-28 15:10:14 -04:00
class="btn btn-neutral text-wrap"
2024-07-18 22:12:47 -04:00
type="radio"
name="order_by"
2024-07-19 09:05:47 -04:00
id="updated_at"
value="updated_at"
2024-10-28 15:10:14 -04:00
aria-label={$t('adventures.updated')}
2024-08-05 09:59:34 -04:00
checked={currentSort.order_by === 'updated_at'}
2024-07-18 22:12:47 -04:00
/>
<input
2024-10-28 15:10:14 -04:00
class="btn btn-neutral text-wrap"
2024-07-18 22:12:47 -04:00
type="radio"
name="order_by"
id="name"
2024-10-28 15:10:14 -04:00
aria-label={$t('adventures.name')}
2024-07-18 22:12:47 -04:00
value="name"
2024-08-05 09:59:34 -04:00
checked={currentSort.order_by === 'name'}
2024-07-18 22:12:47 -04:00
/>
<input
2024-10-28 15:10:14 -04:00
class="btn btn-neutral text-wrap"
2024-07-18 22:12:47 -04:00
type="radio"
value="date"
name="order_by"
id="date"
2024-10-28 15:10:14 -04:00
aria-label={$t('adventures.date')}
2024-08-05 09:59:34 -04:00
checked={currentSort.order_by === 'date'}
2024-07-18 22:12:47 -04:00
/>
<input
2024-10-28 15:10:14 -04:00
class="btn btn-neutral text-wrap"
2024-07-18 22:12:47 -04:00
type="radio"
name="order_by"
id="rating"
2024-10-28 15:10:14 -04:00
aria-label={$t('adventures.rating')}
2024-07-18 22:12:47 -04:00
value="rating"
2024-08-05 09:59:34 -04:00
checked={currentSort.order_by === 'rating'}
2024-07-18 22:12:47 -04:00
/>
</div>
<!-- is visited true false or all -->
<p class="text-lg font-semibold mt-2 mb-2">{$t('adventures.visited')}</p>
<div class="join">
2024-07-15 12:09:20 -04:00
<input
class="join-item btn btn-neutral"
type="radio"
name="is_visited"
id="all"
value="all"
aria-label={$t('adventures.all')}
checked={currentSort.is_visited === 'all'}
2024-07-15 12:09:20 -04:00
/>
<input
class="join-item btn btn-neutral"
type="radio"
name="is_visited"
id="true"
value="true"
aria-label={$t('adventures.visited')}
checked={currentSort.is_visited === 'true'}
/>
<input
class="join-item btn btn-neutral"
type="radio"
name="is_visited"
id="false"
value="false"
aria-label={$t('adventures.not_visited')}
checked={currentSort.is_visited === 'false'}
/>
</div>
<div class="divider"></div>
<div class="form-control">
<br />
<p class="text-lg font-semibold mt-2 mb-2">{$t('adventures.sources')}</p>
<label class="label cursor-pointer">
<span class="label-text">{$t('adventures.collection_adventures')}</span>
<input
type="checkbox"
name="include_collections"
id="include_collections"
class="checkbox checkbox-primary"
checked={currentSort.includeCollections}
/>
</label>
<button type="submit" class="btn btn-success mt-4">{$t('adventures.filter')}</button>
</div>
2024-07-11 15:37:04 -04:00
</form>
2024-07-11 09:33:52 -04:00
</div>
</ul>
</div>
</div>
2024-08-06 08:50:15 -04:00
<svelte:head>
2024-10-28 15:10:14 -04:00
<title>{$t('navbar.adventures')}</title>
2024-08-06 08:50:15 -04:00
<meta name="description" content="View your completed and planned adventures." />
</svelte:head>