Skip to content

Commit

Permalink
Implemented services and routes
Browse files Browse the repository at this point in the history
  • Loading branch information
pateatlau committed Mar 27, 2019
1 parent 02facf9 commit a2dadb1
Show file tree
Hide file tree
Showing 28 changed files with 490 additions and 29 deletions.
4 changes: 4 additions & 0 deletions src/app/Hero.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class Hero {
id: number;
name: string;
}
11 changes: 9 additions & 2 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {
Routes,
RouterModule
} from '@angular/router';

const routes: Routes = [];
import { HeroesComponent } from './heroes/heroes.component';

const routes: Routes = [
{ path: 'heroes', component: HeroesComponent }
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
Expand Down
25 changes: 5 additions & 20 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">
<h1>
Welcome to {{ title }}!
</h1>
<img width="300" alt="Angular Logo" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==">
</div>
<h2>Here are some links to help you start: </h2>
<ul>
<li>
<h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2>
</li>
<li>
<h2><a target="_blank" rel="noopener" href="https://angular.io/cli">CLI Documentation</a></h2>
</li>
<li>
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
</li>
</ul>

<h1>{{title}}</h1>
<nav>
<a routerLink="/heroes">Heroes</a>
</nav>
<router-outlet></router-outlet>
<app-messages></app-messages>
35 changes: 35 additions & 0 deletions src/app/app.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* AppComponent's private CSS styles */
h1 {
font-size: 1.2em;
color: #999;
margin-bottom: 0;
}

h2 {
font-size: 2em;
margin-top: 0;
padding-top: 0;
}

nav a {
padding: 5px 10px;
text-decoration: none;
margin-top: 10px;
display: inline-block;
background-color: #eee;
border-radius: 4px;
}

nav a:visited,
a:link {
color: #607d8b;
}

nav a:hover {
color: #039be5;
background-color: #cfd8dc;
}

nav a.active {
color: #039be5;
}
2 changes: 1 addition & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import { Component } from '@angular/core';
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'angular-tour-of-heroes';
title = 'Angular Tour of Heroes';
}
26 changes: 21 additions & 5 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
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';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard/dashboard.component';

@NgModule({
declarations: [
AppComponent
AppComponent,
HeroesComponent,
HeroDetailComponent,
MessagesComponent,
DashboardComponent,
],

imports: [
AppRoutingModule,
BrowserModule,
AppRoutingModule
FormsModule,
],
providers: [],
bootstrap: [AppComponent]

providers: [
// no need to place any providers due to the `providedIn` flag...
],

bootstrap: [ AppComponent ]
})

export class AppModule { }
3 changes: 3 additions & 0 deletions src/app/dashboard/dashboard.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>
dashboard works!
</p>
Empty file.
25 changes: 25 additions & 0 deletions src/app/dashboard/dashboard.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { DashboardComponent } from './dashboard.component';

describe('DashboardComponent', () => {
let component: DashboardComponent;
let fixture: ComponentFixture<DashboardComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ DashboardComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(DashboardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
15 changes: 15 additions & 0 deletions src/app/dashboard/dashboard.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {

constructor() { }

ngOnInit() {
}

}
9 changes: 9 additions & 0 deletions src/app/hero-detail/hero-detail.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div *ngIf="hero">
<h2>{{hero.name | uppercase}} Details</h2>
<div><span>id: </span>{{hero.id}}</div>
<div>
<label>name:
<input [(ngModel)]="hero.name" placeholder="name"/>
</label>
</div>
</div>
Empty file.
25 changes: 25 additions & 0 deletions src/app/hero-detail/hero-detail.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { HeroDetailComponent } from './hero-detail.component';

describe('HeroDetailComponent', () => {
let component: HeroDetailComponent;
let fixture: ComponentFixture<HeroDetailComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ HeroDetailComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(HeroDetailComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
17 changes: 17 additions & 0 deletions src/app/hero-detail/hero-detail.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Component, OnInit, Input } from '@angular/core';
import { Hero } from '../hero';

@Component({
selector: 'app-hero-detail',
templateUrl: './hero-detail.component.html',
styleUrls: ['./hero-detail.component.scss']
})
export class HeroDetailComponent implements OnInit {
@Input() hero: Hero;

constructor() { }

ngOnInit() {
}

}
12 changes: 12 additions & 0 deletions src/app/hero.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';

import { HeroService } from './hero.service';

describe('HeroService', () => {
beforeEach(() => TestBed.configureTestingModule({}));

it('should be created', () => {
const service: HeroService = TestBed.get(HeroService);
expect(service).toBeTruthy();
});
});
21 changes: 21 additions & 0 deletions src/app/hero.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';

import { Observable, of } from 'rxjs';

import { Hero } from './hero';
import { HEROES } from './mock-heroes';
import { MessageService } from './message.service';

@Injectable({
providedIn: 'root',
})
export class HeroService {

constructor(private messageService: MessageService) { }

getHeroes(): Observable<Hero[]> {
// TODO: send the message _after_ fetching the heroes
this.messageService.add('HeroService: fetched heroes');
return of(HEROES);
}
}
18 changes: 18 additions & 0 deletions src/app/heroes/heroes.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h2>My Heroes</h2>
<ul class="heroes">
<li *ngFor="let hero of heroes" (click)="onSelect(hero)" [class.selected]="hero === selectedHero">
<span class="badge">{{hero.id}}</span> {{hero.name}}
</li>
</ul>

<app-hero-detail [hero]="selectedHero"></app-hero-detail>

<!-- <div *ngIf="selectedHero">
<h2>{{ selectedHero?.name | uppercase }} details:</h2>
<p>Id: {{ selectedHero?.id }}</p>
<div>
<label>name:
<input [(ngModel)]="selectedHero.name" placeholder="name"/>
</label>
</div>
</div> -->
54 changes: 54 additions & 0 deletions src/app/heroes/heroes.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* HeroesComponent's private CSS styles */
.selected {
background-color: #CFD8DC !important;
color: white;
}

.heroes {
margin: 0 0 2em 0;
list-style-type: none;
padding: 0;
width: 15em;
}

.heroes li {
cursor: pointer;
position: relative;
left: 0;
background-color: #EEE;
margin: .5em;
padding: .3em 0;
height: 1.6em;
border-radius: 4px;
}

.heroes li.selected:hover {
background-color: #BBD8DC !important;
color: white;
}

.heroes li:hover {
color: #607D8B;
background-color: #DDD;
left: .1em;
}

.heroes .text {
position: relative;
top: -3px;
}

.heroes .badge {
display: inline-block;
font-size: small;
color: white;
padding: 0.8em 0.7em 0 0.7em;
background-color: #607D8B;
line-height: 1em;
position: relative;
left: -1px;
top: -4px;
height: 1.8em;
margin-right: .8em;
border-radius: 4px 0 0 4px;
}
25 changes: 25 additions & 0 deletions src/app/heroes/heroes.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { HeroesComponent } from './heroes.component';

describe('HeroesComponent', () => {
let component: HeroesComponent;
let fixture: ComponentFixture<HeroesComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ HeroesComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(HeroesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
31 changes: 31 additions & 0 deletions src/app/heroes/heroes.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Component, OnInit } from '@angular/core';

import { Hero } from '../hero';
import { HeroService } from '../hero.service';

@Component({
selector: 'app-heroes',
templateUrl: './heroes.component.html',
styleUrls: ['./heroes.component.scss']
})
export class HeroesComponent implements OnInit {

selectedHero: Hero;

heroes: Hero[];

constructor(private heroService: HeroService) { }

ngOnInit() {
this.getHeroes();
}

onSelect(hero: Hero): void {
this.selectedHero = hero;
}

getHeroes(): void {
this.heroService.getHeroes()
.subscribe(heroes => this.heroes = heroes);
}
}
Loading

0 comments on commit a2dadb1

Please sign in to comment.