Now complete address is sent to API to avoid multiple nominatim requests
continuous-integration/drone/push Build is passing Details

timeline
Alix JEUDI--LEMOINE 2 weeks ago
parent e030dadca4
commit 04a1d01904

@ -50,6 +50,8 @@ export class AddPinPopupComponent implements OnInit {
title: new FormControl(''), title: new FormControl(''),
description: new FormControl(''), description: new FormControl(''),
location: new FormControl(''), location: new FormControl(''),
complete_address: new FormControl(''),
coordinates: new FormControl<number[]>([]),
files: new FormControl(null), files: new FormControl(null),
date: new FormControl(''), date: new FormControl(''),
}); });
@ -106,6 +108,8 @@ export class AddPinPopupComponent implements OnInit {
const locationControl = this.form.get('location'); const locationControl = this.form.get('location');
if (locationControl instanceof FormControl) { if (locationControl instanceof FormControl) {
locationControl.setValue(suggestion.display_name); 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 = []; this.suggestions = [];
} }
@ -123,6 +127,8 @@ export class AddPinPopupComponent implements OnInit {
if (address) { if (address) {
console.error('Data : ' + JSON.stringify(address)); console.error('Data : ' + JSON.stringify(address));
this.form.get('location')?.setValue(address.display_name); 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; break;
} }
} }
@ -137,32 +143,26 @@ export class AddPinPopupComponent implements OnInit {
} }
submitForm(): void { submitForm(): void {
// if (this.form.valid) {
// this.files = this.files.map((file) => {
// // return file.name; //TODO: Mettre le hash du fichier
// this.imageService.postImage(file).subscribe((response) => {
// // console.log('Image uploaded:', response);
// let imageId = response.json();
// return imageId.id;
// });
// });
// console.log('Files : ' + JSON.stringify(this.files));
if (this.form.valid) { if (this.form.valid) {
const uploadObservables = this.files.map((file) => const uploadObservables = this.files.map((file) =>
this.imageService.postImage(file) this.imageService.postImage(file)
); );
forkJoin(uploadObservables).subscribe((responses) => { forkJoin(uploadObservables).subscribe((responses) => {
// responses est un tableau de réponses API this.files = responses.map((res: any) => res.id);
this.files = responses.map((res: any) => res.id); // ou res.json().id si nécessaire
const coordinates = this.form.get('coordinates')?.value;
const pinData = { const pinData = {
...this.form.value, ...this.form.value,
files: this.files, files: this.files,
date: this.form.get('date')?.value || null, date: this.form.get('date')?.value || null,
location: coordinates || [0, 0], // 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;
console.log('Files : ' + JSON.stringify(this.files)); console.log('Files : ' + JSON.stringify(this.files));
console.log('Pin Data : ' + JSON.stringify(pinData)); console.log('Pin Data : ' + JSON.stringify(pinData));

@ -65,6 +65,8 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
title: new FormControl(''), title: new FormControl(''),
description: new FormControl(''), description: new FormControl(''),
location: new FormControl(''), location: new FormControl(''),
complete_address: new FormControl(''),
coordinates: new FormControl<number[]>([]),
files: new FormControl(null), files: new FormControl(null),
date: new FormControl(''), date: new FormControl(''),
}); });
@ -85,43 +87,12 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
this.form.patchValue({ this.form.patchValue({
title: this.pin?.title || '', title: this.pin?.title || '',
description: this.pin?.description || '', 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 || '', 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 // S'abonner aux changements d'état du modal
this.modalOpenSubscription = this.modalService this.modalOpenSubscription = this.modalService
.getModalState(this.modalId) .getModalState(this.modalId)
@ -204,6 +175,8 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
const locationControl = this.form.get('location'); const locationControl = this.form.get('location');
if (locationControl instanceof FormControl) { if (locationControl instanceof FormControl) {
locationControl.setValue(suggestion.display_name); 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 = []; this.suggestions = [];
} }
@ -224,17 +197,16 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
if (address) { if (address) {
this.form.get('location')?.setValue(address.display_name); 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; break;
} }
} catch (addressError) { } catch (addressError) {
console.error( console.error("Erreur lors de la récupération de l'adresse:", addressError);
"Erreur lors de la récupération de l'adresse:",
addressError
);
// Utiliser les coordonnées brutes en cas d'échec // Utiliser les coordonnées brutes en cas d'échec
this.form this.form.get('location')?.setValue(`${data.latitude}, ${data.longitude}`);
.get('location') this.form.get('complete_address')?.setValue(`${data.latitude}, ${data.longitude}`);
?.setValue(`${data.latitude}, ${data.longitude}`); this.form.get('coordinates')?.setValue([data.latitude, data.longitude]);
} }
} }
} catch (error) { } catch (error) {
@ -245,23 +217,22 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
submitForm(): void { submitForm(): void {
if (this.form.valid) { if (this.form.valid) {
this.files = this.files.map((file) => { const coordinates = this.form.get('coordinates')?.value;
return file.name; //TODO: Mettre le hash du fichier
});
const pinData = { const pinData = {
...this.form.value, ...this.form.value,
files: this.files, files: this.files,
user_id: this.pin.user_id,
date: this.form.get('date')?.value || null, 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.pinService.updatePin(this.pin.id, pinData).subscribe(() => {
this.mapReloadService.requestReload(); // Demander le rechargement de la carte this.mapReloadService.requestReload();
this.closePinModal(); this.closePinModal();
}); });
} else {
console.error('Le formulaire est invalide');
} }
} }

@ -91,28 +91,13 @@ export class LeafletMapComponent implements OnInit {
private loadCountriesForFiltrers(pins: Pin[]): void { private loadCountriesForFiltrers(pins: Pin[]): void {
const countrySet = new Set<string>(); const countrySet = new Set<string>();
const requests = pins.map((pin) => const requests = pins.map((pin: Pin) => {
this.autocompleteService const country = this.extractLastFromDisplayName(pin.complete_address);
.getAddressFromCoordinates(pin.location[0], pin.location[1]) if (country) {
.toPromise() this.pinCountries[pin.id] = country;
.then((res: any) => { countrySet.add(country);
const address = res?.address; }
const country = });
address?.country ||
this.extractLastFromDisplayName(res?.display_name);
if (country) {
this.pinCountries[pin.id] = country;
countrySet.add(country);
}
})
.catch((err: any) => {
console.error(
'Erreur lors de la récupération du pays pour le pin',
pin.id,
err
);
})
);
Promise.all(requests).then(() => { Promise.all(requests).then(() => {
this.availableCountries = Array.from(countrySet).sort(); this.availableCountries = Array.from(countrySet).sort();

@ -1,6 +1,7 @@
export interface Pin { export interface Pin {
id: string; id: string;
location: number[]; location: number[];
complete_address: string;
title: string; title: string;
files: string[]; files: string[];
description: string; description: string;

@ -29,46 +29,20 @@ export class PinService {
return this.http.get<any>(url, { headers }); return this.http.get<any>(url, { headers });
} }
addPin(pin: { addPin(pin: Pin) {
title: string;
description: string;
location: string;
files: string[];
date: string;
}) {
const url = `${this.apiURL}/pin/add`; const url = `${this.apiURL}/pin/add`;
const headers = new HttpHeaders({ const headers = new HttpHeaders({
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: 'Bearer ' + localStorage.getItem('auth_token'), Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
}); });
return this.autoCompleteService.getAdressCoordinates(pin.location).pipe( return this.http.post<any>(
switchMap((response: any) => { url, pin, { headers }
const coords: [string, string] = [response[0].lat, response[0].lon];
return this.http.post<any>(
url,
{
title: pin.title,
description: pin.description,
location: coords,
files: pin.files,
user_id: '',
date: pin.date,
},
{ headers }
);
})
); );
} }
updatePin( updatePin(
id: string, id: string,
pin: { pin: Pin
title: string;
description: string;
location: string;
files: any[];
user_id: string;
}
) { ) {
const url = `${this.apiURL}/pin/${id}`; const url = `${this.apiURL}/pin/${id}`;
const headers = new HttpHeaders({ const headers = new HttpHeaders({
@ -77,21 +51,8 @@ export class PinService {
}); });
// Obtenir les coordonnées GPS à partir de l'adresse // Obtenir les coordonnées GPS à partir de l'adresse
return this.autoCompleteService.getAdressCoordinates(pin.location).pipe( return this.http.patch<any>(
switchMap((response: any) => { url, pin, { headers }
const coords: [string, string] = [response[0].lat, response[0].lon];
return this.http.patch<any>(
url,
{
title: pin.title,
description: pin.description,
location: coords,
files: pin.files,
user_id: pin.user_id,
},
{ headers }
);
})
); );
} }

Loading…
Cancel
Save