Skip to content

Commit b7fb497

Browse files
committed
feat(hero): added fields to Hero class, created a form to add a new hero, and show details
1 parent b890735 commit b7fb497

11 files changed

Lines changed: 58 additions & 27 deletions

app/app.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const AppConfig: IAppConfig = {
1616
heroById: heroesRoute + '/:id'
1717
},
1818
endpoints: {
19-
heroes: 'api/heroes'
19+
heroes: 'api/heroes',
20+
heroesPowers: 'api/heroesPowers'
2021
}
2122
};

app/heroes/hero-detail/hero-detail.component.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
label {
22
display: inline-block;
3-
width: 3em;
3+
width: 5em;
44
margin: .5em 0;
55
color: #607D8B;
66
font-weight: bold;

app/heroes/hero-detail/hero-detail.component.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ <h2>{{hero.name}} details!</h2>
77
<label>name: </label>
88
<input [(ngModel)]="hero.name" placeholder="name"/>
99
</div>
10+
<div>
11+
<label>alterEgo: </label>
12+
<input [(ngModel)]="hero.alterEgo" placeholder="alterEgo"/>
13+
</div>
1014
<button (click)="goBack()">Back</button>
11-
<button (click)="save()">Save</button>
15+
<button (click)="saveHero()">Save hero</button>
1216
</div>

app/heroes/hero-detail/hero-detail.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export class HeroDetailComponent implements OnInit {
2525
.switchMap((params: Params) => this.heroService.getHeroById(+params[ 'id' ]))
2626
.subscribe(hero => this.hero = hero);
2727
}
28-
29-
save(): void {
28+
29+
saveHero(): void {
3030
this.heroService.update(this.hero)
3131
.then(() => this.goBack());
3232
}

app/heroes/hero-form/hero-form.component.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
.form-label {
2+
display: inline-block;
3+
width: 10%;
4+
text-align: left;
5+
}
6+
7+
.form-button {
8+
font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
9+
background-color: #eee;
10+
border: none;
11+
padding: 5px 10px;
12+
border-radius: 4px;
13+
cursor: hand;
14+
margin: 1.5em 0 0 0;
15+
}
16+
117
.ng-valid[required], .ng-valid.required {
218
border-left: 5px solid #42A948;
319
}

app/heroes/hero-form/hero-form.component.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<h1>New hero</h1>
44
<form (ngSubmit)="onSubmit()" #heroForm="ngForm">
55
<div class="form-group">
6-
<label for="name">Name</label>
6+
<label class="form-label" for="name">Name</label>
77
<input type="text" class="form-control" id="name"
88
required
99
[(ngModel)]="hero.name" name="name"
@@ -14,19 +14,19 @@ <h1>New hero</h1>
1414
</div>
1515
</div>
1616
<div class="form-group">
17-
<label for="alterEgo">Alter Ego</label>
17+
<label class="form-label" for="alterEgo">Alter Ego</label>
1818
<input type="text" class="form-control" id="alterEgo"
1919
[(ngModel)]="hero.alterEgo" name="alterEgo">
2020
</div>
2121
<div class="form-group">
22-
<label for="power">Hero Power</label>
22+
<label class="form-label" for="power">Hero Power</label>
2323
<select class="form-control" id="power"
2424
required
2525
[(ngModel)]="hero.power" name="power">
2626
<option *ngFor="let power of powers" [value]="power">{{power}}</option>
2727
</select>
2828
</div>
29-
<button type="submit" class="btn btn-default" [disabled]="!heroForm.form.valid">Create new hero!</button>
29+
<button type="submit" class="button form-button" [disabled]="!heroForm.form.valid">Create new hero!</button>
3030
</form>
3131
</div>
3232
<div [hidden]="!submitted">

app/heroes/hero-form/hero-form.component.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ export class HeroFormComponent {
2222
constructor(private heroService: HeroService) {
2323
this.hero = new Hero(-1, '', '');
2424
this.submitted = false;
25-
26-
// TODO
27-
this.powers = [ 'Really Smart', 'Super Flexible',
28-
'Super Hot', 'Weather Changer' ];
25+
26+
this.heroService.getHeroesPowers().then(powers => this.powers = powers);
2927
}
30-
28+
3129
onSubmit() {
3230
this.heroService.create(this.hero)
3331
.then(hero => {

app/heroes/hero-list/hero-list.component.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
margin: 0 0 2em 0;
88
list-style-type: none;
99
padding: 0;
10-
width: 23em;
10+
width: 40%;
1111
}
1212

1313
.heroes li {

app/heroes/hero-search/hero-search.component.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
list-style-type: none;
1414
margin: 0;
1515
padding: 0;
16-
position: fixed;
16+
position: absolute;
1717
z-index: 20;
1818
}
1919

app/heroes/shared/hero.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export class HeroService {
2424
.then(response => response.json().data as Hero[])
2525
.catch(this.handleError);
2626
}
27+
28+
getHeroesPowers(): Promise<string[]> {
29+
return this.http.get(this.appConfig.endpoints.heroesPowers)
30+
.toPromise()
31+
.then(response => response.json().data as string[])
32+
.catch(this.handleError);
33+
}
2734

2835
getHeroById(id: number): Promise<Hero> {
2936
const url = `${this.heroesUrl}/${id}`;

0 commit comments

Comments
 (0)