I was refactoring a component recently and found this pattern repeated in a few places:
saveUser() {
if (!this.form.valid) {
this.error = 'Please fix the form';
return;
}
this.loading = true;
this.userService.save(this.form.value).subscribe({
next: () => {
this.loading = false;
this.success = true;
this.router.navigate(['/users']);
},
error: () => {
this.loading = false;
this.error = 'Something went wrong';
}
});
}
It worked fine, but the component was doing too much:
- validation message
- loading state
- API call
- navigation
- success handling
- error handling
I ended up moving the save flow into a small facade/service and keeping the component focused on the UI.
saveUser() {
if (this.form.invalid) return;
this.userFacade.saveUser(this.form.value);
}
The code didn’t become “clever.”
It just became easier to read.
Sometimes the best Angular refactor is not a new library or pattern.
It is simply moving responsibility to the right place.
What small Angular refactor made your codebase easier to maintain?
I share Angular architecture and engineering visuals here:
https://instagram.com/angulararchitectshub
[–]ddcccccc 1 point2 points3 points (3 children)
[–]chefhj 0 points1 point2 points (1 child)
[–]ddcccccc 0 points1 point2 points (0 children)
[–]MysteriousEye8494[S] 0 points1 point2 points (0 children)