Angular Front-end editing picture problem

So i am currently working on a web application that i can add user which has name last name age born date and a picture .And after i create a user i can delete and edit it . My problem is when i create a user and then after i try to edit it and replace the picture of the user and then save it it creates a new user with the same info but with the picture i tried to change on the first place.

edit-user.component.ts :

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { UserService } from '../user.service';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

declare var bootstrap: any;

@Component({
  selector: 'app-edit-user',
  templateUrl: './edit-user.component.html',
  styleUrls: ['./edit-user.component.css']
})
export class EditUserComponent implements OnInit {
  userForm: FormGroup;
  userId!: string;
  notificationMessage: string = '';
  private confirmModalInstance: any;
  photoUrl?: string | ArrayBuffer | null = '';

  constructor(
    private fb: FormBuilder,
    private userService: UserService,
    private route: ActivatedRoute,
    private router: Router
  ) {
    this.userForm = this.fb.group({
      id: [''],
      firstName: ['', [Validators.required, this.noNumbers]],
      lastName: ['', [Validators.required, this.noNumbers]],
      occupation: ['', [Validators.required, this.noNumbers]],
      gender: ['', Validators.required],
      dateOfBirth: ['', Validators.required],
      photo: ['']
    });
  }

  noNumbers = (control: any): { [key: string]: boolean } | null => {
    if (control.value && /d/.test(control.value)) {
      return { 'containsNumber': true };
    }
    return null;
  }

  ngOnInit(): void {
    this.userId = this.route.snapshot.params['id'];
    this.userService.getUser(Number(this.userId)).subscribe(user => {
      if (user) {
        this.userForm.patchValue(user);
        this.photoUrl = user.photo;
      }
    });
  
    const modalElement = document.getElementById('confirmSaveUserModal');
    if (modalElement) {
      this.confirmModalInstance = new bootstrap.Modal(modalElement);
    }
  }

  showNotification(message: string): void {
    this.notificationMessage = ''; 
    setTimeout(() => {
      this.notificationMessage = message;  
    }, 0);
  }

  handleSave(event: Event): void {
    event.preventDefault(); 
    if (this.userForm.invalid || this.hasErrors()) {
      this.showNotification('Please ensure all fields are correctly filled and do not contain numbers.');
    } else {
      this.showConfirmationModal(); 
    }
  }

  hasErrors(): boolean {
    const controls = this.userForm.controls;
    return Object.keys(controls).some(key => controls[key].hasError('containsNumber'));
  }

  showConfirmationModal(): void {
    if (this.confirmModalInstance) {
      this.confirmModalInstance.show();
    }
  }

  confirmSaveUser(): void {
    if (this.userForm.valid) {
      const updatedUser = { ...this.userForm.value };
      updatedUser.id = this.userId; // Explicitly assign the ID to avoid duplications
  
      this.userService.updateUser(updatedUser).subscribe(() => {
        this.confirmModalInstance.hide();
        this.router.navigate(['/home']);
      }, (error) => {
        console.error('Error updating user:', error);
      });
    }
  }

  cancel(): void {
    this.router.navigate(['/home']);
  }

  onFileChange(event: any): void {
    const file = event.target.files[0];
    if (file) {
      const reader = new FileReader();
      reader.onload = () => {
        this.photoUrl = reader.result as string; // Ensure it's a string
        this.userForm.patchValue({ photo: this.photoUrl }); 
      };
      reader.readAsDataURL(file);
    }
  }
}
user.service.ts :

import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { User } from './user.model';

@Injectable({
  providedIn: 'root'
})
export class UserService {
  private users: User[] = [];

  constructor() { }

  getUsers(): Observable<User[]> {
    return of(this.users);
  }

  getUser(id: number): Observable<User | undefined> {
    const user = this.users.find(user => user.id === id.toString());
    return of(user);
  }

  addUser(user: User): Observable<void> {
    if (!user.id) {
      user.id = (this.users.length + 1).toString();  
    }
    user.createdDate = new Date(); 
    if (user.dateOfBirth) {
      user.age = this.calculateAge(user.dateOfBirth);
    }
    this.users.push({ ...user });  
    return of();
  }
  
  updateUser(updatedUser: User): Observable<void> {
    const index = this.users.findIndex(user => user.id === updatedUser.id);
    if (index !== -1) {
      updatedUser.age = this.calculateAge(updatedUser.dateOfBirth);
      this.users[index] = updatedUser;
    }
    return of();
  }

  updateUserPhoto(id: string, photoUrl: string | ArrayBuffer | null): Observable<void> {
    const index = this.users.findIndex(user => user.id === id);
    if (index !== -1 && photoUrl) {
      if (typeof photoUrl !== 'string') {
        // Convert ArrayBuffer to base64 string
        photoUrl = this.arrayBufferToBase64(photoUrl);
      }
      this.users[index].photo = photoUrl;
    }
    return of();
  }

  deleteUser(id: number): Observable<void> {
    this.users = this.users.filter(user => user.id !== id.toString());
    return of();
  }

  private calculateAge(dateOfBirth: string): number {
    const dob = new Date(dateOfBirth);
    const ageDifMs = Date.now() - dob.getTime();
    const ageDate = new Date(ageDifMs);
    return Math.abs(ageDate.getUTCFullYear() - 1970);
  }

  private arrayBufferToBase64(buffer: ArrayBuffer): string {
    let binary = '';
    const bytes = new Uint8Array(buffer);
    const len = bytes.byteLength;
    for (let i = 0; i < len; i++) {
      binary += String.fromCharCode(bytes[i]);
    }
    return window.btoa(binary);
  }
}

I tried everything and it isnt working. I want when i edit it and place a new picture and save it the picture to change and not a new user to add.

New contributor

Ivan Drusanov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật