From 8a6a28f67cca158428c8d6f0ecbebb13d96c82ca Mon Sep 17 00:00:00 2001 From: Flo Date: Sun, 1 Nov 2026 12:23:31 +0100 Subject: [PATCH] feat: simplify app structure --- src/app/app.component.css | 95 ------------------- src/app/app.component.html | 38 -------- src/app/app.component.ts | 71 +++++++------- .../disease-typeahead.component.ts | 63 ++++++++++++ src/app/disease-typeahead/index.ts | 1 + src/app/disease-typeahead/partials/index.ts | 2 + .../partials/search/search.component.ts | 47 +++++++++ .../suggestions/suggestions.component.ts | 66 +++++++++++++ .../shared/highlight/highlight.directive.ts | 2 +- 9 files changed, 214 insertions(+), 171 deletions(-) delete mode 100644 src/app/app.component.css delete mode 100644 src/app/app.component.html create mode 100644 src/app/disease-typeahead/disease-typeahead.component.ts create mode 100644 src/app/disease-typeahead/index.ts create mode 100644 src/app/disease-typeahead/partials/index.ts create mode 100644 src/app/disease-typeahead/partials/search/search.component.ts create mode 100644 src/app/disease-typeahead/partials/suggestions/suggestions.component.ts diff --git a/src/app/app.component.css b/src/app/app.component.css deleted file mode 100644 index ff63d4d..0000000 --- a/src/app/app.component.css +++ /dev/null @@ -1,95 +0,0 @@ - - -input { - width: 100%; - padding: 20px; -} - -.search-form { - max-width: 400px; - margin: 50px auto; -} - -input.search { - margin: 0; - text-align: center; - outline: 0; - border: 10px solid #f7f7f7; - width: 120%; - left: -10%; - position: relative; - top: 10px; - z-index: 2; - border-radius: 5px; - font-size: 40px; - box-shadow: - 0 0 5px rgba(0, 0, 0, 0.12), - inset 0 0 2px rgba(0, 0, 0, 0.19); -} - -input.search:disabled { - background: white; - cursor: not-allowed; -} - -.suggestions { - margin: 0; - padding: 0; - position: relative; -} - -.suggestions li { - background: white; - list-style: none; - border-bottom: 1px solid #d8d8d8; - box-shadow: 0 0 10px rgba(0, 0, 0, 0.14); - margin: 0; - padding: 20px; - transition: background 0.2s; - display: flex; - gap: 4px; - justify-content: space-between; -} - -.suggestions li:nth-child(even) { - transform: perspective(100px) rotateX(3deg) translateY(2px) scale(1.001); - background: linear-gradient(to bottom, #ffffff 0%, #efefef 100%); -} - -.suggestions li:nth-child(odd) { - transform: perspective(100px) rotateX(-3deg) translateY(3px); - background: linear-gradient(to top, #ffffff 0%, #efefef 100%); -} - -span.abbr { - font-size: 15px; - margin-inline-start: auto; -} - -.details { - font-size: 15px; -} - -::highlight(search-match) { - background: #ffc600; - color: black; -} - -.info { - text-align: center; - line-height: 2; - color: white; -} - -a { - color: white; - 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 deleted file mode 100644 index 9a0e114..0000000 --- a/src/app/app.component.html +++ /dev/null @@ -1,38 +0,0 @@ -
- - - @let error = diseaseService.error(); - - @if (error) { -

{{`HTTP error ${error.message}`}}

- } @else { - - } -
- -

- Using JavaScript, replicate the - functionality found here.
- The solution does not have to be perfect. Most important is to see how you - approach such tasks. -

diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 6a963f0..8b5e482 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,41 +1,38 @@ -import { Component, computed, inject, signal } from '@angular/core'; -import { DiseaseService } from './shared/services/disease/disease.service'; -import { disabled, form, FormField } from '@angular/forms/signals'; -import { HighlightDirective } from './shared/highlight/highlight.directive'; +import { Component } from '@angular/core'; +import { DiseaseTypeaheadComponent } from './disease-typeahead'; @Component({ - imports: [FormField, HighlightDirective], + imports: [DiseaseTypeaheadComponent], selector: 'app-root', - styleUrl: './app.component.css', - templateUrl: './app.component.html', + template: ` + + +

+ Using JavaScript, replicate the + functionality found here.
+ The solution does not have to be perfect. Most important is to see how you approach such + tasks. +

+ `, + styles: ` + ::highlight(search-match) { + background: #ffc600; + color: black; + } + + .info { + text-align: center; + line-height: 2; + color: white; + } + + a { + color: white; + background: rgba(0, 0, 0, 0.1); + text-decoration: none; + } + `, }) -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), - ); - }); -} +export class AppComponent {} diff --git a/src/app/disease-typeahead/disease-typeahead.component.ts b/src/app/disease-typeahead/disease-typeahead.component.ts new file mode 100644 index 0000000..2b334f3 --- /dev/null +++ b/src/app/disease-typeahead/disease-typeahead.component.ts @@ -0,0 +1,63 @@ +import { Component, computed, inject, signal } from '@angular/core'; +import { AppDiseaseSearchComponent, AppDiseaseSuggestionsComponent } from './partials'; +import { DiseaseService } from '../shared/services/disease/disease.service'; +import { disabled, form } from '@angular/forms/signals'; + +@Component({ + imports: [AppDiseaseSearchComponent, AppDiseaseSuggestionsComponent], + selector: 'app-disease-typeahead', + template: ` + + + + @if (diseaseService.error(); as error) { +

HTTP error {{ error.message }}

+ } @else { + + } +
+ `, + styles: ` + .search-form { + max-width: 400px; + margin: 50px auto; + } + + .error { + color: red; + background-color: white; + padding: 16px; + border-radius: 12px; + } + `, +}) +export class DiseaseTypeaheadComponent { + protected readonly diseaseService = inject(DiseaseService); + + protected readonly searchModel = signal({ + query: '', + }); + + protected readonly searchForm = form(this.searchModel, (field) => { + disabled(field.query, { + when: () => this.diseaseService.isLoading() || !!this.diseaseService.error(), + }); + }); + + protected readonly searchTerm = computed(() => { + const query = this.searchForm.query().value().trim().toLowerCase(); + + return query.length >= 2 ? query : ''; + }); + + protected 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), + ); + }); +} diff --git a/src/app/disease-typeahead/index.ts b/src/app/disease-typeahead/index.ts new file mode 100644 index 0000000..b552070 --- /dev/null +++ b/src/app/disease-typeahead/index.ts @@ -0,0 +1 @@ +export { DiseaseTypeaheadComponent } from './disease-typeahead.component'; diff --git a/src/app/disease-typeahead/partials/index.ts b/src/app/disease-typeahead/partials/index.ts new file mode 100644 index 0000000..6e6d131 --- /dev/null +++ b/src/app/disease-typeahead/partials/index.ts @@ -0,0 +1,2 @@ +export { AppDiseaseSearchComponent } from './search/search.component'; +export { AppDiseaseSuggestionsComponent } from './suggestions/suggestions.component'; diff --git a/src/app/disease-typeahead/partials/search/search.component.ts b/src/app/disease-typeahead/partials/search/search.component.ts new file mode 100644 index 0000000..dfdbb42 --- /dev/null +++ b/src/app/disease-typeahead/partials/search/search.component.ts @@ -0,0 +1,47 @@ +import { Component, input } from '@angular/core'; +import { type FieldTree, FormField } from '@angular/forms/signals'; + +@Component({ + imports: [FormField], + selector: 'app-disease-search', + template: ` + + `, + styles: ` + input { + width: 100%; + padding: 20px; + } + + input.search { + margin: 0; + text-align: center; + outline: 0; + border: 10px solid #f7f7f7; + width: 120%; + left: -10%; + position: relative; + top: 10px; + z-index: 2; + border-radius: 5px; + font-size: 40px; + box-shadow: + 0 0 5px rgba(0, 0, 0, 0.12), + inset 0 0 2px rgba(0, 0, 0, 0.19); + } + + input.search:disabled { + background: white; + cursor: not-allowed; + } + `, +}) +export class AppDiseaseSearchComponent { + readonly field = input.required>(); +} diff --git a/src/app/disease-typeahead/partials/suggestions/suggestions.component.ts b/src/app/disease-typeahead/partials/suggestions/suggestions.component.ts new file mode 100644 index 0000000..2412625 --- /dev/null +++ b/src/app/disease-typeahead/partials/suggestions/suggestions.component.ts @@ -0,0 +1,66 @@ +import { Component, input } from '@angular/core'; +import type { Disease } from '../../../shared/services/disease/disease.service'; +import { HighlightDirective } from '../../../shared/highlight/highlight.directive'; + +@Component({ + imports: [HighlightDirective], + selector: 'app-disease-suggestions', + template: ` +
    + @for (disease of diseases(); track disease) { +
  • + + {{ disease.name }}, + {{ disease.code }} + + + {{ disease.abbr }} +
  • + } +
+ `, + styles: ` + .suggestions { + margin: 0; + padding: 0; + position: relative; + } + + .suggestions li { + background: white; + list-style: none; + border-bottom: 1px solid #d8d8d8; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.14); + margin: 0; + padding: 20px; + transition: background 0.2s; + display: flex; + gap: 4px; + justify-content: space-between; + } + + .suggestions li:nth-child(even) { + transform: perspective(100px) rotateX(3deg) translateY(2px) scale(1.001); + background: linear-gradient(to bottom, #ffffff 0%, #efefef 100%); + } + + .suggestions li:nth-child(odd) { + transform: perspective(100px) rotateX(-3deg) translateY(3px); + background: linear-gradient(to top, #ffffff 0%, #efefef 100%); + } + + span.abbr { + font-size: 15px; + margin-inline-start: auto; + } + + .details { + font-size: 15px; + } + `, +}) +export class AppDiseaseSuggestionsComponent { + readonly diseases = input.required(); + readonly query = input.required(); +} diff --git a/src/app/shared/highlight/highlight.directive.ts b/src/app/shared/highlight/highlight.directive.ts index aa727bd..9ad3baa 100644 --- a/src/app/shared/highlight/highlight.directive.ts +++ b/src/app/shared/highlight/highlight.directive.ts @@ -12,7 +12,7 @@ import { selector: 'ul[appHighlight]', }) export class HighlightDirective { - readonly query = input('', { alias: 'appHighlight' }); + readonly query = input.required({ alias: 'appHighlight' }); private readonly labels = contentChildren>('highlightTarget', { descendants: true,