1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-19 21:09:37 +02:00
AdventureLog/frontend/src/lib/components/RegionCard.svelte

83 lines
2.5 KiB
Svelte
Raw Normal View History

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();
import { t } from 'svelte-i18n';
2024-07-08 11:44:39 -04:00
export let region: Region;
export let visited: boolean | undefined;
2024-07-08 11:44:39 -04: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() {
let res = await fetch(`/api/visitedregion/`, {
headers: { 'Content-Type': 'application/json' },
2024-07-08 11:44:39 -04:00
method: 'POST',
body: JSON.stringify({ region: region.id })
2024-07-08 11:44:39 -04:00
});
if (res.ok) {
visited = true;
let data = await res.json();
addToast(
'success',
`${$t('worldtravel.visit_to')} ${region.name} ${$t('worldtravel.marked_visited')}`
);
dispatch('visit', data);
2024-07-08 11:44:39 -04:00
} else {
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() {
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;
addToast('info', `${$t('worldtravel.visit_to')} ${region.name} ${$t('worldtravel.removed')}`);
dispatch('remove', region);
2024-07-08 11:44:39 -04:00
} else {
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
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">
<h2 class="card-title overflow-ellipsis">{region.name}</h2>
<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> -->
{#if !visited && visited !== undefined}
<button class="btn btn-primary" on:click={markVisited}
>{$t('adventures.mark_visited')}</button
>
2024-07-08 11:44:39 -04:00
{/if}
{#if visited && visited !== undefined}
<button class="btn btn-warning" on:click={removeVisit}>{$t('adventures.remove')}</button>
2024-07-08 11:44:39 -04:00
{/if}
{#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>