1
|
/**
|
2
|
* Created by stefania on 7/14/17.
|
3
|
*/
|
4
|
import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core';
|
5
|
import { PageContentFormComponent } from "./page-help-content-form.component";
|
6
|
import { Subscription } from "rxjs/Subscription";
|
7
|
import { HelpContentService } from "../../services/help-content.service";
|
8
|
import { PageHelpContent } from "../../domain/page-help-content";
|
9
|
import { ActivatedRoute, Router } from "@angular/router";
|
10
|
|
11
|
@Component({
|
12
|
selector: 'edit-page-help-content',
|
13
|
templateUrl: 'edit-page-help-content.component.html',
|
14
|
})
|
15
|
|
16
|
export class EditPageHelpContentComponent implements OnInit, OnDestroy{
|
17
|
|
18
|
@ViewChild(PageContentFormComponent)
|
19
|
public formComponent : PageContentFormComponent;
|
20
|
|
21
|
private communityId: string;
|
22
|
|
23
|
private pageId: string;
|
24
|
|
25
|
private sub: Subscription;
|
26
|
|
27
|
private pageHelpContent: PageHelpContent;
|
28
|
|
29
|
private errorMessage : string = null;
|
30
|
|
31
|
constructor(
|
32
|
private route: ActivatedRoute,
|
33
|
private router: Router,
|
34
|
private _helpContentService: HelpContentService) {}
|
35
|
|
36
|
ngOnInit() {
|
37
|
|
38
|
this.sub = this.route.queryParams.subscribe(params => {
|
39
|
//let id = params['id'];
|
40
|
let pageContentId = params['pageContentId'];
|
41
|
this.communityId = params['communityId'];
|
42
|
this.pageId = params['pageId'];
|
43
|
this._helpContentService.getPageHelpContent(pageContentId as string).subscribe(
|
44
|
pageHelpContent => this.updateForm(pageHelpContent),
|
45
|
error => this.handleError('System error retrieving page help content', error));
|
46
|
});
|
47
|
}
|
48
|
ngOnDestroy() {
|
49
|
this.sub.unsubscribe();
|
50
|
}
|
51
|
|
52
|
handleError(message: string, error) {
|
53
|
this.errorMessage = message + ' (Server responded: ' + error + ')';
|
54
|
}
|
55
|
|
56
|
private updateForm(pageHelpContent : PageHelpContent) {
|
57
|
this.pageHelpContent = pageHelpContent;
|
58
|
this.formComponent.myForm.patchValue((pageHelpContent));
|
59
|
// console.log("patching",pageHelpContent);
|
60
|
}
|
61
|
|
62
|
private saveCustom() {
|
63
|
if(this.formComponent.myForm.valid) {
|
64
|
let pageHelpContent : PageHelpContent = this.formComponent.myForm.value;
|
65
|
this._helpContentService.savePageHelpContent(pageHelpContent).subscribe(
|
66
|
_ => {
|
67
|
if(this.pageId) {
|
68
|
this.router.navigate( ['/pageContents/'], { queryParams: { "community": this.communityId, "page": this.pageId } } );
|
69
|
} else {
|
70
|
this.router.navigate(['/pageContents']);
|
71
|
}
|
72
|
},
|
73
|
err => this.handleError('System error updating page content', err)
|
74
|
);
|
75
|
} else {
|
76
|
this.errorMessage = "Please fill all required fields";
|
77
|
}
|
78
|
|
79
|
}
|
80
|
}
|