|
|
|
@ -65,6 +65,8 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
|
|
|
title: new FormControl(''),
|
|
|
|
|
description: new FormControl(''),
|
|
|
|
|
location: new FormControl(''),
|
|
|
|
|
complete_address: new FormControl(''),
|
|
|
|
|
coordinates: new FormControl<number[]>([]),
|
|
|
|
|
files: new FormControl(null),
|
|
|
|
|
date: new FormControl(''),
|
|
|
|
|
});
|
|
|
|
@ -85,43 +87,12 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
|
|
|
this.form.patchValue({
|
|
|
|
|
title: this.pin?.title || '',
|
|
|
|
|
description: this.pin?.description || '',
|
|
|
|
|
location: "Chargement de l'adresse...",
|
|
|
|
|
location: this.pin?.complete_address || '',
|
|
|
|
|
complete_address: this.pin?.complete_address || '',
|
|
|
|
|
coordinates: this.pin?.location || [],
|
|
|
|
|
date: this.pin?.date || '',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Vérifier si nous avons des coordonnées valides dans pin.location
|
|
|
|
|
if (
|
|
|
|
|
this.pin?.location &&
|
|
|
|
|
Array.isArray(this.pin.location) &&
|
|
|
|
|
this.pin.location.length >= 2
|
|
|
|
|
) {
|
|
|
|
|
const lat = this.pin.location[0];
|
|
|
|
|
const lon = this.pin.location[1];
|
|
|
|
|
|
|
|
|
|
if (lat !== undefined && lon !== undefined) {
|
|
|
|
|
// Récupérer l'adresse à partir des coordonnées
|
|
|
|
|
this.locationSubscription = this.autocompleteService
|
|
|
|
|
.getAddressFromCoordinates(lat, lon)
|
|
|
|
|
.pipe(take(1))
|
|
|
|
|
.subscribe(
|
|
|
|
|
(address) => {
|
|
|
|
|
if (address && address.display_name) {
|
|
|
|
|
this.form.patchValue({ location: address.display_name });
|
|
|
|
|
} else {
|
|
|
|
|
this.form.patchValue({ location: `${lat}, ${lon}` });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
console.error(
|
|
|
|
|
"Erreur lors de la récupération de l'adresse:",
|
|
|
|
|
error
|
|
|
|
|
);
|
|
|
|
|
this.form.patchValue({ location: `${lat}, ${lon}` });
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// S'abonner aux changements d'état du modal
|
|
|
|
|
this.modalOpenSubscription = this.modalService
|
|
|
|
|
.getModalState(this.modalId)
|
|
|
|
@ -147,7 +118,7 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
|
|
|
debounceTime(300), // Attendre 300ms après la dernière frappe
|
|
|
|
|
distinctUntilChanged(), // Ignorer si la nouvelle valeur est la même que la précédente
|
|
|
|
|
switchMap((query) => {
|
|
|
|
|
// Vérifier que query est une chaîne de caractères
|
|
|
|
|
// Vérifier que query est une chaîne de caractères
|
|
|
|
|
if (typeof query !== 'string') {
|
|
|
|
|
return of([]);
|
|
|
|
|
}
|
|
|
|
@ -204,6 +175,8 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
|
|
|
const locationControl = this.form.get('location');
|
|
|
|
|
if (locationControl instanceof FormControl) {
|
|
|
|
|
locationControl.setValue(suggestion.display_name);
|
|
|
|
|
this.form.get('complete_address')?.setValue(suggestion.display_name);
|
|
|
|
|
this.form.get('coordinates')?.setValue([suggestion.lat, suggestion.lon]);
|
|
|
|
|
}
|
|
|
|
|
this.suggestions = [];
|
|
|
|
|
}
|
|
|
|
@ -224,17 +197,16 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
|
|
|
|
|
|
|
|
if (address) {
|
|
|
|
|
this.form.get('location')?.setValue(address.display_name);
|
|
|
|
|
this.form.get('complete_address')?.setValue(address.display_name);
|
|
|
|
|
this.form.get('coordinates')?.setValue([data.latitude, data.longitude]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} catch (addressError) {
|
|
|
|
|
console.error(
|
|
|
|
|
"Erreur lors de la récupération de l'adresse:",
|
|
|
|
|
addressError
|
|
|
|
|
);
|
|
|
|
|
console.error("Erreur lors de la récupération de l'adresse:", addressError);
|
|
|
|
|
// Utiliser les coordonnées brutes en cas d'échec
|
|
|
|
|
this.form
|
|
|
|
|
.get('location')
|
|
|
|
|
?.setValue(`${data.latitude}, ${data.longitude}`);
|
|
|
|
|
this.form.get('location')?.setValue(`${data.latitude}, ${data.longitude}`);
|
|
|
|
|
this.form.get('complete_address')?.setValue(`${data.latitude}, ${data.longitude}`);
|
|
|
|
|
this.form.get('coordinates')?.setValue([data.latitude, data.longitude]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
@ -245,23 +217,22 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
|
|
|
|
|
|
|
|
submitForm(): void {
|
|
|
|
|
if (this.form.valid) {
|
|
|
|
|
this.files = this.files.map((file) => {
|
|
|
|
|
return file.name; //TODO: Mettre le hash du fichier
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const coordinates = this.form.get('coordinates')?.value;
|
|
|
|
|
const pinData = {
|
|
|
|
|
...this.form.value,
|
|
|
|
|
files: this.files,
|
|
|
|
|
user_id: this.pin.user_id,
|
|
|
|
|
date: this.form.get('date')?.value || null,
|
|
|
|
|
location: coordinates || this.pin.location, // Utiliser les coordonnées pour location
|
|
|
|
|
complete_address: this.form.get('complete_address')?.value || this.form.get('location')?.value,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Supprimer le champ coordinates qui n'est pas dans le modèle Pin
|
|
|
|
|
delete pinData.coordinates;
|
|
|
|
|
|
|
|
|
|
this.pinService.updatePin(this.pin.id, pinData).subscribe(() => {
|
|
|
|
|
this.mapReloadService.requestReload(); // Demander le rechargement de la carte
|
|
|
|
|
this.mapReloadService.requestReload();
|
|
|
|
|
this.closePinModal();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
console.error('Le formulaire est invalide');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|