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';
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;
// unlinked if collection.start_date and collection.end_date are not within transportation.date and transportation.end_date. check for null in the collection dates first to avoid errors
$: {
if (collection?.start_date && collection.end_date) {
const startOutsideRange =
transportation.date & &
collection.start_date < transportation.date & &
collection.end_date < transportation.date ;
const endOutsideRange =
transportation.end_date & &
collection.start_date > transportation.end_date & &
collection.end_date > transportation.end_date;
2024-12-26 22:10:03 -05:00
unlinked = !!(
startOutsideRange ||
endOutsideRange ||
(!transportation.date & & !transportation.end_date)
);
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 -->
< div class = "flex items-center justify-between" >
< h2 class = "card-title text-lg font-semibold truncate" > { transportation . name } </ h2 >
2024-12-20 15:55:30 -05:00
< div class = "flex items-center gap-2" >
< div class = "badge badge-secondary" >
{ $t ( `transportation.modes.$ { transportation . type } `) }
< / div >
{ #if transportation . type == 'plane' && transportation . flight_number }
< div class = "badge badge-neutral-200" > { transportation . flight_number } </ div >
{ /if }
2024-12-19 18:46:52 -05:00
< / div >
< / div >
2024-12-26 21:56:14 -05:00
{ #if unlinked }
< div class = "badge badge-error" > { $t ( 'adventures.out_of_range' )} </ div >
{ /if }
2024-12-19 18:46:52 -05:00
<!-- 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 >
< p > { new Date ( transportation . date ). toLocaleString ( undefined , { timeZone : 'UTC' })} </ 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 >
< p > { new Date ( transportation . end_date ). toLocaleString ( undefined , { timeZone : 'UTC' })} </ p >
< / 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 >