From 3ce4a46a76eea75a80c026db70f963ab00d51dc0 Mon Sep 17 00:00:00 2001 From: nikurasu Date: Sat, 24 Sep 2022 19:04:42 +0200 Subject: [PATCH] Completed Angular Services Chapter --- src/app/app.component.html | 3 ++- src/app/app.module.ts | 4 +++- src/app/hero.service.spec.ts | 16 ++++++++++++++ src/app/hero.service.ts | 19 +++++++++++++++++ src/app/heroes/heroes.component.ts | 14 ++++++++++--- src/app/messages.service.spec.ts | 16 ++++++++++++++ src/app/messages.service.ts | 18 ++++++++++++++++ src/app/messages/messages.component.html | 6 ++++++ src/app/messages/messages.component.scss | 19 +++++++++++++++++ src/app/messages/messages.component.spec.ts | 23 +++++++++++++++++++++ src/app/messages/messages.component.ts | 16 ++++++++++++++ 11 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 src/app/hero.service.spec.ts create mode 100644 src/app/hero.service.ts create mode 100644 src/app/messages.service.spec.ts create mode 100644 src/app/messages.service.ts create mode 100644 src/app/messages/messages.component.html create mode 100644 src/app/messages/messages.component.scss create mode 100644 src/app/messages/messages.component.spec.ts create mode 100644 src/app/messages/messages.component.ts diff --git a/src/app/app.component.html b/src/app/app.component.html index ee3c252..b9bf817 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,2 +1,3 @@

{{ title }}

- \ No newline at end of file + + \ No newline at end of file diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 3317136..9eaf3b9 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -6,12 +6,14 @@ import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { HeroesComponent } from './heroes/heroes.component'; import { HeroDetailComponent } from './hero-detail/hero-detail.component'; +import { MessagesComponent } from './messages/messages.component'; @NgModule({ declarations: [ AppComponent, HeroesComponent, - HeroDetailComponent + HeroDetailComponent, + MessagesComponent ], imports: [ BrowserModule, diff --git a/src/app/hero.service.spec.ts b/src/app/hero.service.spec.ts new file mode 100644 index 0000000..42311f5 --- /dev/null +++ b/src/app/hero.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { HeroService } from './hero.service'; + +describe('HeroService', () => { + let service: HeroService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(HeroService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/hero.service.ts b/src/app/hero.service.ts new file mode 100644 index 0000000..5a5f136 --- /dev/null +++ b/src/app/hero.service.ts @@ -0,0 +1,19 @@ +import { Injectable } from '@angular/core'; +import { Observable, of } from 'rxjs'; +import { Hero } from './hero'; +import { MessageService } from './messages.service'; +import { HEROES } from './mock-heroes'; + +@Injectable({ + providedIn: 'root' +}) +export class HeroService { + + constructor(private messageService: MessageService) { } + + getHeroes(): Observable { + const heroes = of(HEROES) + this.messageService.add('HeroService: Heroes fetched!') + return heroes + } +} diff --git a/src/app/heroes/heroes.component.ts b/src/app/heroes/heroes.component.ts index ad67a77..42beb29 100644 --- a/src/app/heroes/heroes.component.ts +++ b/src/app/heroes/heroes.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { Hero } from '../hero'; -import { HEROES } from '../mock-heroes'; +import { HeroService } from '../hero.service'; +import { MessageService } from '../messages.service'; @Component({ selector: 'app-heroes', @@ -9,16 +10,23 @@ import { HEROES } from '../mock-heroes'; }) export class HeroesComponent implements OnInit { - constructor() { } + constructor(private heroService: HeroService, private messageService: MessageService) { } selectedHero?: Hero + onSelect(hero: Hero): void { this.selectedHero = hero + this.messageService.add(`Hero Component: Selected hero id=${hero.id}`) } - heroes = HEROES + heroes: Hero[] = [] ngOnInit(): void { + this.getHeroes() + } + + getHeroes(): void { + this.heroService.getHeroes().subscribe(heroes => this.heroes = heroes) } } diff --git a/src/app/messages.service.spec.ts b/src/app/messages.service.spec.ts new file mode 100644 index 0000000..c09e291 --- /dev/null +++ b/src/app/messages.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { MessageService } from './messages.service'; + +describe('MessagesService', () => { + let service: MessageService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(MessageService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/messages.service.ts b/src/app/messages.service.ts new file mode 100644 index 0000000..1e9b8f1 --- /dev/null +++ b/src/app/messages.service.ts @@ -0,0 +1,18 @@ +import { Injectable } from '@angular/core'; + +@Injectable({ + providedIn: 'root' +}) +export class MessageService { + messages: string[] = [] + + add(message: string) { + this.messages.push(message) + } + + clear() { + this.messages = [] + } + + constructor() { } +} diff --git a/src/app/messages/messages.component.html b/src/app/messages/messages.component.html new file mode 100644 index 0000000..5467660 --- /dev/null +++ b/src/app/messages/messages.component.html @@ -0,0 +1,6 @@ +
+ +

Messages

+ +
{{message}}
+
diff --git a/src/app/messages/messages.component.scss b/src/app/messages/messages.component.scss new file mode 100644 index 0000000..65eb04b --- /dev/null +++ b/src/app/messages/messages.component.scss @@ -0,0 +1,19 @@ +/* MessagesComponent's private CSS styles */ +h2 { + color: #A80000; + font-family: Arial, Helvetica, sans-serif; + font-weight: lighter; + } + + .clear { + color: #333; + background-color: #eee; + margin-bottom: 12px; + padding: 1rem; + border-radius: 4px; + font-size: 1rem; + } + .clear:hover { + color: white; + background-color: #42545C; + } \ No newline at end of file diff --git a/src/app/messages/messages.component.spec.ts b/src/app/messages/messages.component.spec.ts new file mode 100644 index 0000000..a674793 --- /dev/null +++ b/src/app/messages/messages.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { MessagesComponent } from './messages.component'; + +describe('MessagesComponent', () => { + let component: MessagesComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ MessagesComponent ] + }) + .compileComponents(); + + fixture = TestBed.createComponent(MessagesComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/messages/messages.component.ts b/src/app/messages/messages.component.ts new file mode 100644 index 0000000..9d620f3 --- /dev/null +++ b/src/app/messages/messages.component.ts @@ -0,0 +1,16 @@ +import { Component, OnInit } from '@angular/core'; +import { MessageService } from '../messages.service'; + +@Component({ + selector: 'app-messages', + templateUrl: './messages.component.html', + styleUrls: ['./messages.component.scss'] +}) +export class MessagesComponent implements OnInit { + + constructor(public messageService: MessageService) { } + + ngOnInit(): void { + } + +}