A Simple Angular User Profile Form: Building a Foundation for Refactoring
Tags: angular, tutorial, form
Published: 2024-04-17 13:29:19

## Introduction

In this article series I want to walk you through a journey of code evolution within a practical user profile editor application. I will transform and optimize a simple Angular implementation into a well structured, maintainable solution using advanced techniques.

If you've ever felt overwhelmed by growing code complexity, this series is for you! I'll guide you step-by-step, demonstrating how to tackle common challenges through **state management**, **imperative programming**, **reactive programming**, **SOLID principles**, **Observables**, **NgRx component-store**, **NgRx global store**, **signals**, **error handling**, **uploading files**, **form validation**, **custom form control**, **HTTP interceptors**, **local web server** with node.js, **directives**, and many more. I'll start with the basics and progressively add layers of sophistication and discuss their tradeoffs. You will be able to better understand when and why to choose the proper solution for a specific context.

## The Problem

The task at hand is to create a **user profile editor**, similar to the one in the image below. The editor will include a form with four fields: _Name_, _Email_, _Address_, and _Avatar_.

![User profile editor form](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmyx4axzgbq30k6rique.png)

## Requirements

From the end-user perspective, I want to be able to:
- change any of the fields (name, email, address)
- change the avatar image

For an improved user experience, I'll add some functional requirements:
- display any validation error messages (e.g. “Please enter a valid email address”)
- disable the Save button if there are no changes or while the saving request is in progress
- provide a button for resetting the changes
- display a progress indicator for upload status (when changing the avatar).
- display a loading indicator while the user data is retrieved from the server.

Other improvements may come later on the way.

## Implementation

### Project set up

Begin by cloning the [GitHub repository](https://github.com/cezar-plescan/user-profile-editor.git) and checkout the `00.base` branch. The project uses Angular v17.3.3.

Then run `yarn install` and `ng serve`. Currently there is nothing implemented so far, but this is what we'll be doing next.

_**Note**: Yarn is a replacement for npm. It can be installed with `npm install -g yarn`._

### Planning

**Before actually writing code, we need some planning**. It might be tempting to jump right into the editor and start writing code. However, I consider it essential to take a step back and think about the overall structure of the application. 

In this planning phase I want to focus on the following:
- **What is the end result, what will the user see on the page?**
- **What are the main tasks that need to be completed?**
- **In what order should these tasks be done?**

The high-level tasks I would define are, in order of their priority:
- create the form layout (just displaying the necessary fields, without any data handling)
- read user data from an API and populate the form
- handle server errors
- updating the user data
- uploading a new avatar

Once we have a good understanding of the overall plan, we can start to break down the tasks into smaller, more manageable pieces, which will make it easier to write the code and track the progress.

### Task 1: Create the form layout

I will use a reactive form with Material components. Open _user-profile.component.html_ file and paste the following content:
{% embed https://gist.github.com/cezar-plescan/380fe7eabd7150ac716cdbb0a0ede95a %}
Then open _user-profile.component.ts_ and paste:
{% embed https://gist.github.com/cezar-plescan/4bf7bd3e3ad3ddcacfadc437eff15381 %}
Your user-profile component code should look like [here](https://github.com/cezar-plescan/user-profile-editor/tree/01.form/src/app/user-profile). I also added some styling for the template.

The form is now in place with some placeholder values. We want to make this realistic and read the data from the backend, which will be covered in the next section.

### Task 2: Develop an API endpoint for user data retrieval

To facilitate communication with the backend to read and save data, I'll use a simple node.js express server that simulates a real-world backend. The code needs to reside in a folder named `backend` within the root project folder. You can checkout the entire code from `02.get-api` branch where you'll find the contents of the backend server. The `package.json` file includes the new packages and scripts for building and running the server. The data is stored locally in the db.json file. You need to run `yarn install` and then `yarn run backend:run` to start the server (run these commands from the root project folder). The server starts listening on port 3000, so make sure no other app is using it.

Navigate to [http://localhost:3000/users](http://localhost:3000/users) or [http://localhost:3000/users/1](http://localhost:3000/users/1) and you should see the contents from the db.json file.

Now that we have the get endpoint API in place, let's read the data from our component.

### Task 3: Read user data and populate the form

First of all I'll jump to our component class and declare my intention to read the user data when the component is initialized. 
{% embed https://gist.github.com/cezar-plescan/113282b627c1b69f64654d5f2ca97290 %}

**Note**: As a _general practice_, I prefer to use **descriptive functions or methods** for **what** I have to to, and then to describe **how** to implement them. This approach is like starting with building a skeleton of a system, along with the high-level workflow, and after that to actually implement how each operation is executed. Working this way has been helping me to implement complex systems and I consider it paramount in designing efficient software.

Now let's describe what `loadUserData()` should do:
- make an http get request to the API.
- when the response is ready, to populate the form with the user data.

For simplicity, I'll write the entire necessary code in the component class, without creating an additional service for getting user data. Of course, this is not a good practice for a production code, but I want to make small steps and gradually improve the code. 
{% embed https://gist.github.com/cezar-plescan/04a4e71b173495a486043f7c2d9a78de %}

I had to create some interfaces for our data:
{% embed https://gist.github.com/cezar-plescan/413632b78950b2ed1a3af69a08b20fba %}

The `pick` function is from the lodash library, so you need to install some packages: `yarn add lodash-es`, `yarn add --dev @types/lodash-es`.

You also need to inject the `HttpClient` service into the component class and to declare its provider in the `app.config.ts` file:
{% embed https://gist.github.com/cezar-plescan/6d51445cfda1c338d571177b0b5425e8 %}

For now I assume a successful API response, and later we'll implement the error handling.

Having the server started, reload the page. Your code should now be similar to the one on the [03.get-user-data](https://github.com/cezar-plescan/user-profile-editor/tree/03.get-user-data/src/app/user-profile) branch.

### Task 4. Display a loading indicator

We have the user data displayed, which is a good progress. I want to improve the user experience by showing a loading indicator until the request is completed.

For this I need two pieces of information:
- a flag that indicates that the load request is in progress,
- a visual representation of the loading state.

I will implement the flag as a boolean class property `isLoadRequestInProgress` and update its value right before the request is initiated and after it completes:
{% embed https://gist.github.com/cezar-plescan/3c5490bbeaa887dcc5b57514cc280ad9 %}

At this moment we know when the load request is in progress and we have to update the template based on this state. There could be different ways to visualize the loading progress (like a spinner animation), but I will choose the simplest one, that is, to hide the entire form and show a message.

_**Note**: My intention with this tutorial is to provide the mechanisms to handle different scenarios, upon which visually appealing content could be created in collaboration with the UX team. From a software design perspective, I achieve the separation of concerns: I just have the information about the loading status, but I'm not concerned about how the view will look like. This separation enhances extensibility, someone could want later to change the view, but the logic of how the loading status is computed will be unaffected by the change request._
{% embed https://gist.github.com/cezar-plescan/0faa97151d9d1cc11b01b0ec6d2d014f %}

As you may notice, I'm using the `@if` new control syntax from Angular v17. It does the same as the classical `*ngIf` directive:
{% embed https://gist.github.com/cezar-plescan/c907718020f224524703b64b41ef1af6 %}

Because you're serving the application form your localhost you may not notice the loading message. You could simulate a slower connection from the Network tab in DevTools window.

Your code should now be similar to the one on the [04.loading-indicator](https://github.com/cezar-plescan/user-profile-editor/tree/04.loading-indicator/src/app/user-profile) branch.

### Task 5. Handling loading errors

In the real world we could encounter errors when making an HTTP request, for whatever reasons. This is something that we must handle and not assume that errors don't happen.

What do we need in order to handle any errors from the loading request?
- to know when an error occurs
- to inform the user about the error
- to offer the user the ability to retry the request

#### Catching the error

I'll use the `catchError` RxJS operator ([see RxJS docs](https://rxjs.dev/api/index/function/catchError)) and for simplicity I'll just set a flag `hasLoadingError` that indicates the presence of an error. 

_**Note**: As I've mentioned earlier, I want to provide only the mechanism of signaling the error, without considering any other details. In a real-world scenario the errors could have various sources, like no network connection, internal server error, unauthorized access, and so on. Handling these errors is a different topic not covered by this tutorial._

Here is the updated `loadUserData()` method:
{% embed https://gist.github.com/cezar-plescan/c8108500e7ab77d77c36f4ca559fb711 %}

**Note**: I must return an observable from the catchError callback, so I'm using the `EMPTY` observable ([see RxJS docs](https://rxjs.dev/api/index/const/EMPTY)), otherwise, if I return a non-empty observable, the subscriber's `next` method will be invoked with the returned observable value.

I've also used the `finalize` operator ([see RxJS docs](https://rxjs.dev/api/operators/finalize)), which has its callback executed when the request observable terminates on complete or error, to reset the `isLoadRequestInProgress` flag.

#### Inform the user about the error and retry the operation

Now that we have the information if an error occurred, we have to update the template to display a simple message. Additionally we can add a Retry button to reload the data. In a real-world application there could be different use cases, but I choose a simple scenario for now.
{% embed https://gist.github.com/cezar-plescan/a2127683dcf4ee02d8a5287fad63a3e0 %}

To simulate an error there are some options:
- to modify the URL in the `getUserData$()` method
- to stop the backend server
- to simulate an offline network connection from DevTools

I consider stopping the backend server the most convenient option. Here is my scenario you could follow: 
- stop the backend server
- reload the app in the browser: you should see the error message
- click Retry: the loading message should be visible and then the error (you can eventually throttle down the network connection)
- restart the backend server and click Retry: the form should be now displayed with the expected user data

At this stage you should have the code similar to the one on the [05.loading-errors](https://github.com/cezar-plescan/user-profile-editor/tree/05.loading-errors/src/app/user-profile) branch.

### Task 6. Saving the user data

One of the main features of this tutorial is the ability to update the user profile data. For this we need a data persistent storage which is provided by the backend server. At this stage I'll focus on updating the name, email and address, without the profile avatar, which I'll cover in a separate task. I'll implement error handling in a later step, but for now, I'll assume successful save operations.

The implementation for this features requires two parts:
- sending the data to the server
- saving the data on the server-side

#### Sending the data to the server

This action is triggered when the form is submitted, so I need to define the `ngSubmit` handler to make a PUT request to the server with the current form value. To keep the things simple, I leave aside any form validation for now.

Update the user-profile.component.html template:
{% embed https://gist.github.com/cezar-plescan/538f6a215e983463b2e1ab10147554a5 %}

and the user-profile.component.ts component class:
{% embed https://gist.github.com/cezar-plescan/8757701da2dd134c9042c4b8cc071f3a %}

#### Updating the data on the server

Add the following content to your server.ts file:
{% embed https://gist.github.com/cezar-plescan/4b908bfcd341496bb3818eb349cbd959 %}

**Note**: This is just a simple implementation to save the user data and shouldn't be used in a production code.

After making these changes restart the backend server, reload the browser, and make any changes in the form (keep in mind that there are no validations in place). After you submit the form reload the page and the changes should be persistent.

At this stage you should have the code similar to the one on the [06.save-user-data](https://github.com/cezar-plescan/user-profile-editor/tree/06.save-user-data/src/app/user-profile) branch.

### Task 7. Validate user data and display the validation errors

The user data need to have some constraints to be saved in the database, like:
- the name should not be empty and it should be unique
- the email should have a valid format
- the address should not be empty

**Note**: these are just some simple validation rules to illustrate how to work with them.

As a general best practice, the server should always validate the data it receives, regardless on any client-side validations. For simplicity, our server will validate only if the user's name is unique, leaving the other validation rules on the client-side.

#### Client-side validation and error messages

We already have some validators in place for the form fields (see the `form` property definition in the `UserProfileComponent` class). What we need is to display the validation error messages in the template:
{% embed https://gist.github.com/cezar-plescan/267ce649076cf82490a0b83dab3e1590 %}

I've added the `<mat-error>` element for displaying the validation errors, which is automatically shown by Angular Reactive Forms only when the corresponding field has errors.

#### Server-side validation

As mentioned above, I'll validate on the server-side only if the user's name is unique:
{% embed https://gist.github.com/cezar-plescan/3850661480ccc1f6c506bbb1cc05a617 %}

In order for the validation to fail, you need to add a new user in the `db.json` file.

As a convention, the server responds with 400 HTTP status code, and with a body containing the validation errors.

#### Display server-side validation errors

In the case of client-side validation, the errors were implicitly set by the Angular Reactive Form, but for the server-side validation errors we need to manually handle them. Update the `saveUserData()` method from the `user-profile.component.ts` file:
{% embed https://gist.github.com/cezar-plescan/1e62e9d0dc0832997decf9290d60da87 %}

The template should be updated too:
{% embed https://gist.github.com/cezar-plescan/d7bf221dbcceaed06ecc6e3dc39ad066 %}

#### Disable form submission

You may have noticed that even if there are client-side validation errors, the form can still be submitted. To avoid this we can simply disable the submit button. The form will internally detect the status of the submit button and even if we hit the ENTER key in any of the fields, the form won't be submitted.
{% embed https://gist.github.com/cezar-plescan/2b165eb737207f498d1122813b9e172e %}
{% embed https://gist.github.com/cezar-plescan/a40d1c601c602329bd572d9d050f2aa6 %}

At this stage you should have the code similar to the one on the [07.validate-user-data](https://github.com/cezar-plescan/user-profile-editor/tree/07.validate-user-data/src/app/user-profile) branch.

### Task 8. Add some UX improvements

- add a Reset button
- disable the Save button while the save request is in progress or the form is pristine
- add a notification when the saving successfully completes

#### Reset the form

This feature is useful when we want to reset the form to the last saved state.
These are the changes I've made:
- created a new Reset button
- defined the `restoreForm()` method
- defined the disabled state of the button
- determine is the form value is changed since the last save
- disable the Save button if there are no changes

{% embed https://gist.github.com/cezar-plescan/e36baebda9371d3c0d754d16c66b624d %}
{% embed https://gist.github.com/cezar-plescan/cdfc0afdbaaf290d418ee54e33e3ad8c %}

#### Disable the Save button

One additional condition can be used to disable the Save button: while the save request is in progress. I also want to disable the form controls during this state.

{% embed https://gist.github.com/cezar-plescan/86b937cc6148033660ec3a598ef0e4e2 %}

#### Add a notification

After a successful save I want to display a notification. I'll use the Material snackbar component for this. 

{% embed https://gist.github.com/cezar-plescan/0d47cbf7e184ea99d87dd0a912baf2af %}
{% embed https://gist.github.com/cezar-plescan/cc54d9d7edc780d7436052474f0da543 %}

At this stage you should have the code similar to the one on the [08.ux-improvements](https://github.com/cezar-plescan/user-profile-editor/tree/08.ux-improvements/src/app/user-profile) branch.

### Task 9. Handle unexpected saving errors

Earlier I've described how to handle validation errors, which is something that we can expect to happen. But what about other errors, like internal server error, or no network connection error. If any of such errors happen, I want to simply display a notification to the user. I'll just use the `NotificationService` for this, which can eventually be customized to use different colors for the snackbar, but this is out of scope for now.
Let's update the `catchError` callback in the `saveUserData()` method:
{% embed https://gist.github.com/cezar-plescan/bbac7e722ad28c280415392044ef7cd6 %}

We need to simulate some errors to see how the error handling works. Here are some possible options to try:
- simulate no network connection from the browser DevTools,
- stop the backend server,
- programatically send an error from the server.

The code below shows how to generate a server error. Just for demo purposes, if we enter '_error_' in the name field, the server will send a 500 error:
{% embed https://gist.github.com/cezar-plescan/2c409362cb0b443ab18e4130e57d5514 %}

### Task 10. Uploading an avatar image

Here comes a very nice feature to implement: uploading and displaying an image. The entire process requires some steps:
- select an image to upload
- display the uploaded image
- store the image on the server
- generate a URL for it

#### Select an image and display it

The straightforward implementation is to use the `<input type="file" />` element for selecting an image from the local computer.

In the template, we'll have to update 2 parts:
- the source of the image
- the button to upload an image

{% embed https://gist.github.com/cezar-plescan/3ff4cc3fe4174fd71e56a24a969d1d24 %}

The `onImageSelected` method is called every time the user selects a new image. We need to display that image in place of the existing avatar. An image needs an URL, which can be an external or local resource. I don't want to upload the image right after selecting it, because the user could maybe change their mind, so I'll use `URL.createObjectURL()` function that will generate a local URL based on the file content.
{% embed https://gist.github.com/cezar-plescan/9bdef726d01fa05b2726c7fe42451ce4 %}

I want to add a fix here: after I select an image and then click the Reset button, the avatar is indeed restored, but the name of the previously selected file is still displayed, and it should be reset too. For this I'll just write an empty value to the input element:
{% embed https://gist.github.com/cezar-plescan/5079f40afd16e5f995fcb0350816029f %}

#### Upload the image to the server

The standard and recommended way to send files to the server is using the `FormData` browser API. The object created with this constructor will be sent as a payload to the saving request. As a consequence, the values of the other 3 form fields (name, email, address) will be part of this FormData object.

Let's see how we can construct this object. Instead of directly sending the form values, we'll create a FormData object that holds the form values and replace the avatar with the selected image, if any.
{% embed https://gist.github.com/cezar-plescan/a8018ed98933d45940e5da073c709db2 %}

Once the client-side is set up, we have to upgrade the backend server to support uploading files. An implementation option is to use the `multer` package. First of all, we have to install the additional packages as dev dependencies:
`yarn add -D multer @types/multer`.

Then open your _server.ts_ file and add the following contents to set up the library configuration (it's recommended to be added at the beginning of the file):
{% embed https://gist.github.com/cezar-plescan/20b5a0d7fdfb10d1ea4651e81495d308 %}

**Note**: There are some additional things to do:
- create the `images` folder inside `backend`
- add the contents of that folder to `.gitignore`: `/backend/images/*`

Now we need to run the uploading process and return the generated filename. For this we have to update the `/users/:id` route:
{% embed https://gist.github.com/cezar-plescan/f2414de62d676b2a2205acce516010d7 %}

Make sure to restart the backend server and then go to the app and upload an image. If you refresh the page, the image is not displayed anymore, because its URL points to the local Angular web server, instead of the server URL. Let's fix this.

In the template I'll use a method to compute the image URL:
{% embed https://gist.github.com/cezar-plescan/789f68d07a2a8b5617d27e2139d33d3a %}
{% embed https://gist.github.com/cezar-plescan/47773c1de917ddc83315ab4a69e3bf29 %}

Try to upload again an image and save the profile. You may notice that the Save and Reset buttons are still enabled, but they should be disabled. Why is this happening? Because the form is not computed as pristine due to the avatar value holding the local URL to the selected file. To solve this, we have to reset the form after the saving completes successfully:
{% embed https://gist.github.com/cezar-plescan/d7413a0ef5753dfdd4eaa3a0624df0dc %}

At this stage you should have the code similar to the one on the [10.avatar-upload](https://github.com/cezar-plescan/user-profile-editor/tree/10.avatar-upload/src/app/user-profile) branch.

### Task 11. Upload progress indicator

A nice feature I want to have is an indicator for the upload progress. For the visual representation I pick the `MatProgressBar` component from the Angular Material library. Let's explore how the compute the percentage value of the progress bar.

#### Getting access to upload progress information

It is made available by setting `reportProgress` and `observe` options of the request:
{% embed https://gist.github.com/cezar-plescan/9f27dc2404b8398824197c28ed5474ec %}
Now the return type of the `saveUserData$` method is 
`Observable<HttpEvent<UserDataResponse>>`, where before it was just ` Observable<UserDataResponse>`. We'll make the necessary adjustments soon.

#### Visual representation

Add the following snippet right before the closing `</form>` tag:
{% embed https://gist.github.com/cezar-plescan/824f641584628e80f82a672e79d45310 %}
The `uploadProgress` variable will be described in the next paragraph.

#### Computing the upload progress percentage

We need to update the observer function of the `saveUserData$()` observable in the `saveUserData()` method:
{% embed https://gist.github.com/cezar-plescan/856885457c3ef061065dcabffb0d6b92 %}

Let's see it in action! Select an image having around 1MB and throttle down the network connection from the DevTools. You should see how the progress bar grows and then it hides.

You should be in sync with the code from [11.upload-progress](https://github.com/cezar-plescan/user-profile-editor/tree/11.upload-progress/src/app/user-profile) branch.

### Outro

We've covered up the requirements from the beginning of the tutorial and we've made a good progress so far. While functional, the current implementation prioritized getting the features working, without discussing about any software design best practices. I've just wanted to have a starting point for the following articles that will be about improving this simple version by focusing on scalability and extensibility, which are the foundation of Angular enterprise applications. 

To manage something big we have to be able to control small things first, every oak grows from a little seed, and so is our expertise: start small, add little by little continuously, and the result will get bigger and bigger.

In the next articles I'll focus on refactoring the existing code by following the SOLID principles and by covering the implementation of: **custom form control** for image management, error handling in **HTTP interceptors**, **custom RxJs operators**, **directive** for displaying the form field errors.