Added Navigation
This commit is contained in:
parent
3ce4a46a76
commit
6f069e8847
15 changed files with 249 additions and 85 deletions
|
@ -1,7 +1,15 @@
|
||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { RouterModule, Routes } from '@angular/router';
|
import { RouterModule, Routes } from '@angular/router';
|
||||||
|
import { DashboardComponent } from './dashboard/dashboard.component';
|
||||||
|
import { HeroDetailComponent } from './hero-detail/hero-detail.component';
|
||||||
|
import { HeroesComponent } from './heroes/heroes.component';
|
||||||
|
|
||||||
const routes: Routes = [];
|
const routes: Routes = [
|
||||||
|
{path: 'heroes', component: HeroesComponent},
|
||||||
|
{path: 'dashboard', component: DashboardComponent},
|
||||||
|
{path: 'detail/:id', component: HeroDetailComponent},
|
||||||
|
{path: '', redirectTo: '/dashboard', pathMatch: 'full'},
|
||||||
|
];
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
imports: [RouterModule.forRoot(routes)],
|
imports: [RouterModule.forRoot(routes)],
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
<h1>{{ title }}</h1>
|
<h1>{{ title }}</h1>
|
||||||
<app-heroes></app-heroes>
|
<nav>
|
||||||
|
<a routerLink="/dashboard">Dashboard</a>
|
||||||
|
<a routerLink="/heroes">Heroes</a>
|
||||||
|
</nav>
|
||||||
|
<router-outlet></router-outlet>
|
||||||
<app-messages></app-messages>
|
<app-messages></app-messages>
|
|
@ -0,0 +1,21 @@
|
||||||
|
/* AppComponent's private CSS styles */
|
||||||
|
h1 {
|
||||||
|
margin-bottom: 0;
|
||||||
|
}
|
||||||
|
nav a {
|
||||||
|
padding: 1rem;
|
||||||
|
text-decoration: none;
|
||||||
|
margin-top: 10px;
|
||||||
|
display: inline-block;
|
||||||
|
background-color: #e8e8e8;
|
||||||
|
color: #3d3d3d;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
nav a:hover {
|
||||||
|
color: white;
|
||||||
|
background-color: #42545C;
|
||||||
|
}
|
||||||
|
nav a:active {
|
||||||
|
background-color: black;
|
||||||
|
}
|
|
@ -7,13 +7,15 @@ import { AppComponent } from './app.component';
|
||||||
import { HeroesComponent } from './heroes/heroes.component';
|
import { HeroesComponent } from './heroes/heroes.component';
|
||||||
import { HeroDetailComponent } from './hero-detail/hero-detail.component';
|
import { HeroDetailComponent } from './hero-detail/hero-detail.component';
|
||||||
import { MessagesComponent } from './messages/messages.component';
|
import { MessagesComponent } from './messages/messages.component';
|
||||||
|
import { DashboardComponent } from './dashboard/dashboard.component';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
AppComponent,
|
AppComponent,
|
||||||
HeroesComponent,
|
HeroesComponent,
|
||||||
HeroDetailComponent,
|
HeroDetailComponent,
|
||||||
MessagesComponent
|
MessagesComponent,
|
||||||
|
DashboardComponent
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
BrowserModule,
|
BrowserModule,
|
||||||
|
|
6
src/app/dashboard/dashboard.component.html
Normal file
6
src/app/dashboard/dashboard.component.html
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<h2>Top Heroes</h2>
|
||||||
|
<div class="heroes-menu">
|
||||||
|
<a *ngFor="let hero of heroes" routerLink="/detail/{{hero.id}}">
|
||||||
|
{{hero.name}}
|
||||||
|
</a>
|
||||||
|
</div>
|
50
src/app/dashboard/dashboard.component.scss
Normal file
50
src/app/dashboard/dashboard.component.scss
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
/* DashboardComponent's private CSS styles */
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.heroes-menu {
|
||||||
|
padding: 0;
|
||||||
|
margin: auto;
|
||||||
|
max-width: 1000px;
|
||||||
|
|
||||||
|
/* flexbox */
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
justify-content: space-around;
|
||||||
|
align-content: flex-start;
|
||||||
|
align-items: flex-start;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
background-color: #3f525c;
|
||||||
|
border-radius: 2px;
|
||||||
|
padding: 1rem;
|
||||||
|
font-size: 1.2rem;
|
||||||
|
text-decoration: none;
|
||||||
|
display: inline-block;
|
||||||
|
color: #fff;
|
||||||
|
text-align: center;
|
||||||
|
width: 100%;
|
||||||
|
min-width: 70px;
|
||||||
|
margin: .5rem auto;
|
||||||
|
box-sizing: border-box;
|
||||||
|
|
||||||
|
/* flexbox */
|
||||||
|
order: 0;
|
||||||
|
flex: 0 1 auto;
|
||||||
|
align-self: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (min-width: 600px) {
|
||||||
|
a {
|
||||||
|
width: 18%;
|
||||||
|
box-sizing: content-box;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
a:hover {
|
||||||
|
background-color: #000;
|
||||||
|
}
|
23
src/app/dashboard/dashboard.component.spec.ts
Normal file
23
src/app/dashboard/dashboard.component.spec.ts
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { DashboardComponent } from './dashboard.component';
|
||||||
|
|
||||||
|
describe('DashboardComponent', () => {
|
||||||
|
let component: DashboardComponent;
|
||||||
|
let fixture: ComponentFixture<DashboardComponent>;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await TestBed.configureTestingModule({
|
||||||
|
declarations: [ DashboardComponent ]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
|
||||||
|
fixture = TestBed.createComponent(DashboardComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
23
src/app/dashboard/dashboard.component.ts
Normal file
23
src/app/dashboard/dashboard.component.ts
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
import { Hero } from '../hero';
|
||||||
|
import { HeroService } from '../hero.service';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-dashboard',
|
||||||
|
templateUrl: './dashboard.component.html',
|
||||||
|
styleUrls: ['./dashboard.component.scss']
|
||||||
|
})
|
||||||
|
export class DashboardComponent implements OnInit {
|
||||||
|
heroes: Hero[] = []
|
||||||
|
|
||||||
|
constructor(private heroService: HeroService) { }
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
this.getHeroes()
|
||||||
|
}
|
||||||
|
|
||||||
|
getHeroes(): void {
|
||||||
|
this.heroService.getHeroes().subscribe(heroes => this.heroes = heroes.slice(1,5))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,8 +1,9 @@
|
||||||
<div *ngIf="hero">
|
<div *ngIf="hero">
|
||||||
<h2>{{hero.name | uppercase}}</h2>
|
<h2>{{hero.name | uppercase}} Details</h2>
|
||||||
<div>id: {{hero.id}}</div>
|
<div>id: {{hero.id}}</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="hero-name">Hero name: </label>
|
<label for="hero-name">Hero name: </label>
|
||||||
<input id="hero-name" [(ngModel)]="hero.name" placeholder="name">
|
<input id="hero-name" [(ngModel)]="hero.name" placeholder="name">
|
||||||
</div>
|
</div>
|
||||||
|
<button type="button" (click)="goBack()">go back</button>
|
||||||
</div>
|
</div>
|
|
@ -0,0 +1,24 @@
|
||||||
|
/* HeroDetailComponent's private CSS styles */
|
||||||
|
label {
|
||||||
|
color: #435960;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
input {
|
||||||
|
font-size: 1em;
|
||||||
|
padding: .5rem;
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
margin-top: 20px;
|
||||||
|
background-color: #eee;
|
||||||
|
padding: 1rem;
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 1rem;
|
||||||
|
}
|
||||||
|
button:hover {
|
||||||
|
background-color: #cfd8dc;
|
||||||
|
}
|
||||||
|
button:disabled {
|
||||||
|
background-color: #eee;
|
||||||
|
color: #ccc;
|
||||||
|
cursor: auto;
|
||||||
|
}
|
|
@ -1,5 +1,8 @@
|
||||||
import { Component, OnInit,Input } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
import { Location } from '@angular/common';
|
||||||
|
import { ActivatedRoute } from '@angular/router';
|
||||||
import { Hero } from '../hero';
|
import { Hero } from '../hero';
|
||||||
|
import { HeroService } from '../hero.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-hero-detail',
|
selector: 'app-hero-detail',
|
||||||
|
@ -7,12 +10,26 @@ import { Hero } from '../hero';
|
||||||
styleUrls: ['./hero-detail.component.scss']
|
styleUrls: ['./hero-detail.component.scss']
|
||||||
})
|
})
|
||||||
export class HeroDetailComponent implements OnInit {
|
export class HeroDetailComponent implements OnInit {
|
||||||
|
hero: Hero | undefined
|
||||||
|
|
||||||
@Input() hero?: Hero
|
constructor(
|
||||||
|
private route: ActivatedRoute,
|
||||||
constructor() { }
|
private location: Location,
|
||||||
|
private heroService: HeroService,
|
||||||
|
) { }
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
this.getHero()
|
||||||
|
}
|
||||||
|
|
||||||
|
getHero(): void {
|
||||||
|
const id = Number(this.route.snapshot.paramMap.get('id'));
|
||||||
|
this.heroService.getHero(id)
|
||||||
|
.subscribe(hero => this.hero = hero);
|
||||||
|
}
|
||||||
|
|
||||||
|
goBack(): void {
|
||||||
|
this.location.back()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,4 +16,10 @@ export class HeroService {
|
||||||
this.messageService.add('HeroService: Heroes fetched!')
|
this.messageService.add('HeroService: Heroes fetched!')
|
||||||
return heroes
|
return heroes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getHero(id: number): Observable<Hero> {
|
||||||
|
const hero = HEROES.find(h => h.id === id)!
|
||||||
|
this.messageService.add(`HeroService: fetched hero id=${id}`)
|
||||||
|
return of(hero)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
<h2>Heroes</h2>
|
<h2>Heroes</h2>
|
||||||
<ul class="heroes">
|
<ul class="heroes">
|
||||||
<li *ngFor="let hero of heroes">
|
<li *ngFor="let hero of heroes">
|
||||||
<button [class.selected]="hero === selectedHero" type="button" (click)="onSelect(hero)" class="button">
|
<a routerLink="/detail/{{hero.id}}">
|
||||||
<span class="badge">{{hero.id}}</span>
|
<span class="badge">{{hero.id}}</span>{{hero.name}}
|
||||||
<span class="name">{{hero.name}}</span>
|
</a>
|
||||||
</button>
|
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
<app-hero-detail [hero]="selectedHero"></app-hero-detail>
|
|
|
@ -4,63 +4,51 @@
|
||||||
list-style-type: none;
|
list-style-type: none;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
width: 15em;
|
width: 15em;
|
||||||
}
|
}
|
||||||
|
.heroes li {
|
||||||
.heroes li {
|
|
||||||
display: flex;
|
|
||||||
}
|
|
||||||
|
|
||||||
.heroes button {
|
|
||||||
flex: 1;
|
|
||||||
cursor: pointer;
|
|
||||||
position: relative;
|
position: relative;
|
||||||
left: 0;
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.heroes li:hover {
|
||||||
|
left: .1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.heroes a {
|
||||||
|
color: #333;
|
||||||
|
text-decoration: none;
|
||||||
background-color: #EEE;
|
background-color: #EEE;
|
||||||
margin: .5em;
|
margin: .5em;
|
||||||
padding: 0;
|
padding: .3em 0;
|
||||||
|
height: 1.6em;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
display: flex;
|
display: block;
|
||||||
align-items: stretch;
|
width: 100%;
|
||||||
height: 1.8em;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
.heroes button:hover {
|
.heroes a:hover {
|
||||||
color: #2c3a41;
|
color: #2c3a41;
|
||||||
background-color: #e6e6e6;
|
background-color: #e6e6e6;
|
||||||
left: .1em;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
.heroes button:active {
|
.heroes a:active {
|
||||||
background-color: #525252;
|
background-color: #525252;
|
||||||
color: #fafafa;
|
color: #fafafa;
|
||||||
}
|
}
|
||||||
|
|
||||||
.heroes button.selected {
|
.heroes .badge {
|
||||||
background-color: black;
|
|
||||||
color: white;
|
|
||||||
}
|
|
||||||
|
|
||||||
.heroes button.selected:hover {
|
|
||||||
background-color: #505050;
|
|
||||||
color: white;
|
|
||||||
}
|
|
||||||
|
|
||||||
.heroes button.selected:active {
|
|
||||||
background-color: black;
|
|
||||||
color: white;
|
|
||||||
}
|
|
||||||
|
|
||||||
.heroes .badge {
|
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
font-size: small;
|
font-size: small;
|
||||||
color: white;
|
color: white;
|
||||||
padding: 0.8em 0.7em 0 0.7em;
|
padding: 0.8em 0.7em 0 0.7em;
|
||||||
background-color: #405061;
|
background-color: #405061;
|
||||||
line-height: 1em;
|
line-height: 1em;
|
||||||
|
position: relative;
|
||||||
|
left: -1px;
|
||||||
|
top: -4px;
|
||||||
|
height: 1.8em;
|
||||||
|
min-width: 16px;
|
||||||
|
text-align: right;
|
||||||
margin-right: .8em;
|
margin-right: .8em;
|
||||||
border-radius: 4px 0 0 4px;
|
border-radius: 4px 0 0 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.heroes .name {
|
|
||||||
align-self: center;
|
|
||||||
}
|
|
|
@ -12,13 +12,6 @@ export class HeroesComponent implements OnInit {
|
||||||
|
|
||||||
constructor(private heroService: HeroService, private messageService: MessageService) { }
|
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: Hero[] = []
|
heroes: Hero[] = []
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
|
Loading…
Reference in a new issue