2024-07-08 11:44:39 -04:00
|
|
|
<script lang="ts">
|
|
|
|
import { goto } from '$app/navigation';
|
|
|
|
import { addToast } from '$lib/toasts';
|
|
|
|
import type { Region, VisitedRegion } from '$lib/types';
|
|
|
|
import { createEventDispatcher } from 'svelte';
|
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
|
|
|
|
export let region: Region;
|
|
|
|
export let visited: boolean;
|
|
|
|
|
|
|
|
export let visit_id: number | undefined | null;
|
|
|
|
|
|
|
|
async function markVisited() {
|
|
|
|
let res = await fetch(`/worldtravel?/markVisited`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({ regionId: region.id })
|
|
|
|
});
|
|
|
|
if (res.ok) {
|
|
|
|
// visited = true;
|
|
|
|
const result = await res.json();
|
|
|
|
const data = JSON.parse(result.data);
|
|
|
|
if (data[1] !== undefined) {
|
|
|
|
console.log('New adventure created with id:', data[3]);
|
|
|
|
let visit_id = data[3];
|
|
|
|
let region_id = data[5];
|
|
|
|
let user_id = data[4];
|
|
|
|
|
|
|
|
let newVisit: VisitedRegion = {
|
|
|
|
id: visit_id,
|
|
|
|
region: region_id,
|
2024-11-16 22:31:39 -05:00
|
|
|
user_id: user_id,
|
|
|
|
longitude: 0,
|
|
|
|
latitude: 0,
|
|
|
|
name: ''
|
2024-07-08 11:44:39 -04:00
|
|
|
};
|
|
|
|
addToast('success', `Visit to ${region.name} marked`);
|
|
|
|
dispatch('visit', newVisit);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.error('Failed to mark region as visited');
|
2024-08-17 08:08:46 -04:00
|
|
|
addToast('error', `Failed to mark visit to ${region.name}`);
|
2024-07-08 11:44:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
async function removeVisit() {
|
|
|
|
let res = await fetch(`/worldtravel?/removeVisited`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({ visitId: visit_id })
|
|
|
|
});
|
|
|
|
if (res.ok) {
|
|
|
|
visited = false;
|
|
|
|
addToast('info', `Visit to ${region.name} removed`);
|
2024-07-10 17:27:43 -04:00
|
|
|
dispatch('remove', null);
|
2024-07-08 11:44:39 -04:00
|
|
|
} else {
|
|
|
|
console.error('Failed to remove visit');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div
|
2024-09-06 23:35:48 -04:00
|
|
|
class="card w-full max-w-xs sm:max-w-sm md:max-w-md lg:max-w-md xl:max-w-md bg-neutral text-neutral-content shadow-xl overflow-hidden"
|
2024-07-08 11:44:39 -04:00
|
|
|
>
|
|
|
|
<div class="card-body">
|
2024-10-13 23:23:32 -04:00
|
|
|
<h2 class="card-title overflow-ellipsis">{region.name}</h2>
|
2024-07-08 11:44:39 -04:00
|
|
|
<p>{region.id}</p>
|
|
|
|
<div class="card-actions justify-end">
|
|
|
|
<!-- <button class="btn btn-info" on:click={moreInfo}>More Info</button> -->
|
|
|
|
{#if !visited}
|
|
|
|
<button class="btn btn-primary" on:click={markVisited}>Mark Visited</button>
|
|
|
|
{/if}
|
|
|
|
{#if visited}
|
|
|
|
<button class="btn btn-warning" on:click={removeVisit}>Remove</button>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|