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 { ActivatedRoute } from "@angular/router";
|
7
|
import { FormGroup } from "@angular/forms";
|
8
|
import { FAQService } from "../services/faq.service";
|
9
|
import { Topic } from "../domain/topic";
|
10
|
import { Question } from "../domain/question";
|
11
|
import { HelpContentService } from "../services/help-content.service";
|
12
|
import { Page } from "../domain/page";
|
13
|
import { Entity } from "../domain/entity";
|
14
|
import { Community } from "../domain/community";
|
15
|
import { DivId } from "../domain/divId";
|
16
|
import { EnvProperties } from '../openaireLibrary/utils/properties/env-properties';
|
17
|
|
18
|
@Component({
|
19
|
selector: 'modal-form',
|
20
|
templateUrl: './modal-form.component.html'
|
21
|
})
|
22
|
export class ModalFormComponent {
|
23
|
|
24
|
constructor(private route: ActivatedRoute, private _faqService : FAQService, private _helpService: HelpContentService){
|
25
|
}
|
26
|
|
27
|
@ViewChild('autoShownModal')
|
28
|
public autoShownModal:ModalDirective;
|
29
|
|
30
|
@Input()
|
31
|
public isModalShown:boolean = false;
|
32
|
|
33
|
@Input()
|
34
|
public saveText : string;
|
35
|
|
36
|
@Input()
|
37
|
public titleText : string;
|
38
|
|
39
|
@Input()
|
40
|
public formGroup : FormGroup;
|
41
|
|
42
|
@Input()
|
43
|
public type : string = 'topic';
|
44
|
|
45
|
public errorMessage : string = null;
|
46
|
|
47
|
@Output() emmitObject: EventEmitter<any> = new EventEmitter();
|
48
|
|
49
|
@Output() emmitError: EventEmitter<any> = new EventEmitter();
|
50
|
public properties:EnvProperties = null;
|
51
|
|
52
|
ngOnInit() {
|
53
|
|
54
|
this.route.data
|
55
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
56
|
this.properties = data.envSpecific;
|
57
|
});
|
58
|
}
|
59
|
|
60
|
public showModal():void {
|
61
|
this.isModalShown = true;
|
62
|
}
|
63
|
|
64
|
public hideModal():void {
|
65
|
this.autoShownModal.hide();
|
66
|
}
|
67
|
|
68
|
public onHidden():void {
|
69
|
this.isModalShown = false;
|
70
|
this.emmitError.emit(null);
|
71
|
this.errorMessage = null;
|
72
|
}
|
73
|
|
74
|
public saveCustom(obj : any) {
|
75
|
if (this.type == 'none') {
|
76
|
this.emmitObject.emit(true);
|
77
|
this.hideModal();
|
78
|
} else {
|
79
|
if(!this.formGroup.valid) {
|
80
|
this.errorMessage = "Please fill in all required fields marked with *"
|
81
|
} else {
|
82
|
|
83
|
if (this.type == 'topic') {
|
84
|
this._faqService.saveTopic(<Topic> obj).subscribe(
|
85
|
data => this.emmitObject.emit(data),
|
86
|
error => this.emmitError.emit(error)
|
87
|
);
|
88
|
} else if (this.type == 'question') {
|
89
|
this._faqService.saveQuestion(<Question> obj).subscribe(
|
90
|
data => this.emmitObject.emit(data),
|
91
|
error => this.emmitError.emit(error)
|
92
|
);
|
93
|
} else if (this.type == 'community') {
|
94
|
if(this.saveText == 'Update') {
|
95
|
this._helpService.updateCommunity(<Community> obj, this.properties.adminToolsAPIURL).subscribe(
|
96
|
data => this.emmitObject.emit(data),
|
97
|
error => this.emmitError.emit(error)
|
98
|
);
|
99
|
console.info(<Community> obj);
|
100
|
|
101
|
} else if(this.saveText == 'Save') {
|
102
|
this._helpService.saveCommunity(<Community> obj, this.properties.adminToolsAPIURL).subscribe(
|
103
|
data => this.emmitObject.emit(data),
|
104
|
error => this.emmitError.emit(error)
|
105
|
);
|
106
|
console.info(<Community> obj);
|
107
|
}
|
108
|
} else if (this.type == 'page') {
|
109
|
if(this.saveText == 'Update') {
|
110
|
this._helpService.updatePage(<Page> obj, this.properties.adminToolsAPIURL).subscribe(
|
111
|
data => this.emmitObject.emit(data),
|
112
|
error => this.emmitError.emit(error)
|
113
|
);
|
114
|
} else if(this.saveText == 'Save') {
|
115
|
this._helpService.savePage(<Page> obj, this.properties.adminToolsAPIURL).subscribe(
|
116
|
data => this.emmitObject.emit(data),
|
117
|
error => this.emmitError.emit(error)
|
118
|
);
|
119
|
}
|
120
|
} else if (this.type == 'entity') {
|
121
|
if(this.saveText == 'Update') {
|
122
|
this._helpService.updateEntity(<Entity> obj, this.properties.adminToolsAPIURL).subscribe(
|
123
|
data => this.emmitObject.emit(data),
|
124
|
error => this.emmitError.emit(error)
|
125
|
);
|
126
|
} else if(this.saveText == 'Save') {
|
127
|
this._helpService.saveEntity(<Entity> obj, this.properties.adminToolsAPIURL).subscribe(
|
128
|
data => this.emmitObject.emit(data),
|
129
|
error => this.emmitError.emit(error)
|
130
|
);
|
131
|
}
|
132
|
} else if (this.type == 'divId') {
|
133
|
if(this.saveText == 'Update') {
|
134
|
this._helpService.updateDivId(<DivId> obj, this.properties.adminToolsAPIURL).subscribe(
|
135
|
data => this.emmitObject.emit(data),
|
136
|
error => this.emmitError.emit(error)
|
137
|
);
|
138
|
} else if(this.saveText == 'Save') {
|
139
|
this._helpService.saveDivId(<DivId> obj, this.properties.adminToolsAPIURL).subscribe(
|
140
|
data => this.emmitObject.emit(data),
|
141
|
error => this.emmitError.emit(error)
|
142
|
);
|
143
|
}
|
144
|
}
|
145
|
this.hideModal();
|
146
|
}
|
147
|
}
|
148
|
}
|
149
|
}
|