feat: simplify app structure
This commit is contained in:
parent
ed21210a07
commit
8a6a28f67c
@ -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;
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
<form class="search-form">
|
||||
<input
|
||||
type="search"
|
||||
[formField]="searchForm.query"
|
||||
class="search"
|
||||
placeholder="Disease or Code"
|
||||
/>
|
||||
|
||||
@let error = diseaseService.error();
|
||||
|
||||
@if (error) {
|
||||
<p class="error">{{`HTTP error ${error.message}`}}</p>
|
||||
} @else {
|
||||
<ul [appHighlight]="searchTerm()" class="suggestions">
|
||||
@for (disease of filteredDiseases(); track $index) {
|
||||
<li>
|
||||
<span class="details">
|
||||
<span class="name" #highlightTarget>{{ disease.name }}</span>,
|
||||
<span class="code" #highlightTarget>{{ disease.code }}</span>
|
||||
</span>
|
||||
|
||||
<span class="abbr">{{ disease.abbr }}</span>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
</form>
|
||||
|
||||
<p class="info">
|
||||
Using JavaScript, replicate the
|
||||
<a
|
||||
href="https://medicalvalues.de/wp-content/uploads/2021/01/js_challenge.mp4"
|
||||
target="_blank"
|
||||
>functionality found here</a
|
||||
>. <br />
|
||||
The solution does not have to be perfect. Most important is to see how you
|
||||
approach such tasks.
|
||||
</p>
|
||||
@ -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: `
|
||||
<app-disease-typeahead />
|
||||
|
||||
<p class="info">
|
||||
Using JavaScript, replicate the
|
||||
<a href="https://medicalvalues.de/wp-content/uploads/2021/01/js_challenge.mp4" target="_blank"
|
||||
>functionality found here</a
|
||||
>. <br />
|
||||
The solution does not have to be perfect. Most important is to see how you approach such
|
||||
tasks.
|
||||
</p>
|
||||
`,
|
||||
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 {}
|
||||
|
||||
63
src/app/disease-typeahead/disease-typeahead.component.ts
Normal file
63
src/app/disease-typeahead/disease-typeahead.component.ts
Normal file
@ -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: `
|
||||
<search class="search-form">
|
||||
<app-disease-search [field]="searchForm.query" />
|
||||
|
||||
@if (diseaseService.error(); as error) {
|
||||
<p class="error">HTTP error {{ error.message }}</p>
|
||||
} @else {
|
||||
<app-disease-suggestions [diseases]="filteredDiseases()" [query]="searchTerm()" />
|
||||
}
|
||||
</search>
|
||||
`,
|
||||
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),
|
||||
);
|
||||
});
|
||||
}
|
||||
1
src/app/disease-typeahead/index.ts
Normal file
1
src/app/disease-typeahead/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export { DiseaseTypeaheadComponent } from './disease-typeahead.component';
|
||||
2
src/app/disease-typeahead/partials/index.ts
Normal file
2
src/app/disease-typeahead/partials/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export { AppDiseaseSearchComponent } from './search/search.component';
|
||||
export { AppDiseaseSuggestionsComponent } from './suggestions/suggestions.component';
|
||||
@ -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: `
|
||||
<input
|
||||
type="search"
|
||||
class="search"
|
||||
[formField]="field()"
|
||||
placeholder="Disease or Code"
|
||||
aria-label="Search diseases"
|
||||
/>
|
||||
`,
|
||||
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<FieldTree<string>>();
|
||||
}
|
||||
@ -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: `
|
||||
<ul [appHighlight]="query()" class="suggestions">
|
||||
@for (disease of diseases(); track disease) {
|
||||
<li>
|
||||
<span class="details">
|
||||
<span class="name" #highlightTarget>{{ disease.name }}</span
|
||||
>,
|
||||
<span class="code" #highlightTarget>{{ disease.code }}</span>
|
||||
</span>
|
||||
|
||||
<span class="abbr">{{ disease.abbr }}</span>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
`,
|
||||
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 Disease[]>();
|
||||
readonly query = input.required<string>();
|
||||
}
|
||||
@ -12,7 +12,7 @@ import {
|
||||
selector: 'ul[appHighlight]',
|
||||
})
|
||||
export class HighlightDirective {
|
||||
readonly query = input('', { alias: 'appHighlight' });
|
||||
readonly query = input.required<string>({ alias: 'appHighlight' });
|
||||
|
||||
private readonly labels = contentChildren<ElementRef<HTMLElement>>('highlightTarget', {
|
||||
descendants: true,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user