-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
387 additions
and
68 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,23 @@ | ||
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; | ||
import { | ||
ApplicationConfig, | ||
importProvidersFrom, | ||
provideZoneChangeDetection, | ||
} from '@angular/core'; | ||
import { provideRouter } from '@angular/router'; | ||
|
||
import { routes } from './app.routes'; | ||
import { provideHttpClient } from '@angular/common/http'; | ||
import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api'; | ||
import { InMemoryDataService } from './in-memory-data.service'; | ||
|
||
export const appConfig: ApplicationConfig = { | ||
providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes)] | ||
providers: [ | ||
provideHttpClient(), | ||
provideRouter(routes), | ||
importProvidersFrom([ | ||
HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService, { | ||
dataEncapsulation: false, | ||
}), | ||
]), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
import { Routes } from '@angular/router'; | ||
import { HeroesComponent } from './heroes/heroes.component'; | ||
import { DashboardComponent } from './dashboard/dashboard.component'; | ||
import { HeroDetailComponent } from './hero-detail/hero-detail.component'; | ||
|
||
export const routes: Routes = []; | ||
export const routes: Routes = [ | ||
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' }, | ||
{ path: 'heroes', component: HeroesComponent }, | ||
{ path: 'dashboard', component: DashboardComponent }, | ||
{ path: 'detail/:id', component: HeroDetailComponent }, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ <h2>Top Heroes</h2> | |
{{hero.name}} | ||
</a> | ||
</div> | ||
<app-hero-search></app-hero-search> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* HeroSearch private styles */ | ||
|
||
label { | ||
display: block; | ||
font-weight: bold; | ||
font-size: 1.2rem; | ||
margin-top: 1rem; | ||
margin-bottom: .5rem; | ||
|
||
} | ||
input { | ||
padding: .5rem; | ||
width: 100%; | ||
max-width: 600px; | ||
box-sizing: border-box; | ||
display: block; | ||
} | ||
|
||
input:focus { | ||
outline: #336699 auto 1px; | ||
} | ||
|
||
li { | ||
list-style-type: none; | ||
} | ||
.search-result li a { | ||
border-bottom: 1px solid gray; | ||
border-left: 1px solid gray; | ||
border-right: 1px solid gray; | ||
display: inline-block; | ||
width: 100%; | ||
max-width: 600px; | ||
padding: .5rem; | ||
box-sizing: border-box; | ||
text-decoration: none; | ||
color: black; | ||
} | ||
|
||
.search-result li a:hover { | ||
background-color: #435A60; | ||
color: white; | ||
} | ||
|
||
ul.search-result { | ||
margin-top: 0; | ||
padding-left: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div id="search-component"> | ||
<label for="search-box">Hero Search</label> | ||
<input #searchBox id="search-box" (input)="search(searchBox.value)" /> | ||
|
||
<ul class="search-result"> | ||
<li *ngFor="let hero of heroes$ | async" > | ||
<a routerLink="/detail/{{hero.id}}"> | ||
{{hero.name}} | ||
</a> | ||
</li> | ||
</ul> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { HeroSearchComponent } from './hero-search.component'; | ||
|
||
describe('HeroSearchComponent', () => { | ||
let component: HeroSearchComponent; | ||
let fixture: ComponentFixture<HeroSearchComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [HeroSearchComponent] | ||
}) | ||
.compileComponents(); | ||
|
||
fixture = TestBed.createComponent(HeroSearchComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Observable, Subject } from 'rxjs'; | ||
import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; | ||
import { Hero } from '../hero'; | ||
import { HeroService } from '../hero.service'; | ||
import { CommonModule } from '@angular/common'; | ||
import { RouterLink } from '@angular/router'; | ||
|
||
@Component({ | ||
selector: 'app-hero-search', | ||
standalone: true, | ||
imports: [CommonModule, RouterLink], | ||
templateUrl: './hero-search.component.html', | ||
styleUrl: './hero-search.component.css', | ||
}) | ||
export class HeroSearchComponent implements OnInit { | ||
heroes$!: Observable<Hero[]>; | ||
private searchTerms = new Subject<string>(); | ||
|
||
constructor(private heroService: HeroService) {} | ||
|
||
// Push a search term into the observable stream. | ||
search(term: string): void { | ||
this.searchTerms.next(term); | ||
} | ||
|
||
ngOnInit(): void { | ||
this.heroes$ = this.searchTerms.pipe( | ||
// wait 300ms after each keystroke before considering the term | ||
debounceTime(300), | ||
|
||
// ignore new term if same as previous term | ||
distinctUntilChanged(), | ||
|
||
// switch to new search observable each time the term changes | ||
switchMap((term: string) => this.heroService.searchHeroes(term)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.