forked from uguratar/codefiction-top
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathregister.component.ts
More file actions
67 lines (59 loc) · 2.3 KB
/
register.component.ts
File metadata and controls
67 lines (59 loc) · 2.3 KB
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
65
66
67
import { Component, OnInit } from '@angular/core';
import { SocketService } from '../socket.service';
import { Datastructure } from '../../../Datastructure/TopperStack';
import { Router } from '@angular/router';
@Component({
template: `<div class="o-container o-container--small">
<div class="u-center-block" style="height: 20px;">
</div>
<form id="addUserForm" name="addUserForm" onsubmit="return false;">
<div class="o-grid">
<div class="o-grid__cell o-grid__cell--width-60">
<input type="text" name="userName" id="name" placeholder="e.g: Nickname" [class.c-field--error]="usernameInvalid" [(ngModel)]="currentTopper.name" class="c-field">
<br />
<input type="text" name="roomName" id="name" placeholder="Room to join" [(ngModel)]="currentTopper.room" class="c-field">
<br />
<span>Current online toppers: {{ onlineCount }}</span>
</div>
<div class="o-grid__cell o-grid__cell--width-40">
<button (click)="addUser()" class="c-button c-button--info">Start topping <i class="fa fa-play" aria-hidden="true"></i></button>
</div>
</div>
</form>
</div>
<router-outlet></router-outlet>`,
})
export class RegisterComponent implements OnInit {
currentTopper: Datastructure.ITopper;
usernameInvalid: boolean = false;
onlineCount: number = 0;
constructor(private socketService: SocketService, private router: Router) {
this.router = router;
this.socketService = socketService;
this.currentTopper = this.socketService.getCurrentTopper();
}
addUser() {
this.socketService
.getSocketConnection()
.emit('addUser', this.currentTopper);
}
ngOnInit() {
this.socketService
.getSocketConnection()
.on('update_toppers', data => {
if (typeof data === 'number') {
this.onlineCount = data;
} else if (data && data.toppers) {
this.onlineCount = data.toppers.length;
}
});
this.socketService.getSocketConnection().on('update_me', data => {
this.currentTopper.id = data.id;
this.router.navigate(['/topper'], {});
});
this.socketService.getSocketConnection().on('register_failed', data => {
this.currentTopper.id = data.id;
this.usernameInvalid = true;
});
}
}