Project

General

Profile

1
import {Component, OnInit, ViewChild} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {EmailService} from "../openaireLibrary/utils/email/email.service";
4
import {Email} from "../openaireLibrary/utils/email/email";
5
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
6
import {Composer} from "../openaireLibrary/utils/email/composer";
7
import {Meta, Title} from "@angular/platform-browser";
8
import {PiwikService} from "../openaireLibrary/utils/piwik/piwik.service";
9
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
10
import {HelperService} from "../openaireLibrary/utils/helper/helper.service";
11
import {SEOService} from "../openaireLibrary/sharedComponents/SEO/SEO.service";
12
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
13

    
14
@Component({
15
    selector: 'contact',
16
    templateUrl: './contact.component.html',
17
})
18

    
19
export class ContactComponent implements OnInit {
20
    public url: string = null;
21
    public pageTitle: string = "OpenAIRE - Monitor | Contact Us";
22
    public piwiksub: any;
23
    public showLoading = true;
24
    public errorMessage = '';
25
    public email: Email;
26
    public properties: EnvProperties = null;
27
    public pageContents = null;
28
    public divContents = null;
29

    
30
    public contactForm: FormGroup;
31
    @ViewChild('AlertModal') modal;
32
    @ViewChild('recaptcha') recaptcha;
33

    
34
    constructor(private route: ActivatedRoute,
35
                private _router: Router,
36
                private _emailService: EmailService,
37
                private _meta: Meta,
38
                private _title: Title,
39
                private seoService: SEOService,
40
                private _piwikService: PiwikService,
41
                private fb: FormBuilder,
42
                private helper: HelperService) {
43
    }
44

    
45
    ngOnInit() {
46
        this._title.setTitle('OpenAIRE-Monitor | Contact Us');
47
        this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
48
            this.properties = data.envSpecific;
49
            this.email = {body: '', subject: '', recipients: []};
50

    
51
            if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
52
                this.piwiksub = this._piwikService.trackView(this.properties, this.pageTitle, this.properties.piwikSiteId).subscribe();
53
            }
54
            this.url = this.properties.baseLink + this._router.url;
55
            this.seoService.createLinkForCanonicalURL(this.url);
56
            this.updateUrl(this.url);
57
            this.updateTitle(this.pageTitle);
58
            this.updateDescription("OpenAIRE - Monitor, Community Gateway, research community - Contact Us");
59
            this.reset();
60
            //this.getDivContents();
61
            this.getPageContents();
62
            HelperFunctions.scroll();
63
            this.showLoading = false;
64
        });
65
    }
66

    
67
    private getPageContents() {
68
        this.helper.getPageHelpContents(this.properties, 'monitor', this._router.url).subscribe(contents => {
69
            this.pageContents = contents;
70
        })
71
    }
72

    
73
    private getDivContents() {
74
        this.helper.getDivHelpContents(this.properties, 'monitor', this._router.url).subscribe(contents => {
75
            this.divContents = contents;
76
        })
77
    }
78

    
79
    public send(event) {
80
        HelperFunctions.scroll();
81
        if(event.valid === true) {
82
            this.sendMail(this.properties.admins);
83
        } else {
84
            this.errorMessage = 'Please fill in all the required fields!';
85
        }
86
    }
87

    
88
    public reset() {
89
        this.contactForm = this.fb.group( {
90
            name: this.fb.control('', Validators.required),
91
            surname: this.fb.control('', Validators.required),
92
            email: this.fb.control('', [Validators.required, Validators.email]),
93
            affiliation: this.fb.control('', Validators.required),
94
            stakeholder: this.fb.control('', Validators.required),
95
            subject: this.fb.control('', Validators.required),
96
            message: this.fb.control('', Validators.required),
97
            recaptcha: this.fb.control('', Validators.required),
98
        });
99
        this.errorMessage = '';
100
    }
101

    
102
    private sendMail(admins: any) {
103
        this.showLoading = true;
104
        this._emailService.contact(this.properties,
105
            Composer.composeEmailForNewStakeholder(this.contactForm.value, admins),
106
            this.contactForm.value.recaptcha).subscribe(
107
            res => {
108
                if (res) {
109
                    this.reset();
110
                    this.modalOpen();
111
                    this.showLoading = false;
112
                }
113
            },
114
            error => {
115
                this.handleError('Email sent failed! Please try again.', error);
116
                this.showLoading = false;
117
                this.contactForm.get('recaptcha').setValue('');
118
            }
119
        );
120
    }
121

    
122
    public modalOpen() {
123
        this.modal.okButton = true;
124
        this.modal.alertTitle = 'Your request has been successfully submitted';
125
        this.modal.alertMessage = false;
126
        this.modal.cancelButton = false;
127
        this.modal.okButtonLeft = false;
128
        this.modal.okButtonText = 'OK';
129
        this.modal.open();
130
    }
131

    
132
    handleError(message: string, error) {
133
        this.errorMessage = message;
134
        console.log('Server responded: ' + error);
135
        this.showLoading = false;
136
    }
137

    
138
    public goToHome() {
139
        this._router.navigate(['/']);
140
    }
141

    
142
    private updateDescription(description: string) {
143
        this._meta.updateTag({content: description}, "name='description'");
144
        this._meta.updateTag({content: description}, "property='og:description'");
145
    }
146

    
147
    private updateTitle(title: string) {
148
        var _title = ((title.length > 50) ? title.substring(0, 50) : title);
149
        this._title.setTitle(_title);
150
        this._meta.updateTag({content: _title}, "property='og:title'");
151
    }
152

    
153
    private updateUrl(url: string) {
154
        this._meta.updateTag({content: url}, "property='og:url'");
155
    }
156
}
(3-3/4)