diff --git a/src/app/components/animaux/animaux-edit/animaux-edit.component.ts b/src/app/components/animaux/animaux-edit/animaux-edit.component.ts
index d9efe3e..9bfec9a 100644
--- a/src/app/components/animaux/animaux-edit/animaux-edit.component.ts
+++ b/src/app/components/animaux/animaux-edit/animaux-edit.component.ts
@@ -1,17 +1,20 @@
-import { Component, Input } from '@angular/core';
+import { Component, ElementRef, Input, ChangeDetectorRef, AfterViewChecked, ChangeDetectionStrategy } from '@angular/core';
import { ToastrService } from 'ngx-toastr';
import { AppService } from '../../../services/app.service';
import { UserService } from '../../../services/user.service';
import { Chat, Sexe } from '../../../interfaces/interfaces';
import { faMars, faTrash, faUpload, faVenus, faHeart } from '@fortawesome/free-solid-svg-icons';
import { faHeart as fasHeart } from '@fortawesome/free-regular-svg-icons';
+import { environment } from 'src/environments/environment';
+import { NgZone } from '@angular/core';
@Component({
selector: 'app-animaux-edit',
templateUrl: './animaux-edit.component.html',
- styleUrls: ['./animaux-edit.component.scss']
+ styleUrls: ['./animaux-edit.component.scss'],
+ changeDetection: ChangeDetectionStrategy.OnPush
})
-export class AnimauxEditComponent {
+export class AnimauxEditComponent implements AfterViewChecked{
@Input() chat: Chat | undefined;
@Input() isCreationMode: boolean = false;
isLoaded: boolean = false;
@@ -24,18 +27,25 @@ export class AnimauxEditComponent {
fasHeart = fasHeart;
dataModel: any;
isAuthenticated: boolean = false;
-
-
+ selectedFiles: File[] = [];
+ s3Url = environment.s3Url;
constructor(
private appService: AppService,
private toastr: ToastrService,
public userService: UserService,
+ private cdr: ChangeDetectorRef,
+ private zone: NgZone
) {}
ngOnInit() {
this.initializeChat();
}
+ ngAfterViewChecked() {
+ // Code à exécuter après que la vue ait été vérifiée
+ // this.cdr.detectChanges();
+ }
+
private initializeChat() {
if (!this.chat) {
this.chat = this.createDefaultChat();
@@ -76,9 +86,59 @@ export class AnimauxEditComponent {
}
+ onFileSelected(event: Event, existingPhoto: any) {
+ const input = event.target as HTMLInputElement;
+ if (input.files && input.files.length > 0) {
+ const file = input.files[0];
+
+ // Replace the existing photo with the new file
+ this.replacePhoto(existingPhoto, file);
+ }
+ }
+
+ onFilesSelected(event: Event) {
+ const input = event.target as HTMLInputElement;
+ if (input.files && input.files.length > 0) {
+ this.zone.run(() => {
+ for (let i = 0; i < input!.files!.length; i++) {
+ this.selectedFiles.push(input!.files![i]);
+ }
+ this.cdr.detectChanges(); // Ou markForCheck ici si OnPush
+ });
+ }
+ }
+
+
+
+ replacePhoto(existingPhoto: any, newFile: File) {
+ const index = this.chat!.photos.indexOf(existingPhoto);
+ if (index > -1) {
+ this.chat!.photos.splice(index, 1);
+ }
+
+ this.selectedFiles.push(newFile);
+
+ // Délai court pour attendre la fin du cycle de détection du changement
+ setTimeout(() => {
+ this.cdr.detectChanges(); // Force la détection de changement
+ }, 0);
+ }
+
+
+ getFileUrl(file: File): string {
+ return URL.createObjectURL(file);
+ }
+
+
saveChat() {
- // Function to create a new chat
- this.appService.createChat(this.chat!).subscribe({
+ const formData = new FormData();
+ formData.append('chat', JSON.stringify(this.chat));
+
+ this.selectedFiles.forEach((file) => {
+ formData.append('photos', file, file.name);
+ });
+
+ this.appService.createChat(formData).subscribe({
error: (error) => {
console.error('error:', error);
this.toastr.error('Erreur lors de la création du chat');
@@ -89,18 +149,27 @@ export class AnimauxEditComponent {
});
}
+ removePhoto(file : File) {
+ const index = this.selectedFiles.indexOf(file);
+ console.log('🚀 ~ AnimauxEditComponent ~ removePhoto ~ index:', index);
+ if (index > -1) {
+ this.selectedFiles.splice(index, 1);
+ }
+ }
+
+
updateChat() {
- this.appService.updateChat(this.chat!).subscribe({
+ const formData = new FormData();
+ formData.append('chat', JSON.stringify(this.chat));
+
+ this.selectedFiles.forEach((file) => {
+ formData.append('photos', file, file.name);
+ });
+
+ this.appService.updateChat(this.chat!.id, formData).subscribe({
error: (error) => {
console.error('error:', error);
- if (error?.error?.message) {
- this.toastr.error(
- error.error.message,
- 'Erreur de mise à jour du chat'
- );
- } else {
- this.toastr.error('Erreur de mise à jour du chat');
- }
+ this.toastr.error('Erreur de mise à jour du chat');
},
complete: () => {
this.toastr.success('Chat mis à jour avec succès');
diff --git a/src/app/components/animaux/animaux-list/animaux-list.component.html b/src/app/components/animaux/animaux-list/animaux-list.component.html
index c94f135..6a62981 100644
--- a/src/app/components/animaux/animaux-list/animaux-list.component.html
+++ b/src/app/components/animaux/animaux-list/animaux-list.component.html
@@ -56,12 +56,12 @@
Les chat
-
-
+
{{chat.association.nom}}
- {{formatDate(chat.createdAt)}}
diff --git a/src/app/components/animaux/animaux-list/animaux-list.component.ts b/src/app/components/animaux/animaux-list/animaux-list.component.ts
index 556457a..3c78757 100644
--- a/src/app/components/animaux/animaux-list/animaux-list.component.ts
+++ b/src/app/components/animaux/animaux-list/animaux-list.component.ts
@@ -11,6 +11,7 @@ import { AuthService } from '@auth0/auth0-angular';
import { Subscription, firstValueFrom } from 'rxjs';
import { PaginatorState } from 'primeng/paginator';
import { FormBuilder, FormGroup } from '@angular/forms';
+import { environment } from 'src/environments/environment';
@Component({
selector: 'app-animaux-list',
@@ -32,6 +33,7 @@ export class AnimauxListComponent {
chats: Chat[] = [];
favoris: Favori[] = [];
associations: Association[] = [];
+ s3Url = environment.s3Url;
private subscriptions = new Subscription();
diff --git a/src/app/services/app.service.ts b/src/app/services/app.service.ts
index 03c0093..67283a5 100644
--- a/src/app/services/app.service.ts
+++ b/src/app/services/app.service.ts
@@ -68,22 +68,23 @@ export class AppService {
);
}
- updateChat(chat: Chat): Observable {
- return this.http.put(this.api + '/chats/' + chat.id, chat).pipe(
+ updateChat(id: number, formData: FormData): Observable {
+ return this.http.put(this.api + `/chats/${id}`, formData).pipe(
map((res: any) => res),
share(),
take(1)
);
}
- createChat(chat: Chat): Observable {
- return this.http.post(this.api + '/chats', chat).pipe(
+ createChat(formData: FormData): Observable {
+ return this.http.post(this.api + '/chats', formData).pipe(
map((res: any) => res),
share(),
take(1)
);
}
+
getAllAsso(): Observable {
return this.http.get(this.api + '/associations').pipe(
map((res: any) => res),
diff --git a/src/environments/environment.model.ts b/src/environments/environment.model.ts
index d10aff6..8b253f7 100644
--- a/src/environments/environment.model.ts
+++ b/src/environments/environment.model.ts
@@ -13,4 +13,5 @@ export interface Environment {
api: {
serverUrl: string;
};
+ s3Url: string;
}
diff --git a/src/environments/environment.prod.ts b/src/environments/environment.prod.ts
index f70fa38..e3637fd 100644
--- a/src/environments/environment.prod.ts
+++ b/src/environments/environment.prod.ts
@@ -15,4 +15,5 @@ export const environment: Environment = {
api: {
serverUrl: 'https://api.potits-chats.com',
},
+ s3Url: 'https://potits-chats.s3.eu-west-3.amazonaws.com/public/',
};
diff --git a/src/environments/environment.ts b/src/environments/environment.ts
index b3838af..ea67d27 100644
--- a/src/environments/environment.ts
+++ b/src/environments/environment.ts
@@ -15,4 +15,5 @@ export const environment: Environment = {
api: {
serverUrl: 'http://localhost:3000',
},
+ s3Url: 'https://potits-chats.s3.eu-west-3.amazonaws.com/public/',
};
From 6943a1f503bfa93441ff6763c5109c87a73b3a74 Mon Sep 17 00:00:00 2001
From: FazCodeFR <30906528+FazCodeFR@users.noreply.github.com>
Date: Sun, 15 Sep 2024 17:41:06 +0200
Subject: [PATCH 3/3] Update et create cat
---
.../animaux-details.component.html | 4 +-
.../animaux-edit/animaux-edit.component.html | 56 ++++++++-----------
.../animaux-edit/animaux-edit.component.ts | 36 ++++++------
.../animaux-list/animaux-list.component.html | 2 +-
.../animaux-list/animaux-list.component.ts | 4 +-
.../associations-details.component.html | 4 +-
src/app/services/app.service.ts | 10 +++-
7 files changed, 61 insertions(+), 55 deletions(-)
diff --git a/src/app/components/animaux/animaux-details/animaux-details.component.html b/src/app/components/animaux/animaux-details/animaux-details.component.html
index 7898e94..a13692c 100644
--- a/src/app/components/animaux/animaux-details/animaux-details.component.html
+++ b/src/app/components/animaux/animaux-details/animaux-details.component.html
@@ -32,10 +32,10 @@
-
+
-
+
diff --git a/src/app/components/animaux/animaux-edit/animaux-edit.component.html b/src/app/components/animaux/animaux-edit/animaux-edit.component.html
index 6ebfb02..0d96456 100644
--- a/src/app/components/animaux/animaux-edit/animaux-edit.component.html
+++ b/src/app/components/animaux/animaux-edit/animaux-edit.component.html
@@ -6,17 +6,21 @@
-