Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hero details, routes, params #13

Merged
merged 4 commits into from
Jun 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
"@angular/router": "^6.0.5",
"bootstrap": "^4.0.0-beta.2",
"core-js": "^2.4.1",
"ngx-take-until-destroy": "^3.0.0",
"rxjs": "^6.2.1",
"zone.js": "^0.8.26",
"rxjs-compat": "^6.0.0-rc.0"
"rxjs-compat": "^6.0.0-rc.0",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.6.8",
Expand Down
7 changes: 0 additions & 7 deletions src/app/app.component.scss
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
nav {
a {
&.active {
font-weight: bold;
}
}
}
12 changes: 12 additions & 0 deletions src/app/heroes/hero-details/hero-details.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

<h3>Hero list</h3>
<ul>
<li *ngFor="let hero of heroes; trackBy: byId">
<a [routerLink]="[ '/heroes', hero.id ]" routerLinkActive="active">{{ hero.name }}</a>
</li>
</ul>

<hr>

<h3>Hero</h3>
<pre>{{ activeHero | json }}</pre>
Empty file.
26 changes: 26 additions & 0 deletions src/app/heroes/hero-details/hero-details.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { HeroDetailsComponent } from './hero-details.component';

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

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [HeroDetailsComponent],
imports: [RouterTestingModule],
}).compileComponents();
}));

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

it('should create', () => {
expect(component).toBeTruthy();
});
});
40 changes: 40 additions & 0 deletions src/app/heroes/hero-details/hero-details.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { TakeUntilDestroy, untilDestroyed } from 'ngx-take-until-destroy';
import { map } from 'rxjs/operators';

import { Hero } from '../shared/hero.model';

@Component({
selector: 'app-hero-details',
templateUrl: './hero-details.component.html',
styleUrls: ['./hero-details.component.scss'],
})
@TakeUntilDestroy()
export class HeroDetailsComponent implements OnInit, OnDestroy {
heroes = [
new Hero({ id: 1, name: 'Saitama' }),
new Hero({ id: 2, name: 'Goku' }),
new Hero({ id: 3, name: 'All Might' }),
];

activeHero: Hero;

constructor(private route: ActivatedRoute) {}

ngOnInit() {
this.route.paramMap
.pipe(map((params) => +params.get('id')))
.pipe(untilDestroyed(this))
.subscribe((id) => {
this.activeHero = this.heroes.find((hero) => hero.id === id);
});
}

ngOnDestroy() {
}

byId(_, hero: Hero) {
return hero.id;
}
}
5 changes: 2 additions & 3 deletions src/app/heroes/heroes.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ describe('HeroesComponent', () => {

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

beforeEach(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/app/heroes/heroes.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';

import { ReactiveFormsModule } from '../../../node_modules/@angular/forms';
import { HeroDetailsComponent } from './hero-details/hero-details.component';
import { HeroEditComponent } from './hero-edit/hero-edit.component';
import { HeroesComponent } from './heroes.component';
import { RoutingModule } from './heroes.router';

@NgModule({
imports: [RoutingModule, CommonModule, ReactiveFormsModule],
declarations: [HeroesComponent, HeroEditComponent],
declarations: [HeroesComponent, HeroEditComponent, HeroDetailsComponent],
})
export class HeroesModule {}
6 changes: 6 additions & 0 deletions src/app/heroes/heroes.router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RouterModule, Routes } from '@angular/router';

import { HeroDetailsComponent } from './hero-details/hero-details.component';
import { HeroEditComponent } from './hero-edit/hero-edit.component';
import { HeroesComponent } from './heroes.component';

Expand All @@ -13,6 +14,11 @@ const routes: Routes = [
path: 'new',
component: HeroEditComponent,
},

{
path: ':id',
component: HeroDetailsComponent,
},
];

export const RoutingModule = RouterModule.forChild(routes);
8 changes: 8 additions & 0 deletions src/app/heroes/shared/hero.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class Hero {
id: number;
name: string;

constructor(hero: Partial<Hero>) {
Object.assign(this, hero);
}
}
6 changes: 6 additions & 0 deletions src/assets/css/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ legend {
padding: .2em .5em;
}

a {
&.active {
font-weight: bold;
}
}

form {
.ng-touched, .ng-dirty {
&.ng-invalid {
Expand Down