add google api and auto complete in custom location part

This commit is contained in:
Chaebean Yang 2025-06-06 04:12:07 +09:00
parent 3caa21522e
commit 82d7418567
2 changed files with 61 additions and 9 deletions

View File

@ -2,19 +2,70 @@
import '../../app.css'; import '../../app.css';
import Button from './Button.svelte'; import Button from './Button.svelte';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { Loader } from '@googlemaps/js-api-loader';
export let showPopup = false; export let showPopup = false;
export let locations = [];
export let onAddMemory = () => {}; export let onAddMemory = () => {};
export let onCancel = () => {}; export let onCancel = () => {};
let destination = "";
let lastSelectedPlaceName = "";
let startDate = "";
let endDate = "";
let destinationError = false;
let startDateError = false;
let endDateError = false;
let dateOrderError = false;
let destinationInput: HTMLDivElement;
let autocomplete: google.maps.places.Autocomplete | null = null;
let isGoogleLoaded = false;
let dragActive = false;
let selectedLocation = ''; let selectedLocation = '';
let customLocation = ''; let customLocation = '';
let customLocationInput: HTMLInputElement;
let images = []; let images = [];
let dragActive = false;
let startDate = ''; let showLocationError = false;
let endDate = ''; let showImageError = false;
const GOOGLE_PLACES_API_KEY = import.meta.env.VITE_GOOGLE_PLACES_API_KEY;
onMount(async () => {
if (!GOOGLE_PLACES_API_KEY) {
console.error('Google Maps API key is missing');
return;
}
const loader = new Loader({
apiKey: GOOGLE_PLACES_API_KEY,
version: "weekly",
libraries: ["places"],
language: 'en'
});
try {
await loader.importLibrary("places");
isGoogleLoaded = true;
} catch (error) {
console.error('Error loading Places Autocomplete:', error);
}
});
$: if (isGoogleLoaded && isCustomLocation() && customLocationInput) {
const autocompleteCustom = new google.maps.places.Autocomplete(customLocationInput, {
types: ['(regions)']
});
autocompleteCustom.setFields(['name', 'formatted_address']);
autocompleteCustom.addListener('place_changed', () => {
const place = autocompleteCustom.getPlace();
if (place.name) {
customLocation = place.name;
showLocationError = false;
}
});
}
function handleFiles(files) { function handleFiles(files) {
for (const file of files) { for (const file of files) {
@ -70,9 +121,6 @@
images = images.filter(img => img !== imageToRemove); images = images.filter(img => img !== imageToRemove);
} }
let showLocationError = false;
let showImageError = false;
function handleAddMemory() { function handleAddMemory() {
showLocationError = selectedLocation === '' || (selectedLocation === 'custom' && customLocation.trim() === ''); showLocationError = selectedLocation === '' || (selectedLocation === 'custom' && customLocation.trim() === '');
showImageError = images.length === 0; showImageError = images.length === 0;
@ -83,6 +131,9 @@
onAddMemory({ location: finalLocation, images, startDate, endDate }); onAddMemory({ location: finalLocation, images, startDate, endDate });
reset(); reset();
} }
// needs to link with plan data set
const locations = ['Paris', 'Tokyo', 'New York'];
</script> </script>
{#if showPopup} {#if showPopup}
@ -107,6 +158,7 @@
<input <input
type="text" type="text"
id="custom-location" id="custom-location"
bind:this={customLocationInput}
bind:value={customLocation} bind:value={customLocation}
placeholder="Type your location" placeholder="Type your location"
/> />
@ -114,6 +166,7 @@
<p class="error-message">Please enter a location.</p> <p class="error-message">Please enter a location.</p>
{/if} {/if}
</div> </div>
<div class="date-group"> <div class="date-group">
<div class="input-form"> <div class="input-form">
<label for="start-date">Start Date</label> <label for="start-date">Start Date</label>
@ -183,7 +236,6 @@
</div> </div>
{/if} {/if}
<style> <style>
*, *::before, *::after { *, *::before, *::after {
box-sizing: border-box; box-sizing: border-box;

View File

@ -278,7 +278,7 @@
} }
.error-message { .error-message {
color: var(--memory-600); color: var(--memory-500);
margin: 0.5rem 0 0 0; margin: 0.5rem 0 0 0;
font-size: 0.8rem; font-size: 0.8rem;
} }