-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit-server.component.ts
More file actions
60 lines (53 loc) · 1.96 KB
/
edit-server.component.ts
File metadata and controls
60 lines (53 loc) · 1.96 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
import {Component, OnInit} from '@angular/core';
import {ServersService} from '../servers.service';
import {ActivatedRoute, Params, Router} from '@angular/router';
import {CanDeactivateComponent} from '../../can-deactivate-component';
import {Observable} from 'rxjs';
@Component({
selector: 'app-edit-server',
templateUrl: './edit-server.component.html',
styleUrls: ['./edit-server.component.css']
})
export class EditServerComponent implements OnInit, CanDeactivateComponent {
server: { id: number, name: string, status: string };
serverName = '';
serverStatus = '';
allowEdit = false;
changesSaved = false;
constructor(private serversService: ServersService,
private route: ActivatedRoute,
private router: Router) {
}
ngOnInit() {
this.allowEdit = +this.route.snapshot.queryParams['allowEdit'] === 1 ? true : false;
this.route.queryParams.subscribe((params: Params) => {
this.allowEdit = +params['allowEdit'] === 1 ? true : false;
});
console.log('Allow Edit ' + this.allowEdit);
let loadServerId = 1;
if (this.route.snapshot.params['id']) {
loadServerId = +this.route.snapshot.params['id'];
}
this.route.params.subscribe((params: Params) => {
loadServerId = +params['id'];
});
this.server = this.serversService.getServer(loadServerId);
this.serverName = this.server.name;
this.serverStatus = this.server.status;
}
onUpdateServer() {
this.serversService.updateServer(this.server.id, {name: this.serverName, status: this.serverStatus});
this.changesSaved = true;
this.router.navigate(['../'], {relativeTo: this.route});
}
canDeactivate(): (Observable<boolean> | Promise<boolean> | boolean) {
if (!this.allowEdit) {
return true;
}
if ((this.serverName !== this.server.name || this.serverStatus !== this.server.status) && !this.changesSaved) {
return confirm('Do you want to discard changes');
} else {
return true;
}
}
}