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();
|
2025-01-09 11:11:02 -05:00
|
|
|
import { t } from 'svelte-i18n';
|
2024-07-08 11:44:39 -04:00
|
|
|
|
|
|
|
export let region: Region;
|
2025-01-18 12:57:56 -05:00
|
|
|
export let visited: boolean | undefined;
|
2024-07-08 11:44:39 -04:00
|
|
|
|
2025-01-09 11:11:02 -05:00
|
|
|
function goToCity() {
|
|
|
|
console.log(region);
|
|
|
|
goto(`/worldtravel/${region.id.split('-')[0]}/${region.id}`);
|
|
|
|
}
|
|
|
|
|
2024-07-08 11:44:39 -04:00
|
|
|
async function markVisited() {
|
2025-01-09 12:38:29 -05:00
|
|
|
let res = await fetch(`/api/visitedregion/`, {
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2024-07-08 11:44:39 -04:00
|
|
|
method: 'POST',
|
2025-01-09 12:38:29 -05:00
|
|
|
body: JSON.stringify({ region: region.id })
|
2024-07-08 11:44:39 -04:00
|
|
|
});
|
|
|
|
if (res.ok) {
|
2025-01-09 12:38:29 -05:00
|
|
|
visited = true;
|
|
|
|
let data = await res.json();
|
2025-01-09 13:53:16 -05:00
|
|
|
addToast(
|
|
|
|
'success',
|
|
|
|
`${$t('worldtravel.visit_to')} ${region.name} ${$t('worldtravel.marked_visited')}`
|
|
|
|
);
|
2025-01-09 12:38:29 -05:00
|
|
|
dispatch('visit', data);
|
2024-07-08 11:44:39 -04:00
|
|
|
} else {
|
2025-01-09 13:53:16 -05:00
|
|
|
console.error($t('worldtravel.region_failed_visited'));
|
|
|
|
addToast('error', `${$t('worldtravel.failed_to_mark_visit')} ${region.name}`);
|
2024-07-08 11:44:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
async function removeVisit() {
|
2025-01-09 12:38:29 -05:00
|
|
|
let res = await fetch(`/api/visitedregion/${region.id}`, {
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
method: 'DELETE'
|
2024-07-08 11:44:39 -04:00
|
|
|
});
|
|
|
|
if (res.ok) {
|
|
|
|
visited = false;
|
2025-01-09 13:53:16 -05:00
|
|
|
addToast('info', `${$t('worldtravel.visit_to')} ${region.name} ${$t('worldtravel.removed')}`);
|
2025-01-09 12:38:29 -05:00
|
|
|
dispatch('remove', region);
|
2024-07-08 11:44:39 -04:00
|
|
|
} else {
|
2025-01-09 13:53:16 -05:00
|
|
|
console.error($t('worldtravel.visit_remove_failed'));
|
|
|
|
addToast('error', `${$t('worldtravel.failed_to_remove_visit')} ${region.name}`);
|
2024-07-08 11:44:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</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>
|
2025-01-09 14:58:45 -05:00
|
|
|
<div>
|
|
|
|
<div class="badge badge-primary">
|
|
|
|
<p>{region.id}</p>
|
|
|
|
</div>
|
|
|
|
<div class="badge badge-neutral-300">
|
|
|
|
<p>{region.num_cities} {$t('worldtravel.cities')}</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-07-08 11:44:39 -04:00
|
|
|
<div class="card-actions justify-end">
|
|
|
|
<!-- <button class="btn btn-info" on:click={moreInfo}>More Info</button> -->
|
2025-01-18 12:57:56 -05:00
|
|
|
{#if !visited && visited !== undefined}
|
2025-01-09 11:11:02 -05:00
|
|
|
<button class="btn btn-primary" on:click={markVisited}
|
|
|
|
>{$t('adventures.mark_visited')}</button
|
|
|
|
>
|
2024-07-08 11:44:39 -04:00
|
|
|
{/if}
|
2025-01-18 12:57:56 -05:00
|
|
|
{#if visited && visited !== undefined}
|
2025-01-09 11:11:02 -05:00
|
|
|
<button class="btn btn-warning" on:click={removeVisit}>{$t('adventures.remove')}</button>
|
2024-07-08 11:44:39 -04:00
|
|
|
{/if}
|
2025-01-09 14:58:45 -05:00
|
|
|
{#if region.num_cities > 0}
|
|
|
|
<button class="btn btn-neutral-300" on:click={goToCity}
|
|
|
|
>{$t('worldtravel.view_cities')}</button
|
|
|
|
>
|
|
|
|
{/if}
|
2024-07-08 11:44:39 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|