diff --git a/src/app/app.component.css b/src/app/app.component.css index 75c7eeb..6fc5dac 100644 --- a/src/app/app.component.css +++ b/src/app/app.component.css @@ -81,3 +81,10 @@ a { background: rgba(0, 0, 0, 0.1); text-decoration: none; } + +.error { + color: red; + background-color: white; + padding: 16px; + border-radius: 12px; +} diff --git a/src/app/app.component.html b/src/app/app.component.html index 6daffd7..c5fbf03 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,17 +1,29 @@
diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 42fa9d7..4c4b986 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,9 +1,40 @@ -import { Component } from '@angular/core'; +import { Component, computed, inject, signal } from '@angular/core'; +import { DiseaseService } from './shared/services/disease/disease.service'; +import { disabled, form, FormField } from '@angular/forms/signals'; @Component({ - imports: [], + imports: [FormField], selector: 'app-root', styleUrl: './app.component.css', templateUrl: './app.component.html', }) -export class AppComponent {} +export class AppComponent { + readonly diseaseService = inject(DiseaseService); + + readonly searchModel = signal({ + query: '', + }); + + readonly searchForm = form(this.searchModel, (field) => { + disabled(field.query, { + when: () => this.diseaseService.isLoading() || !!this.diseaseService.error(), + }); + }); + + readonly searchTerm = computed(() => { + const query = this.searchForm.query().value().trim().toLowerCase(); + + return query.length >= 2 ? query : ''; + }); + + readonly filteredDiseases = computed(() => { + const query = this.searchTerm(); + const diseases = this.diseaseService.diseases(); + + if (!query) return diseases; + + return diseases.filter( + ({ name, code }) => name.toLowerCase().includes(query) || code.toLowerCase().includes(query), + ); + }); +}