feat: implemented editing functionality for Contact; optimized data initialization in ContactsComponent

This commit is contained in:
Chneemann 2025-03-31 20:54:42 +02:00
parent eca837b0ea
commit 3293def254
4 changed files with 75 additions and 30 deletions

View file

@ -5,7 +5,7 @@ import { ContactDetailComponent } from './contact-detail/contact-detail.componen
import { FirebaseService } from '../../services/firebase.service'; import { FirebaseService } from '../../services/firebase.service';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { UserService } from '../../services/user.service'; import { UserService } from '../../services/user.service';
import { finalize, Subject, takeUntil } from 'rxjs'; import { finalize, firstValueFrom, Subject, takeUntil } from 'rxjs';
import { OverlayService } from '../../services/overlay.service'; import { OverlayService } from '../../services/overlay.service';
import { UpdateNotifierService } from '../../services/update-notifier.service'; import { UpdateNotifierService } from '../../services/update-notifier.service';
@ -32,44 +32,57 @@ export class ContactsComponent {
private updateNotifierService: UpdateNotifierService private updateNotifierService: UpdateNotifierService
) {} ) {}
/**
* Trigger onResize when the component is initialized
*/
ngOnInit(): void { ngOnInit(): void {
this.onResize(); this.onResize();
this.loadAllUsers(); this.initializeData();
this.loadCurrentUser();
this.subscribeToUserUpdates();
} }
loadAllUsers(): void { private async initializeData(): Promise<void> {
try {
await this.loadAllUsers();
this.loadCurrentUser();
this.subscribeToUserUpdates();
} catch (error) {
console.error('Error during initialization:', error);
}
}
private async loadAllUsers(): Promise<void> {
this.isLoading = true; this.isLoading = true;
try {
this.userService const response = await firstValueFrom(
.getUsers() this.userService
.pipe(finalize(() => (this.isLoading = false))) .getUsers()
.subscribe({ .pipe(finalize(() => (this.isLoading = false)))
next: (response) => { );
this.allUsers = response; this.allUsers = response;
}, } catch (err) {
error: (err) => { console.error('Error loading the users:', err);
console.error('Error loading the tasks:', err); }
},
});
} }
private subscribeToUserUpdates() { private async subscribeToUserUpdates(): Promise<void> {
this.updateNotifierService.contactUpdated$ this.updateNotifierService.contactUpdated$
.pipe(takeUntil(this.destroy$)) .pipe(takeUntil(this.destroy$))
.subscribe((userId: string) => { .subscribe(async (userId: string) => {
this.selectedUserId = userId; this.selectedUserId = null;
this.loadAllUsers(); try {
await this.loadAllUsers();
this.selectedUserId = userId;
} catch (error) {
console.error('Error while waiting for user load:', error);
}
}); });
} }
loadCurrentUser(): void { private loadCurrentUser(): void {
this.userService.getCurrentUser().subscribe((userData) => { this.userService.getCurrentUser().subscribe({
this.currentUser = userData; next: (userData) => {
this.currentUser = userData;
},
error: (err) => {
console.error('Error loading current user:', err);
},
}); });
} }
@ -119,4 +132,9 @@ export class ContactsComponent {
this.showAllUsers = true; this.showAllUsers = true;
} }
} }
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
} }

View file

@ -81,4 +81,8 @@ export class ApiService {
saveNewUser(user: User): Observable<User> { saveNewUser(user: User): Observable<User> {
return this.request<User>('POST', '/api/users/', user); return this.request<User>('POST', '/api/users/', user);
} }
updateUser(user: User, userId: string): Observable<User> {
return this.request<User>('PATCH', `/api/users/${userId}/`, user);
}
} }

View file

@ -45,6 +45,10 @@ export class ToastNotificationService {
this.createSuccessToast('Contact deleted successfully!', 'Contact Deleted'); this.createSuccessToast('Contact deleted successfully!', 'Contact Deleted');
} }
updateContactSuccessToast(): void {
this.createSuccessToast('Contact updated successfully!', 'Contact Updated');
}
// Helperfunctions // Helperfunctions
createSuccessToast(message: string, title: string = 'Success'): void { createSuccessToast(message: string, title: string = 'Success'): void {

View file

@ -223,11 +223,30 @@ export class ContactFormComponent implements OnInit, OnChanges {
this.newColor && this.newColor !== '' this.newColor && this.newColor !== ''
? this.newColor ? this.newColor
: this.currentColor || this.randomColor; : this.currentColor || this.randomColor;
this.saveContact();
if (this.selectedUserExists) {
this.updateContact();
} else {
this.saveNewContact();
}
} }
} }
async saveContact() { async updateContact() {
try {
const snakeCaseUserData = this.convertCamelToSnake(this.contactData);
const updatedUser = await lastValueFrom(
this.apiService.updateUser(snakeCaseUserData, this.selectedUser.id)
);
this.toastNotificationService.updateContactSuccessToast();
this.updateNotifierService.notifyUpdate('contact', updatedUser.id);
this.closeDialog();
} catch (error) {
console.error('Error updating the contact:', error);
}
}
async saveNewContact() {
try { try {
const userData: User = { const userData: User = {
firstName: this.contactData.firstName, firstName: this.contactData.firstName,
@ -249,7 +268,7 @@ export class ContactFormComponent implements OnInit, OnChanges {
this.updateNotifierService.notifyUpdate('contact', savedUser.id); this.updateNotifierService.notifyUpdate('contact', savedUser.id);
this.closeDialog(); this.closeDialog();
} catch (error) { } catch (error) {
console.error('Fehler beim Speichern des Kontakts:', error); console.error('Error saving the contact:', error);
} }
} }