2024-07-27 19:22:01 -04:00
|
|
|
<script lang="ts">
|
|
|
|
import { createEventDispatcher } from 'svelte';
|
|
|
|
import TrashCanOutline from '~icons/mdi/trash-can-outline';
|
|
|
|
import FileDocumentEdit from '~icons/mdi/file-document-edit';
|
2024-09-09 13:31:00 -04:00
|
|
|
import type { Collection, Transportation, User } from '$lib/types';
|
2024-07-27 19:22:01 -04:00
|
|
|
import { addToast } from '$lib/toasts';
|
2024-11-03 22:55:38 -05:00
|
|
|
import { t } from 'svelte-i18n';
|
2024-12-19 19:07:23 -05:00
|
|
|
import DeleteWarning from './DeleteWarning.svelte';
|
2024-12-19 18:46:52 -05:00
|
|
|
// import ArrowDownThick from '~icons/mdi/arrow-down-thick';
|
2025-03-18 17:40:32 -04:00
|
|
|
import { TRANSPORTATION_TYPES_ICONS } from '$lib';
|
2024-07-27 19:22:01 -04:00
|
|
|
|
2025-03-18 17:40:32 -04:00
|
|
|
function getTransportationIcon(type: string) {
|
|
|
|
if (type in TRANSPORTATION_TYPES_ICONS) {
|
|
|
|
return TRANSPORTATION_TYPES_ICONS[type as keyof typeof TRANSPORTATION_TYPES_ICONS];
|
|
|
|
} else {
|
|
|
|
return '🚗';
|
|
|
|
}
|
|
|
|
}
|
2024-07-27 19:22:01 -04:00
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
|
2024-07-27 21:18:15 -04:00
|
|
|
export let transportation: Transportation;
|
2024-07-29 19:19:24 -04:00
|
|
|
export let user: User | null = null;
|
2024-09-09 13:31:00 -04:00
|
|
|
export let collection: Collection | null = null;
|
2024-07-27 19:22:01 -04:00
|
|
|
|
2024-12-19 19:07:23 -05:00
|
|
|
let isWarningModalOpen: boolean = false;
|
|
|
|
|
2024-07-27 21:18:15 -04:00
|
|
|
function editTransportation() {
|
|
|
|
dispatch('edit', transportation);
|
2024-07-27 19:22:01 -04:00
|
|
|
}
|
|
|
|
|
2024-12-26 21:56:14 -05:00
|
|
|
let unlinked: boolean = false;
|
|
|
|
|
|
|
|
$: {
|
|
|
|
if (collection?.start_date && collection.end_date) {
|
2024-12-28 10:56:25 -05:00
|
|
|
// Parse transportation dates
|
|
|
|
let transportationStartDate = transportation.date
|
|
|
|
? new Date(transportation.date.split('T')[0]) // Ensure proper date parsing
|
|
|
|
: null;
|
|
|
|
let transportationEndDate = transportation.end_date
|
|
|
|
? new Date(transportation.end_date.split('T')[0])
|
|
|
|
: null;
|
|
|
|
|
|
|
|
// Parse collection dates
|
|
|
|
let collectionStartDate = new Date(collection.start_date);
|
|
|
|
let collectionEndDate = new Date(collection.end_date);
|
|
|
|
|
|
|
|
// // Debugging outputs
|
|
|
|
// console.log(
|
|
|
|
// 'Transportation Start Date:',
|
|
|
|
// transportationStartDate,
|
|
|
|
// 'Transportation End Date:',
|
|
|
|
// transportationEndDate
|
|
|
|
// );
|
|
|
|
// console.log(
|
|
|
|
// 'Collection Start Date:',
|
|
|
|
// collectionStartDate,
|
|
|
|
// 'Collection End Date:',
|
|
|
|
// collectionEndDate
|
|
|
|
// );
|
|
|
|
|
|
|
|
// Check if the collection range is outside the transportation range
|
2024-12-26 21:56:14 -05:00
|
|
|
const startOutsideRange =
|
2024-12-28 10:56:25 -05:00
|
|
|
transportationStartDate &&
|
|
|
|
collectionStartDate < transportationStartDate &&
|
|
|
|
collectionEndDate < transportationStartDate;
|
2024-12-26 21:56:14 -05:00
|
|
|
|
|
|
|
const endOutsideRange =
|
2024-12-28 10:56:25 -05:00
|
|
|
transportationEndDate &&
|
|
|
|
collectionStartDate > transportationEndDate &&
|
|
|
|
collectionEndDate > transportationEndDate;
|
2024-12-26 21:56:14 -05:00
|
|
|
|
2024-12-26 22:10:03 -05:00
|
|
|
unlinked = !!(
|
|
|
|
startOutsideRange ||
|
|
|
|
endOutsideRange ||
|
2024-12-28 10:56:25 -05:00
|
|
|
(!transportationStartDate && !transportationEndDate)
|
2024-12-26 22:10:03 -05:00
|
|
|
);
|
2024-12-26 21:56:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-27 21:18:15 -04:00
|
|
|
async function deleteTransportation() {
|
|
|
|
let res = await fetch(`/api/transportations/${transportation.id}`, {
|
|
|
|
method: 'DELETE',
|
2024-07-27 19:22:01 -04:00
|
|
|
headers: {
|
2024-07-27 21:18:15 -04:00
|
|
|
'Content-Type': 'application/json'
|
2024-07-27 19:22:01 -04:00
|
|
|
}
|
|
|
|
});
|
2024-07-27 21:18:15 -04:00
|
|
|
if (!res.ok) {
|
2024-11-03 22:55:38 -05:00
|
|
|
console.log($t('transportation.transportation_delete_error'));
|
2024-07-27 19:22:01 -04:00
|
|
|
} else {
|
2024-11-03 22:55:38 -05:00
|
|
|
addToast('info', $t('transportation.transportation_deleted'));
|
2024-12-19 19:07:23 -05:00
|
|
|
isWarningModalOpen = false;
|
2024-07-27 21:18:15 -04:00
|
|
|
dispatch('delete', transportation.id);
|
2024-07-27 19:22:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2024-12-19 19:07:23 -05:00
|
|
|
{#if isWarningModalOpen}
|
|
|
|
<DeleteWarning
|
|
|
|
title={$t('adventures.delete_transportation')}
|
|
|
|
button_text="Delete"
|
|
|
|
description={$t('adventures.transportation_delete_confirm')}
|
|
|
|
is_warning={false}
|
|
|
|
on:close={() => (isWarningModalOpen = false)}
|
|
|
|
on:confirm={deleteTransportation}
|
|
|
|
/>
|
|
|
|
{/if}
|
|
|
|
|
2024-07-27 19:22:01 -04:00
|
|
|
<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"
|
2024-07-27 19:22:01 -04:00
|
|
|
>
|
2024-12-19 18:46:52 -05:00
|
|
|
<div class="card-body space-y-4">
|
|
|
|
<!-- Title and Type -->
|
2025-04-18 00:22:50 +02:00
|
|
|
<h2 class="card-title text-lg font-semibold truncate">{transportation.name}</h2>
|
|
|
|
<div>
|
|
|
|
<div class="badge badge-secondary">
|
|
|
|
{$t(`transportation.modes.${transportation.type}`) +
|
|
|
|
' ' +
|
|
|
|
getTransportationIcon(transportation.type)}
|
2024-12-19 18:46:52 -05:00
|
|
|
</div>
|
2025-04-18 00:22:50 +02:00
|
|
|
{#if transportation.type == 'plane' && transportation.flight_number}
|
|
|
|
<div class="badge badge-neutral-200">{transportation.flight_number}</div>
|
|
|
|
{/if}
|
|
|
|
{#if unlinked}
|
|
|
|
<div class="badge badge-error">{$t('adventures.out_of_range')}</div>
|
|
|
|
{/if}
|
2024-12-19 18:46:52 -05:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- Locations -->
|
|
|
|
<div class="space-y-2">
|
2024-08-19 16:32:08 -04:00
|
|
|
{#if transportation.from_location}
|
2024-12-19 18:46:52 -05:00
|
|
|
<div class="flex items-center gap-2">
|
|
|
|
<span class="font-medium text-sm">{$t('adventures.from')}:</span>
|
|
|
|
<p class="break-words">{transportation.from_location}</p>
|
|
|
|
</div>
|
2024-08-19 16:32:08 -04:00
|
|
|
{/if}
|
2024-12-26 11:07:59 -05:00
|
|
|
{#if transportation.date}
|
2024-12-19 18:46:52 -05:00
|
|
|
<div class="flex items-center gap-2">
|
2024-12-26 11:07:59 -05:00
|
|
|
<span class="font-medium text-sm">{$t('adventures.start')}:</span>
|
2025-03-18 17:40:32 -04:00
|
|
|
<p>{new Date(transportation.date).toLocaleString()}</p>
|
2024-12-19 18:46:52 -05:00
|
|
|
</div>
|
2024-08-19 16:32:08 -04:00
|
|
|
{/if}
|
|
|
|
</div>
|
2024-12-19 18:46:52 -05:00
|
|
|
|
|
|
|
<!-- Dates -->
|
|
|
|
<div class="space-y-2">
|
2024-12-26 11:07:59 -05:00
|
|
|
{#if transportation.to_location}
|
|
|
|
<!-- <ArrowDownThick class="w-4 h-4" /> -->
|
2024-12-19 18:46:52 -05:00
|
|
|
<div class="flex items-center gap-2">
|
2024-12-26 11:07:59 -05:00
|
|
|
<span class="font-medium text-sm">{$t('adventures.to')}:</span>
|
|
|
|
|
|
|
|
<p class="break-words">{transportation.to_location}</p>
|
2024-12-19 18:46:52 -05:00
|
|
|
</div>
|
2024-08-19 16:32:08 -04:00
|
|
|
{/if}
|
|
|
|
{#if transportation.end_date}
|
2024-12-19 18:46:52 -05:00
|
|
|
<div class="flex items-center gap-2">
|
|
|
|
<span class="font-medium text-sm">{$t('adventures.end')}:</span>
|
2025-03-18 17:40:32 -04:00
|
|
|
<p>{new Date(transportation.end_date).toLocaleString()}</p>
|
2024-12-19 18:46:52 -05:00
|
|
|
</div>
|
2024-08-19 16:32:08 -04:00
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
|
2024-12-19 18:46:52 -05:00
|
|
|
<!-- Actions -->
|
2024-12-26 21:56:14 -05:00
|
|
|
{#if transportation.user_id == user?.uuid || (collection && user && collection.shared_with && collection.shared_with.includes(user.uuid))}
|
2024-07-29 19:19:24 -04:00
|
|
|
<div class="card-actions justify-end">
|
2024-12-19 18:46:52 -05:00
|
|
|
<button
|
|
|
|
class="btn btn-primary btn-sm flex items-center gap-1"
|
|
|
|
on:click={editTransportation}
|
|
|
|
title="Edit"
|
|
|
|
>
|
|
|
|
<FileDocumentEdit class="w-5 h-5" />
|
|
|
|
<span>{$t('transportation.edit')}</span>
|
|
|
|
</button>
|
|
|
|
<button
|
2024-12-19 19:07:23 -05:00
|
|
|
on:click={() => (isWarningModalOpen = true)}
|
2024-12-19 18:46:52 -05:00
|
|
|
class="btn btn-secondary btn-sm flex items-center gap-1"
|
|
|
|
title="Delete"
|
2024-07-29 19:19:24 -04:00
|
|
|
>
|
2024-12-19 18:46:52 -05:00
|
|
|
<TrashCanOutline class="w-5 h-5" />
|
|
|
|
<span>{$t('adventures.delete')}</span>
|
2024-07-29 19:19:24 -04:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
{/if}
|
2024-07-27 19:22:01 -04:00
|
|
|
</div>
|
|
|
|
</div>
|