-
Notifications
You must be signed in to change notification settings - Fork 0
/
combatant.ts
64 lines (59 loc) · 1.77 KB
/
combatant.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
The Combatant component, responsible for rendering a single combatant.
Rendered by the Battle component.
The combatant component has a form where the user can load data for a Github user
If a user is loaded it will display that data through a CombatantDetail component,
and output the startcount to the parent.
*/
import { Component, Output, EventEmitter } from '@angular/core';
import { BattleService } from '../services/battleservice';
type CombatantMode = 'empty' | 'loading' | 'error' | 'data';
@Component({
selector: 'combatant',
template: `
<div [class.winner]="winner">
<input placeholder="Github user name" [(ngModel)]="field" (keyup.enter)="loadData()">
<button (click)="loadData()" [disabled]="!canLoad">Load</button>
<div *ngIf="mode === 'loading'">...loading...</div>
<div *ngIf="mode === 'error'">Oh no, something wen't wrong :(</div>
<div *ngIf="mode === 'data'">
<combatantdetail [data]="data"></combatantdetail>
</div>
</div>
`,
styles: [`
:host {
width: 300px;
display: block;
padding: 5px;
}
`]
})
export class CombatantComponent {
mode: CombatantMode = 'empty'
field = ""
data = null
@Output() stars = new EventEmitter<any>()
constructor(private battleService: BattleService){}
get canLoad() {
return this.field && this.mode != 'loading';
}
loadData() {
if (this.canLoad){
this.mode = 'loading';
this.stars.emit(null);
this.battleService.battleInfoForUser(this.field).subscribe(
data => {
this.data = data;
this.mode = 'data';
this.stars.emit(data.repos.stars);
this.field = '';
},
error => {
this.mode = 'error';
this.stars.emit(null);
}
);
}
}
}