-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathapp-module-activate-button.component.ts
More file actions
111 lines (103 loc) · 4.56 KB
/
Copy pathapp-module-activate-button.component.ts
File metadata and controls
111 lines (103 loc) · 4.56 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {ChangeDetectorRef, Component, EventEmitter, Input, OnDestroy, OnInit, Output} from '@angular/core';
import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
import {Subject} from 'rxjs';
import {filter, takeUntil} from 'rxjs/operators';
import {UtmToastService} from '../../../../shared/alert/utm-toast.service';
import {NavBehavior} from '../../../../shared/behaviors/nav.behavior';
import {ModuleChangeStatusBehavior} from '../../behavior/module-change-status.behavior';
import {ModuleRefreshBehavior} from '../../behavior/module-refresh.behavior';
import {UtmModulesEnum} from '../../enum/utm-module.enum';
import {UtmModulesService} from '../../services/utm-modules.service';
import {UtmModuleType} from '../../type/utm-module.type';
import {AppModuleChecksComponent} from '../app-module-checks/app-module-checks.component';
import {AppModuleDeactivateComponent} from '../app-module-deactivate/app-module-deactivate.component';
@Component({
selector: 'app-app-module-activate-button',
templateUrl: './app-module-activate-button.component.html',
styleUrls: ['./app-module-activate-button.component.css']
})
export class AppModuleActivateButtonComponent implements OnInit, OnDestroy {
@Input() performPreAction = false;
@Input() module: UtmModulesEnum;
@Input() type: string;
@Input() disabled = false;
@Input() serverId: number;
@Output() disableModuleClicked = new EventEmitter<void>();
loadingDetail = true;
moduleDetail: UtmModuleType;
changingStatus: any;
activatable: boolean;
destroy$: Subject<void> = new Subject<void>();
constructor(private utmModulesService: UtmModulesService,
public modalService: NgbModal,
private toastService: UtmToastService,
private moduleRefreshBehavior: ModuleRefreshBehavior,
private navBehavior: NavBehavior,
private moduleChangeStatusBehavior: ModuleChangeStatusBehavior,
private cd: ChangeDetectorRef) {
}
ngOnInit() {
this.moduleChangeStatusBehavior.moduleStatus$
.pipe(filter(value => !!value),
takeUntil(this.destroy$))
.subscribe( value => {
this.performPreAction = value.hasPreAction;
if (value.status != null && this.moduleDetail && (this.moduleDetail.moduleActive && !value.status ||
this.moduleDetail && !this.moduleDetail.moduleActive && value.status)) {
this.changeModuleStatus(value.status);
}
});
this.getModuleDetail(this.module);
}
getModuleDetail(module: UtmModulesEnum) {
this.utmModulesService.getModulesDetails({nameShort: module, serverId: this.serverId})
.subscribe(response => {
this.activatable = response.body.activatable;
this.moduleDetail = response.body;
this.loadingDetail = false;
this.cd.detectChanges();
});
}
enableModule() {
if (!this.moduleDetail.moduleActive) {
const modalChecks = this.modalService.open(AppModuleChecksComponent,
{centered: true});
modalChecks.componentInstance.module = this.moduleDetail;
modalChecks.componentInstance.serverId = this.serverId;
modalChecks.componentInstance.checkResult.subscribe(statusCheck => {
if (statusCheck) {
this.changeModuleStatus(statusCheck, true);
}
});
} else {
const modalDeactivate = this.modalService.open(AppModuleDeactivateComponent, {centered: true});
modalDeactivate.componentInstance.module = this.moduleDetail;
modalDeactivate.componentInstance.serverId = this.serverId;
modalDeactivate.componentInstance.disable.subscribe(statusCheck => {
this.changeModuleStatus(false, true);
});
}
}
changeModuleStatus(status: boolean, fromOnclick = false) {
if (!this.performPreAction || (this.performPreAction && status)) {
this.changingStatus = true;
this.utmModulesService.setModuleActivate(status, this.moduleDetail.moduleName, this.serverId)
.subscribe(response => {
this.moduleDetail.moduleActive = response.moduleActive;
this.changingStatus = false;
this.navBehavior.$nav.next(true);
this.moduleRefreshBehavior.$moduleChange.next(true);
this.toastService.showSuccessBottom('Module ' + this.moduleDetail.moduleName +
' has been ' + (this.moduleDetail.moduleActive ? 'enabled' : 'disabled') + ' successfully');
if (!status) {
this.disableModuleClicked.emit();
}
});
}
}
ngOnDestroy() {
this.moduleChangeStatusBehavior.setStatus(null, null);
this.destroy$.next();
this.destroy$.complete();
}
}