2025-02-05 19:38:04 -05:00
< script lang = "ts" >
import { appVersion } from '$lib/config';
import { addToast } from '$lib/toasts';
2025-05-24 14:12:06 -04:00
import type { Adventure , Lodging , GeocodeSearchResult , Point , ReverseGeocode } from '$lib/types';
2025-02-15 19:44:11 -05:00
import { onMount } from 'svelte';
2025-02-05 19:38:04 -05:00
import { t } from 'svelte-i18n';
import { DefaultMarker , MapEvents , MapLibre } from 'svelte-maplibre';
2025-02-08 16:10:01 -05:00
export let item: Adventure | Lodging;
2025-02-05 19:38:04 -05:00
export let triggerMarkVisted: boolean = false;
2025-02-15 19:44:11 -05:00
export let initialLatLng: { lat : number ; lng : number } | null = null; // Used to pass the location from the map selection to the modal
2025-02-05 19:38:04 -05:00
let reverseGeocodePlace: ReverseGeocode | null = null;
let markers: Point[] = [];
let query: string = '';
let is_custom_location: boolean = false;
let willBeMarkedVisited: boolean = false;
let previousCoords: { lat : number ; lng : number } | null = null;
let old_display_name: string = '';
2025-05-24 14:12:06 -04:00
let places: GeocodeSearchResult[] = [];
2025-02-05 19:38:04 -05:00
let noPlaces: boolean = false;
2025-06-04 14:41:29 -04:00
let isNameAutoGenerated: boolean = false;
2025-02-15 19:44:11 -05:00
onMount(() => {
if (initialLatLng) {
markers = [
{
lngLat: { lng : initialLatLng.lng , lat : initialLatLng.lat } ,
name: '',
location: '',
activity_type: ''
}
];
item.latitude = initialLatLng.lat;
item.longitude = initialLatLng.lng;
reverseGeocode();
}
});
2025-02-05 19:38:04 -05:00
$: if (markers.length > 0) {
const newLat = Math.round(markers[0].lngLat.lat * 1e6) / 1e6;
const newLng = Math.round(markers[0].lngLat.lng * 1e6) / 1e6;
if (!previousCoords || previousCoords.lat !== newLat || previousCoords.lng !== newLng) {
item.latitude = newLat;
item.longitude = newLng;
previousCoords = { lat : newLat , lng : newLng } ;
reverseGeocode();
}
2025-02-23 17:15:37 -05:00
// if (!item.name) {
// item.name = markers[0].name;
// }
2025-02-05 19:38:04 -05:00
}
$: if (triggerMarkVisted && willBeMarkedVisited) {
2025-05-23 10:46:37 -04:00
displaySuccessToast(); // since the server will trigger the geocode automatically, we just need to show the toast and let the server handle the rest. It's kinda a placebo effect
2025-02-05 19:38:04 -05:00
triggerMarkVisted = false;
}
$: {
is_custom_location = Boolean(
item.location != reverseGeocodePlace?.display_name & & item.location
);
}
if (item.longitude && item.latitude) {
markers = [];
markers = [
{
lngLat: { lng : item.longitude , lat : item.latitude } ,
location: item.location || '',
name: item.name,
activity_type: ''
}
];
}
$: {
if ('visits' in item) {
willBeMarkedVisited = false; // Reset before evaluating
const today = new Date(); // Cache today's date to avoid redundant calculations
for (const visit of item.visits) {
const startDate = new Date(visit.start_date);
const endDate = visit.end_date ? new Date(visit.end_date) : null;
// If the visit has both a start date and an end date, check if it started by today
if (startDate && endDate && startDate < = today) {
willBeMarkedVisited = true;
break; // Exit the loop since we've determined the result
}
// If the visit has a start date but no end date, check if it started by today
if (startDate && !endDate && startDate < = today) {
willBeMarkedVisited = true;
break; // Exit the loop since we've determined the result
}
}
}
}
2025-05-22 21:13:31 -04:00
function displaySuccessToast() {
if (reverseGeocodePlace) {
if (reverseGeocodePlace.region) {
addToast('success', `Visit to ${ reverseGeocodePlace . region } marked`);
}
if (reverseGeocodePlace.city) {
addToast('success', `Visit to ${ reverseGeocodePlace . city } marked`);
}
}
}
2025-02-05 19:38:04 -05:00
async function markVisited() {
console.log(reverseGeocodePlace);
if (reverseGeocodePlace) {
if (!reverseGeocodePlace.region_visited && reverseGeocodePlace.region_id) {
let region_res = await fetch(`/api/visitedregion`, {
headers: { 'Content-Type' : 'application/json' } ,
method: 'POST',
body: JSON.stringify({ region : reverseGeocodePlace.region_id } )
});
if (region_res.ok) {
reverseGeocodePlace.region_visited = true;
addToast('success', `Visit to ${ reverseGeocodePlace . region } marked`);
} else {
addToast('error', `Failed to mark visit to ${ reverseGeocodePlace . region } `);
}
}
if (!reverseGeocodePlace.city_visited && reverseGeocodePlace.city_id != null) {
let city_res = await fetch(`/api/visitedcity`, {
headers: { 'Content-Type' : 'application/json' } ,
method: 'POST',
body: JSON.stringify({ city : reverseGeocodePlace.city_id } )
});
if (city_res.ok) {
reverseGeocodePlace.city_visited = true;
addToast('success', `Visit to ${ reverseGeocodePlace . city } marked`);
} else {
addToast('error', `Failed to mark visit to ${ reverseGeocodePlace . city } `);
}
}
}
}
async function addMarker(e: CustomEvent< any > ) {
markers = [];
markers = [
...markers,
{
lngLat: e.detail.lngLat,
name: '',
location: '',
activity_type: ''
}
];
console.log(markers);
}
async function geocode(e: Event | null) {
if (e) {
e.preventDefault();
}
if (!query) {
alert($t('adventures.no_location'));
return;
}
2025-05-24 14:12:06 -04:00
let res = await fetch(`/api/reverse-geocode/search/?query=${ query } `);
2025-02-05 19:38:04 -05:00
console.log(res);
2025-05-24 14:12:06 -04:00
let data = (await res.json()) as GeocodeSearchResult[];
2025-02-05 19:38:04 -05:00
places = data;
if (data.length === 0) {
noPlaces = true;
} else {
noPlaces = false;
}
}
async function reverseGeocode(force_update: boolean = false) {
let res = await fetch(
`/api/reverse-geocode/reverse_geocode/?lat=${ item . latitude } & lon=${ item . longitude } `
);
let data = await res.json();
if (data.error) {
console.log(data.error);
reverseGeocodePlace = null;
return;
}
reverseGeocodePlace = data;
console.log(reverseGeocodePlace);
console.log(is_custom_location);
if (
reverseGeocodePlace & &
reverseGeocodePlace.display_name & &
(!is_custom_location || force_update)
) {
old_display_name = reverseGeocodePlace.display_name;
item.location = reverseGeocodePlace.display_name;
2025-02-23 17:15:37 -05:00
if (reverseGeocodePlace.location_name && !item.name) {
2025-02-15 19:44:11 -05:00
item.name = reverseGeocodePlace.location_name;
}
2025-02-05 19:38:04 -05:00
}
console.log(data);
}
function clearMap() {
console.log('CLEAR');
markers = [];
2025-02-15 19:44:11 -05:00
item.latitude = null;
item.longitude = null;
2025-02-05 19:38:04 -05:00
}
< / script >
< div class = "collapse collapse-plus bg-base-200 mb-4" >
< input type = "checkbox" / >
< div class = "collapse-title text-xl font-medium" >
{ $t ( 'adventures.location_information' )}
< / div >
< div class = "collapse-content" >
<!-- <div class="grid grid - cols - 1 gap - 4 md:grid - cols - 2 lg:grid - cols - 3"> -->
< div >
< label for = "latitude" > { $t ( 'adventures.location' )} </ label >< br />
< div class = "flex items-center" >
< input
type="text"
id="location"
name="location"
bind:value={ item . location }
class="input input-bordered w-full"
/>
{ #if is_custom_location }
< button
class="btn btn-primary ml-2"
type="button"
on:click={() => ( item . location = reverseGeocodePlace ? . display_name )}
>{ $t ( 'adventures.set_to_pin' )} < /button
>
{ /if }
< / div >
< / div >
< div >
< form on:submit = { geocode } class="mt-2" >
< input
type="text"
placeholder={ $t ( 'adventures.search_for_location' )}
class="input input-bordered w-full max-w-xs mb-2"
id="search"
name="search"
bind:value={ query }
/>
< button class = "btn btn-neutral -mt-1" type = "submit" > { $t ( 'navbar.search' )} </ button >
< button class = "btn btn-neutral -mt-1" type = "button" on:click = { clearMap }
>{ $t ( 'adventures.clear_map' )} < /button
>
< / form >
< / div >
{ #if places . length > 0 }
< div class = "mt-4 max-w-full" >
< h3 class = "font-bold text-lg mb-4" > { $t ( 'adventures.search_results' )} </ h3 >
< div class = "flex flex-wrap" >
{ #each places as place }
< button
type="button"
class="btn btn-neutral mb-2 mr-2 max-w-full break-words whitespace-normal text-left"
on:click={() => {
markers = [
{
lngLat: { lng : Number ( place . lon ), lat : Number ( place . lat ) } ,
2025-05-24 14:59:58 -04:00
location: place.display_name ?? '',
name: place.name ?? '',
activity_type: place.type ?? ''
2025-02-05 19:38:04 -05:00
}
];
2025-06-04 14:41:29 -04:00
if (isNameAutoGenerated || !item.name) {
item.name = place.name ?? '';
isNameAutoGenerated = true;
}
2025-02-05 19:38:04 -05:00
}}
>
2025-05-24 14:59:58 -04:00
< span > { place . name } </ span >
< br / >
< small class = "text-xs text-neutral-300" > { place . display_name } </ small >
2025-02-05 19:38:04 -05:00
< / button >
{ /each }
< / div >
< / div >
{ :else if noPlaces }
< p class = "text-error text-lg" > { $t ( 'adventures.no_results' )} </ p >
{ /if }
<!-- </div> -->
< div >
< MapLibre
style="https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json"
class="relative aspect-[9/16] max-h-[70vh] w-full sm:aspect-video sm:max-h-full rounded-lg"
standardControls
2025-02-15 19:44:11 -05:00
zoom={ item . latitude && item . longitude ? 12 : 1 }
center={{ lng : item.longitude || 0 , lat : item.latitude || 0 }}
2025-02-05 19:38:04 -05:00
>
<!-- MapEvents gives you access to map events even from other components inside the map,
where you might not have access to the top-level `MapLibre` component. In this case
it would also work to just use on:click on the MapLibre component itself. -->
< MapEvents on:click = { addMarker } / >
{ #each markers as marker }
< DefaultMarker lngLat = { marker . lngLat } / >
{ /each }
< / MapLibre >
{ #if reverseGeocodePlace }
2025-02-08 16:10:01 -05:00
< div class = "mt-2 p-4 bg-neutral rounded-lg shadow-md" >
< h3 class = "text-lg font-bold mb-2" > { $t ( 'adventures.location_details' )} </ h3 >
< p class = "mb-1" >
< span class = "font-semibold" > { $t ( 'adventures.display_name' )} :</ span >
2025-02-05 19:38:04 -05:00
{ reverseGeocodePlace . city
2025-02-08 16:10:01 -05:00
? reverseGeocodePlace.city + ', '
: ''}{ reverseGeocodePlace . region } , { reverseGeocodePlace . country }
2025-02-05 19:38:04 -05:00
< / p >
2025-02-08 16:10:01 -05:00
< p class = "mb-1" >
< span class = "font-semibold" > { $t ( 'adventures.region' )} :</ span >
{ reverseGeocodePlace . region }
{ reverseGeocodePlace . region_visited ? '✅' : '❌' }
2025-02-05 19:38:04 -05:00
< / p >
{ #if reverseGeocodePlace . city }
2025-02-08 16:10:01 -05:00
< p class = "mb-1" >
< span class = "font-semibold" > { $t ( 'adventures.city' )} :</ span >
{ reverseGeocodePlace . city }
{ reverseGeocodePlace . city_visited ? '✅' : '❌' }
2025-02-05 19:38:04 -05:00
< / p >
{ /if }
< / div >
{ #if ! reverseGeocodePlace . region_visited || ( ! reverseGeocodePlace . city_visited && ! willBeMarkedVisited )}
2025-02-08 16:10:01 -05:00
< button type = "button" class = "btn btn-primary mt-2" on:click = { markVisited } >
2025-02-05 19:38:04 -05:00
{ $t ( 'adventures.mark_visited' )}
< / button >
{ /if }
{ #if ( willBeMarkedVisited && ! reverseGeocodePlace . region_visited && reverseGeocodePlace . region_id ) || ( ! reverseGeocodePlace . city_visited && willBeMarkedVisited && reverseGeocodePlace . city_id )}
2025-02-08 16:10:01 -05:00
< div role = "alert" class = "alert alert-info mt-2 flex items-center" >
2025-02-05 19:38:04 -05:00
< svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
2025-02-08 16:10:01 -05:00
class="h-6 w-6 shrink-0 stroke-current mr-2"
2025-02-05 19:38:04 -05:00
>
< path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
>< / path >
< / svg >
2025-02-08 16:10:01 -05:00
< span >
{ reverseGeocodePlace . city
? reverseGeocodePlace.city + ', '
: ''}{ reverseGeocodePlace . region } , { reverseGeocodePlace . country }
{ $t ( 'adventures.will_be_marked' )}
< / span >
2025-02-05 19:38:04 -05:00
< / div >
{ /if }
{ /if }
< / div >
< / div >
< / div >