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
import {Breadcrumb} from "../openaireLibrary/utils/breadcrumbs/breadcrumbs.component";
14

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

    
20
export class ContactComponent implements OnInit {
21
    public url: string = null;
22
    public pageTitle: string = "OpenAIRE - Connect | Contact Us";
23
    public piwiksub: any;
24
    public showLoading = true;
25
    public errorMessage = '';
26
    public email: Email;
27
    public properties: EnvProperties = null;
28
    public pageContents = null;
29
    public divContents = null;
30
    public breadcrumbs: Breadcrumb[] = [{name: 'home', route: '/'}, {name: 'contact us'}];
31

    
32
    public contactForm: FormGroup;
33
    @ViewChild('AlertModal') modal;
34
    @ViewChild('recaptcha') recaptcha;
35

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

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

    
53
            if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
54
                this.piwiksub = this._piwikService.trackView(this.properties, this.pageTitle, this.properties.piwikSiteId).subscribe();
55
            }
56
            this.url = this.properties.domain + this._router.url;
57
            this.seoService.createLinkForCanonicalURL(this.url);
58
            this.updateUrl(this.url);
59
            this.updateTitle(this.pageTitle);
60
            this.updateDescription("Contact us to learn more about OpenAIRE Connect Research Gateways");
61
            this.reset();
62
            //this.getDivContents();
63
            this.getPageContents();
64
            HelperFunctions.scroll();
65
            this.showLoading = false;
66
        });
67
    }
68

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

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

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

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

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

    
127
    public modalOpen() {
128
        this.modal.okButton = true;
129
        this.modal.alertTitle = 'Your request has been successfully submitted';
130
        this.modal.alertMessage = false;
131
        this.modal.cancelButton = false;
132
        this.modal.okButtonLeft = false;
133
        this.modal.okButtonText = 'OK';
134
        this.modal.open();
135
    }
136

    
137
    handleError(message: string, error) {
138
        this.errorMessage = message;
139
        console.log('Server responded: ' + error);
140
        this.showLoading = false;
141
    }
142

    
143
    public goToHome() {
144
        this._router.navigate(['/']);
145
    }
146

    
147
    private updateDescription(description: string) {
148
        this._meta.updateTag({content: description}, "name='description'");
149
        this._meta.updateTag({content: description}, "property='og:description'");
150
    }
151

    
152
    private updateTitle(title: string) {
153
        var _title = ((title.length > 50) ? title.substring(0, 50) : title);
154
        this._title.setTitle(_title);
155
        this._meta.updateTag({content: _title}, "property='og:title'");
156
    }
157

    
158
    private updateUrl(url: string) {
159
        this._meta.updateTag({content: url}, "property='og:url'");
160
    }
161
}
(3-3/4)