# Header

#### User Edit Profile Icon

* This icon adds a visual representation of the user and helps in quick identification.

<figure><img src="https://3585249674-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9mSCXmgzkm8z0iToQdRK%2Fuploads%2FL6Z54PYh4X16u62KLS4Z%2FScreenshot%202024-01-03%20at%201.47.32%20PM.png?alt=media&#x26;token=9c654fd7-bfb9-4eda-936a-7c7717fb2fe2" alt=""><figcaption><p>User Edit Profile Icon</p></figcaption></figure>

* Click the Edit icon on a profile picture to upload a new profile picture

<figure><img src="https://3585249674-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9mSCXmgzkm8z0iToQdRK%2Fuploads%2Flr2aOfrwzeeFha1CTAHI%2FScreenshot%202024-01-03%20at%201.52.08%20PM.png?alt=media&#x26;token=b8ba6932-c1e0-441d-a9ae-57c0df2d2e7d" alt=""><figcaption><p>User Profile Icon</p></figcaption></figure>

* Upload call after selecting the profile picture
* Request URL: <https://api.eng-dev-1.trilloapps.com/foldersvc/cloudstorage/upload>

**Payload:**

<figure><img src="https://3585249674-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9mSCXmgzkm8z0iToQdRK%2Fuploads%2FuFZKzd6pPPpsqBdIskaf%2FScreenshot%202024-01-03%20at%201.56.21%20PM.png?alt=media&#x26;token=f15331b6-5d28-47d8-95fa-3c225c779e48" alt=""><figcaption><p>Header Payload</p></figcaption></figure>

**Preview:**

<figure><img src="https://3585249674-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9mSCXmgzkm8z0iToQdRK%2Fuploads%2FZDiFWZqj4Ws8TPwemcje%2FScreenshot%202024-01-03%20at%201.58.07%20PM.png?alt=media&#x26;token=68bab062-1ad9-4938-90ff-13d5ecd31b87" alt=""><figcaption><p>Header Preview</p></figcaption></figure>

**Edit Profile Code Snippet**

```
onFileSelect(event) {   
   const selectedFiles: FileList = event.target.files as FileList;
   const validFileTypes = ["image/png", "image/jpeg", "image/jpg"];
   const invalidFiles = Array.from(selectedFiles).filter(
     (file) => !validFileTypes.includes(file.type)
   );
   this.filesToUpload = event.target.files;
   this.uploadFiles();
   this.filename = "";
   if (event.target.files && event.target.files.length > 0) {
     this.file = event.target.files[0];
     this.filename = this.file.name;
   }
 }
 uploadFiles() {
   let functionParam = {
     userId : this.userDetails.id
   }
   for (const file of this.filesToUpload) {
     const formData: FormData = new FormData();
     formData.append("file", file);
     formData.append("folder", "public/images");
     formData.append("makePublic", "true");
     formData.append("functionName", "AddUserImage");
     formData.append("functionParam", JSON.stringify(functionParam));
     this.DataService.ProfileFileUpload(formData).subscribe({
       next: (response) => {
         this.userProfileImage= response.pictureUrl
       },
       error: (error) => {
         console.error(error)
         console.error(error);
       },
       complete: () => { },
     });
   }
 }
```

#### Code Description

**File Selection**

* Captures the selected files from the input event.

**File Type Validation**

* &#x20;Checks the file types against a predefined list of valid image types ("image/png", "image/jpeg", "image/jpg").
* Filters out any files with invalid types and stores them in the invalidFiles array.
* Handling API Response:

**File Upload Initialization**

&#x20;Stores the selected files in the filesToUpload property.

**Filename Initialization**

* &#x20;Resets the filename property.

**Single File Handling**

&#x20; If files are selected

* Takes the first file from the selected files.
* Retrieves and stores its name in the filename property.

**File Upload Execution**

&#x20;  Invokes the uploadFiles method to handle the actual file upload.

**File Upload:**

&#x20; **Function Parameter Preparation**

&#x20; Constructs a functionParam object containing the userId retrieved from this.userDetails.id.

File Iteration:

Loops through each file in this.filesToUpload.

**FormData Preparation**

Creates a FormData object and appends file-related information:

* 'File': Appends the file itself with the key "file".
* 'Folder': Specifies the target folder as "public/images".
* 'makePublic': Sets the visibility of the file as public ("makePublic": "true").
* 'functionName': Specifies the intended function name as "AddUserImage".
* 'functionParam': Appends the function parameters as a JSON string.

**File Upload API Call**

Calls the ProfileFileUpload method from this.DataService with the prepared formData.

Subscribes to the observable returned by the API call.

**Handling API Response**

&#x20;In the next block, update this.userProfileImage with the URL of the uploaded picture from the API response.

**Error Handling**

In the error block, logs and displays any errors that occur during the file upload process.

Completion:

<br>
