|
|
@ -13,7 +13,7 @@ import {
|
|
|
|
ReactiveFormsModule,
|
|
|
|
ReactiveFormsModule,
|
|
|
|
} from '@angular/forms';
|
|
|
|
} from '@angular/forms';
|
|
|
|
import { NavigationEnd, Router } from '@angular/router';
|
|
|
|
import { NavigationEnd, Router } from '@angular/router';
|
|
|
|
import { of, Subscription } from 'rxjs';
|
|
|
|
import { forkJoin, of, Subscription } from 'rxjs';
|
|
|
|
import {
|
|
|
|
import {
|
|
|
|
catchError,
|
|
|
|
catchError,
|
|
|
|
debounceTime,
|
|
|
|
debounceTime,
|
|
|
@ -25,6 +25,7 @@ import {
|
|
|
|
import { Pin } from '../../model/Pin';
|
|
|
|
import { Pin } from '../../model/Pin';
|
|
|
|
import { AutocompleteService } from '../../services/auto-complete/auto-complete.service';
|
|
|
|
import { AutocompleteService } from '../../services/auto-complete/auto-complete.service';
|
|
|
|
import { ExifService } from '../../services/exif/exif.service';
|
|
|
|
import { ExifService } from '../../services/exif/exif.service';
|
|
|
|
|
|
|
|
import { ImageService } from '../../services/image/image.service';
|
|
|
|
import { MapReloadService } from '../../services/map-reload/map-reload.service';
|
|
|
|
import { MapReloadService } from '../../services/map-reload/map-reload.service';
|
|
|
|
import { ModalService } from '../../services/modal/modal.service';
|
|
|
|
import { ModalService } from '../../services/modal/modal.service';
|
|
|
|
import { PinService } from '../../services/pin/pin.service';
|
|
|
|
import { PinService } from '../../services/pin/pin.service';
|
|
|
@ -58,7 +59,8 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
|
|
private exifService: ExifService,
|
|
|
|
private exifService: ExifService,
|
|
|
|
private modalService: ModalService,
|
|
|
|
private modalService: ModalService,
|
|
|
|
private router: Router,
|
|
|
|
private router: Router,
|
|
|
|
private mapReloadService: MapReloadService
|
|
|
|
private mapReloadService: MapReloadService,
|
|
|
|
|
|
|
|
private imageService: ImageService
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
// Initialiser le formulaire avec des valeurs par défaut
|
|
|
|
// Initialiser le formulaire avec des valeurs par défaut
|
|
|
|
this.form = this.fb.group({
|
|
|
|
this.form = this.fb.group({
|
|
|
@ -86,9 +88,15 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
|
|
title: this.pin?.title || '',
|
|
|
|
title: this.pin?.title || '',
|
|
|
|
description: this.pin?.description || '',
|
|
|
|
description: this.pin?.description || '',
|
|
|
|
location: "Chargement de l'adresse...",
|
|
|
|
location: "Chargement de l'adresse...",
|
|
|
|
date: this.pin?.date || '',
|
|
|
|
files: this.pin?.files || [],
|
|
|
|
|
|
|
|
date: this.pin?.date
|
|
|
|
|
|
|
|
? new Date(this.pin.date).toISOString().split('T')[0]
|
|
|
|
|
|
|
|
: '',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Initialiser les fichiers existants
|
|
|
|
|
|
|
|
this.files = this.pin?.files || [];
|
|
|
|
|
|
|
|
|
|
|
|
// Vérifier si nous avons des coordonnées valides dans pin.location
|
|
|
|
// Vérifier si nous avons des coordonnées valides dans pin.location
|
|
|
|
if (
|
|
|
|
if (
|
|
|
|
this.pin?.location &&
|
|
|
|
this.pin?.location &&
|
|
|
@ -209,14 +217,14 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async onFilesReceived(files: FileList): Promise<void> {
|
|
|
|
async onFilesReceived(files: FileList): Promise<void> {
|
|
|
|
this.files = Array.from(files);
|
|
|
|
// Ajouter les nouveaux fichiers à la liste existante
|
|
|
|
|
|
|
|
this.files = [...this.files, ...Array.from(files)];
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this.files.length; i++) {
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const data = await this.exifService.getLocation(this.files[i]);
|
|
|
|
const data = await this.exifService.getLocation(files[i]);
|
|
|
|
if (data.latitude !== undefined && data.longitude !== undefined) {
|
|
|
|
if (data.latitude !== undefined && data.longitude !== undefined) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// Utiliser pipe(take(1)) pour s'assurer que l'observable se termine
|
|
|
|
|
|
|
|
const address = await this.autocompleteService
|
|
|
|
const address = await this.autocompleteService
|
|
|
|
.getAddressFromCoordinates(data.latitude, data.longitude)
|
|
|
|
.getAddressFromCoordinates(data.latitude, data.longitude)
|
|
|
|
.pipe(take(1))
|
|
|
|
.pipe(take(1))
|
|
|
@ -231,7 +239,6 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
|
|
"Erreur lors de la récupération de l'adresse:",
|
|
|
|
"Erreur lors de la récupération de l'adresse:",
|
|
|
|
addressError
|
|
|
|
addressError
|
|
|
|
);
|
|
|
|
);
|
|
|
|
// Utiliser les coordonnées brutes en cas d'échec
|
|
|
|
|
|
|
|
this.form
|
|
|
|
this.form
|
|
|
|
.get('location')
|
|
|
|
.get('location')
|
|
|
|
?.setValue(`${data.latitude}, ${data.longitude}`);
|
|
|
|
?.setValue(`${data.latitude}, ${data.longitude}`);
|
|
|
@ -245,21 +252,57 @@ 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) => {
|
|
|
|
// Filtrer les fichiers qui sont déjà des IDs (images existantes)
|
|
|
|
return file.name; //TODO: Mettre le hash du fichier
|
|
|
|
const existingFiles = this.files.filter(
|
|
|
|
});
|
|
|
|
(file) => typeof file === 'string'
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
const newFiles = this.files.filter((file) => file instanceof File);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Récupérer la date et la formater correctement
|
|
|
|
|
|
|
|
const dateValue = this.form.get('date')?.value;
|
|
|
|
|
|
|
|
const formattedDate = dateValue
|
|
|
|
|
|
|
|
? new Date(dateValue).toISOString()
|
|
|
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (newFiles.length > 0) {
|
|
|
|
|
|
|
|
// Upload des nouveaux fichiers
|
|
|
|
|
|
|
|
const uploadObservables = newFiles.map((file) =>
|
|
|
|
|
|
|
|
this.imageService.postImage(file)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
forkJoin(uploadObservables).subscribe((responses) => {
|
|
|
|
|
|
|
|
// Combiner les IDs des nouvelles images avec les IDs existants
|
|
|
|
|
|
|
|
const allFileIds = [
|
|
|
|
|
|
|
|
...existingFiles,
|
|
|
|
|
|
|
|
...responses.map((res: any) => res.id),
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
const pinData = {
|
|
|
|
const pinData = {
|
|
|
|
...this.form.value,
|
|
|
|
...this.form.getRawValue(),
|
|
|
|
files: this.files,
|
|
|
|
files: allFileIds,
|
|
|
|
user_id: this.pin.user_id,
|
|
|
|
user_id: this.pin.user_id,
|
|
|
|
date: this.form.get('date')?.value || null,
|
|
|
|
date: formattedDate,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
// Si pas de nouveaux fichiers, mettre à jour directement avec les fichiers existants
|
|
|
|
|
|
|
|
const pinData = {
|
|
|
|
|
|
|
|
...this.form.getRawValue(),
|
|
|
|
|
|
|
|
files: existingFiles,
|
|
|
|
|
|
|
|
user_id: this.pin.user_id,
|
|
|
|
|
|
|
|
date: formattedDate,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.pinService.updatePin(this.pin.id, pinData).subscribe(() => {
|
|
|
|
|
|
|
|
this.mapReloadService.requestReload();
|
|
|
|
|
|
|
|
this.closePinModal();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
console.error('Le formulaire est invalide');
|
|
|
|
console.error('Le formulaire est invalide');
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -272,4 +315,18 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
|
|
closePinModal() {
|
|
|
|
closePinModal() {
|
|
|
|
this.modalService.closeModal(this.modalId);
|
|
|
|
this.modalService.closeModal(this.modalId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
removeFile(fileName: string): void {
|
|
|
|
|
|
|
|
const index = this.files.findIndex((file) => {
|
|
|
|
|
|
|
|
if (typeof file === 'string') {
|
|
|
|
|
|
|
|
return file === fileName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.name === fileName;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (index > -1) {
|
|
|
|
|
|
|
|
this.files.splice(index, 1);
|
|
|
|
|
|
|
|
this.form.patchValue({ files: this.files });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|