You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
4.1 KiB
142 lines
4.1 KiB
import { CommonModule } from '@angular/common';
|
|
import { Component, Input, OnInit } from '@angular/core';
|
|
import {
|
|
FormBuilder,
|
|
FormControl,
|
|
FormGroup,
|
|
ReactiveFormsModule,
|
|
} from '@angular/forms';
|
|
import { of } from 'rxjs';
|
|
import {
|
|
catchError,
|
|
debounceTime,
|
|
distinctUntilChanged,
|
|
switchMap,
|
|
} from 'rxjs/operators';
|
|
import { AutocompleteService } from '../../services/auto-complete/auto-complete.service';
|
|
import { ExifService } from '../../services/exif/exif.service';
|
|
import { MapReloadService } from '../../services/map-reload/map-reload.service';
|
|
import { PinService } from '../../services/pin/pin.service';
|
|
import { DragDropComponent } from '../drag-drop/drag-drop.component';
|
|
@Component({
|
|
selector: 'app-add-pin-popup',
|
|
standalone: true,
|
|
imports: [ReactiveFormsModule, CommonModule, DragDropComponent],
|
|
templateUrl: './add-pin-popup.component.html',
|
|
})
|
|
export class AddPinPopupComponent implements OnInit {
|
|
form: FormGroup;
|
|
suggestions: any[] = [];
|
|
inputFocused: boolean = false;
|
|
@Input() isHomePage: boolean = false;
|
|
files: any[] = [];
|
|
isPinModalOpen: boolean = false;
|
|
|
|
constructor(
|
|
private fb: FormBuilder,
|
|
private autocompleteService: AutocompleteService,
|
|
private pinService: PinService,
|
|
private exifService: ExifService,
|
|
private mapReloadService: MapReloadService
|
|
) {
|
|
this.form = this.fb.group({
|
|
title: new FormControl(''),
|
|
description: new FormControl(''),
|
|
location: new FormControl(''),
|
|
files: new FormControl(null),
|
|
});
|
|
}
|
|
|
|
onFocus(): void {
|
|
this.inputFocused = true;
|
|
}
|
|
|
|
onBlur(): void {
|
|
setTimeout(() => {
|
|
this.inputFocused = false; // Désactiver le focus après un petit délai pour permettre un clic sur la liste
|
|
}, 200);
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
this.form
|
|
.get('location')
|
|
?.valueChanges.pipe(
|
|
debounceTime(200), // Attendre 200ms après la dernière frappe
|
|
distinctUntilChanged(), // Ignorer si la nouvelle valeur est la même que la précédente
|
|
switchMap((query) => {
|
|
const trimmedQuery = query.trim();
|
|
if (trimmedQuery.length > 2) {
|
|
return this.autocompleteService.getAddressSuggestions(trimmedQuery);
|
|
}
|
|
return of([]);
|
|
}),
|
|
catchError((error) => {
|
|
console.error('Error fetching suggestions:', error);
|
|
return of([]);
|
|
})
|
|
)
|
|
.subscribe((data) => {
|
|
this.suggestions = data;
|
|
});
|
|
}
|
|
|
|
selectSuggestion(suggestion: any): void {
|
|
const locationControl = this.form.get('location');
|
|
if (locationControl instanceof FormControl) {
|
|
locationControl.setValue(suggestion.display_name);
|
|
}
|
|
this.suggestions = [];
|
|
}
|
|
|
|
async onFilesReceived(files: FileList): Promise<void> {
|
|
this.files = Array.from(files);
|
|
|
|
for (let i = 0; i < this.files.length; i++) {
|
|
try {
|
|
const data = await this.exifService.getLocation(this.files[i]);
|
|
if (data.latitude !== undefined && data.longitude !== undefined) {
|
|
const address = await this.autocompleteService
|
|
.getAddressFromCoordinates(data.latitude, data.longitude)
|
|
.toPromise();
|
|
if (address) {
|
|
console.error('Data : ' + JSON.stringify(address));
|
|
this.form.get('location')?.setValue(address.display_name);
|
|
break;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error : ' + error);
|
|
}
|
|
}
|
|
}
|
|
|
|
submitForm(): void {
|
|
if (this.form.valid) {
|
|
this.files = this.files.map((file) => {
|
|
return file.name; //TODO: Mettre le hash du fichier
|
|
});
|
|
|
|
const pinData = {
|
|
...this.form.value,
|
|
files: this.files,
|
|
};
|
|
|
|
this.pinService.addPin(pinData).subscribe(() => {
|
|
this.mapReloadService.requestReload(); // Demander le rechargement de la carte
|
|
this.closePinModal();
|
|
this.form.reset(); // Réinitialiser le formulaire après soumission
|
|
});
|
|
} else {
|
|
console.error('Le formulaire est invalide');
|
|
}
|
|
}
|
|
|
|
openPinModal() {
|
|
this.isPinModalOpen = true;
|
|
}
|
|
|
|
closePinModal() {
|
|
this.isPinModalOpen = false;
|
|
}
|
|
}
|