1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-04 12:45:17 +02:00
AdventureLog/frontend/src/routes/search/+page.svelte

199 lines
5 KiB
Svelte
Raw Normal View History

2024-07-17 17:36:05 -04:00
<script lang="ts">
import AdventureCard from '$lib/components/AdventureCard.svelte';
import NotFound from '$lib/components/NotFound.svelte';
import type { Adventure, OpenStreetMapPlace } from '$lib/types';
import { onMount } from 'svelte';
2024-07-17 17:36:05 -04:00
import type { PageData } from './$types';
2024-07-22 10:08:42 -04:00
import EditAdventure from '$lib/components/EditAdventure.svelte';
2024-07-22 10:56:18 -04:00
import { appVersion } from '$lib/config';
import { goto } from '$app/navigation';
2024-07-17 17:36:05 -04:00
export let data: PageData;
2024-08-13 12:03:18 -04:00
function deleteAdventure(event: CustomEvent<string>) {
myAdventures = myAdventures.filter((adventure) => adventure.id !== event.detail);
2024-07-17 22:06:55 -04:00
}
let osmResults: OpenStreetMapPlace[] = [];
let myAdventures: Adventure[] = [];
let publicAdventures: Adventure[] = [];
let query: string | null = '';
let property: string = 'all';
// on chage of property, console log the property
function filterByProperty() {
let url = new URL(window.location.href);
url.searchParams.set('property', property);
goto(url.toString(), { invalidateAll: true });
}
onMount(() => {
const urlParams = new URLSearchParams(window.location.search);
query = urlParams.get('query');
});
2024-07-17 17:36:05 -04:00
console.log(data);
$: {
if (data.props) {
myAdventures = data.props.adventures;
publicAdventures = data.props.adventures;
if (data.user?.pk != null) {
myAdventures = myAdventures.filter(
(adventure) => adventure.user_id === data.user?.pk ?? -1
);
} else {
myAdventures = [];
}
publicAdventures = publicAdventures.filter(
(adventure) => adventure.user_id !== data.user?.pk
);
if (data.props.osmData) {
osmResults = data.props.osmData;
}
}
2024-07-17 17:36:05 -04:00
}
2024-07-22 10:08:42 -04:00
let adventureToEdit: Adventure;
let isEditModalOpen: boolean = false;
function editAdventure(event: CustomEvent<Adventure>) {
adventureToEdit = event.detail;
isEditModalOpen = true;
}
function saveEdit(event: CustomEvent<Adventure>) {
myAdventures = myAdventures.map((adventure) => {
2024-07-22 10:08:42 -04:00
if (adventure.id === event.detail.id) {
return event.detail;
}
return adventure;
});
isEditModalOpen = false;
}
2024-07-17 17:36:05 -04:00
</script>
2024-07-22 10:08:42 -04:00
{#if isEditModalOpen}
<EditAdventure
{adventureToEdit}
on:close={() => (isEditModalOpen = false)}
on:saveEdit={saveEdit}
/>
{/if}
{#if myAdventures.length === 0 && osmResults.length === 0}
2024-07-18 14:55:23 -04:00
<NotFound error={data.error} />
{/if}
{#if myAdventures.length !== 0}
<h2 class="text-center font-bold text-2xl mb-4">AdventureLog Results</h2>
<div class="flex items-center justify-center mt-2 mb-2">
<div class="join">
<input
class="join-item btn"
type="radio"
name="filter"
aria-label="All"
id="all"
checked
on:change={() => (property = 'all')}
/>
<input
class="join-item btn"
type="radio"
name="filter"
aria-label="Name"
id="name"
on:change={() => (property = 'name')}
/>
<input
class="join-item btn"
type="radio"
name="filter"
aria-label="Type"
id="type"
on:change={() => (property = 'type')}
/>
<input
class="join-item btn"
type="radio"
name="filter"
aria-label="Location"
id="location"
on:change={() => (property = 'location')}
/>
<input
class="join-item btn"
type="radio"
name="filter"
aria-label="Description"
id="description"
on:change={() => (property = 'description')}
/>
<input
class="join-item btn"
type="radio"
name="filter"
aria-label="Activity Types"
id="activity_types"
on:change={() => (property = 'activity_types')}
/>
</div>
<button class="btn btn-primary ml-2" type="button" on:click={filterByProperty}>Filter</button>
</div>
{/if}
{#if myAdventures.length > 0}
<h2 class="text-center font-bold text-2xl mb-4">My Adventures</h2>
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
{#each myAdventures as adventure}
2024-07-18 18:37:46 -04:00
<AdventureCard
user={data.user}
type={adventure.type}
{adventure}
on:delete={deleteAdventure}
2024-07-22 10:08:42 -04:00
on:edit={editAdventure}
2024-07-18 18:37:46 -04:00
/>
{/each}
</div>
{/if}
{#if publicAdventures.length > 0}
<h2 class="text-center font-bold text-2xl mb-4">Public Adventures</h2>
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
{#each publicAdventures as adventure}
<AdventureCard
user={null}
type={adventure.type}
{adventure}
on:delete={deleteAdventure}
on:edit={editAdventure}
/>
{/each}
</div>
{/if}
{#if myAdventures.length > 0 && osmResults.length > 0 && publicAdventures.length > 0}
<div class="divider"></div>
{/if}
{#if osmResults.length > 0}
<h2 class="text-center font-bold mt-2 text-2xl mb-4">Online Results</h2>
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
{#each osmResults as result}
2024-07-19 09:05:47 -04:00
<div class="bg-base-300 rounded-lg shadow-md p-4 w-96 mb-2">
<h2 class="text-xl font-bold">{result.display_name}</h2>
<p>{result.type}</p>
<p>{result.lat}, {result.lon}</p>
</div>
{/each}
</div>
2024-07-17 17:36:05 -04:00
{/if}
2024-08-06 08:50:15 -04:00
<svelte:head>
<title>Search{query ? `: ${query}` : ''}</title>
<meta name="description" content="Search your adventures." />
</svelte:head>