1
|
/**
|
2
|
* Created by stefanos on 28/4/2017.
|
3
|
*/
|
4
|
import { Component, ViewChild, Input, Output, EventEmitter, OnInit } from '@angular/core';
|
5
|
import { ModalDirective } from 'ngx-bootstrap/modal';
|
6
|
import { FormGroup } from "@angular/forms";
|
7
|
import { FAQService } from "../services/faq.service";
|
8
|
import { Topic } from "../domain/topic";
|
9
|
import { Question } from "../domain/question";
|
10
|
import { HelpContentService } from "../services/help-content.service";
|
11
|
import { Page } from "../domain/page";
|
12
|
import { Entity } from "../domain/entity";
|
13
|
import { Community } from "../domain/community";
|
14
|
|
15
|
@Component({
|
16
|
selector: 'modal-form',
|
17
|
templateUrl: './modal-form.component.html'
|
18
|
})
|
19
|
export class ModalFormComponent {
|
20
|
|
21
|
constructor(private _faqService : FAQService, private _helpService: HelpContentService){
|
22
|
}
|
23
|
|
24
|
@ViewChild('autoShownModal')
|
25
|
public autoShownModal:ModalDirective;
|
26
|
|
27
|
@Input()
|
28
|
public isModalShown:boolean = false;
|
29
|
|
30
|
@Input()
|
31
|
public saveText : string;
|
32
|
|
33
|
@Input()
|
34
|
public titleText : string;
|
35
|
|
36
|
@Input()
|
37
|
public formGroup : FormGroup;
|
38
|
|
39
|
@Input()
|
40
|
public type : string = 'topic';
|
41
|
|
42
|
@Input()
|
43
|
public selectedCommunityId: string;
|
44
|
|
45
|
public errorMessage : string = null;
|
46
|
|
47
|
@Output() emmitObject: EventEmitter<any> = new EventEmitter();
|
48
|
|
49
|
@Output() emmitError: EventEmitter<any> = new EventEmitter();
|
50
|
|
51
|
public showModal():void {
|
52
|
this.isModalShown = true;
|
53
|
}
|
54
|
|
55
|
public hideModal():void {
|
56
|
this.autoShownModal.hide();
|
57
|
}
|
58
|
|
59
|
public onHidden():void {
|
60
|
this.isModalShown = false;
|
61
|
this.emmitError.emit(null);
|
62
|
this.errorMessage = null;
|
63
|
}
|
64
|
|
65
|
public saveCustom(obj : any) {
|
66
|
if(!this.formGroup.valid) {
|
67
|
this.errorMessage = "Please fill in all required fields marked with *"
|
68
|
} else {
|
69
|
|
70
|
if (this.type == 'topic') {
|
71
|
this._faqService.saveTopic(<Topic> obj).subscribe(
|
72
|
data => this.emmitObject.emit(data),
|
73
|
error => this.emmitError.emit(error)
|
74
|
);
|
75
|
} else if (this.type == 'question') {
|
76
|
this._faqService.saveQuestion(<Question> obj).subscribe(
|
77
|
data => this.emmitObject.emit(data),
|
78
|
error => this.emmitError.emit(error)
|
79
|
);
|
80
|
} else if (this.type == 'community') {
|
81
|
if(this.saveText == 'Update') {
|
82
|
this._helpService.updateCommunity(<Community> obj).subscribe(
|
83
|
data => this.emmitObject.emit(data),
|
84
|
error => this.emmitError.emit(error)
|
85
|
);
|
86
|
console.info(<Community> obj);
|
87
|
|
88
|
} else if(this.saveText == 'Save') {
|
89
|
this._helpService.saveCommunity(<Community> obj).subscribe(
|
90
|
data => this.emmitObject.emit(data),
|
91
|
error => this.emmitError.emit(error)
|
92
|
);
|
93
|
console.info(<Community> obj);
|
94
|
}
|
95
|
} else if (this.type == 'page') {
|
96
|
if(this.saveText == 'Update') {
|
97
|
this._helpService.updatePage(<Page> obj).subscribe(
|
98
|
data => this.emmitObject.emit(data),
|
99
|
error => this.emmitError.emit(error)
|
100
|
);
|
101
|
} else if(this.saveText == 'Save') {
|
102
|
this._helpService.savePage(<Page> obj).subscribe(
|
103
|
data => this.emmitObject.emit(data),
|
104
|
error => this.emmitError.emit(error)
|
105
|
);
|
106
|
}
|
107
|
} else if (this.type == 'entity') {
|
108
|
if(this.saveText == 'Update') {
|
109
|
this._helpService.updateEntity(<Entity> obj).subscribe(
|
110
|
data => this.emmitObject.emit(data),
|
111
|
error => this.emmitError.emit(error)
|
112
|
);
|
113
|
} else if(this.saveText == 'Save') {
|
114
|
this._helpService.saveEntity(<Entity> obj).subscribe(
|
115
|
data => this.emmitObject.emit(data),
|
116
|
error => this.emmitError.emit(error)
|
117
|
);
|
118
|
}
|
119
|
}
|
120
|
this.hideModal();
|
121
|
}
|
122
|
}
|
123
|
}
|