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
import {Subscriber} from "rxjs";
15
import {properties} from "../../environments/environment";
16

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

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

    
33
    public contactForm: FormGroup;
34
    @ViewChild('AlertModal') modal;
35
    @ViewChild('recaptcha') recaptcha;
36
    private subscriptions = [];
37
    ngOnDestroy() {
38
        this.subscriptions.forEach(subscription => {
39
            if (subscription instanceof Subscriber) {
40
                subscription.unsubscribe();
41
            }
42
        });
43
    }
44

    
45
    constructor(private route: ActivatedRoute,
46
                private _router: Router,
47
                private _emailService: EmailService,
48
                private _meta: Meta,
49
                private _title: Title,
50
                private seoService: SEOService,
51
                private _piwikService: PiwikService,
52
                private fb: FormBuilder,
53
                private helper: HelperService) {
54
    }
55

    
56
    ngOnInit() {
57
        this._title.setTitle('OpenAIRE-Connect | Contact Us');
58

    
59
        this.properties = properties;
60
        this.email = {body: '', subject: '', recipients: []};
61

    
62
        if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
63
            this.subscriptions.push( this._piwikService.trackView(this.properties, this.pageTitle, this.properties.piwikSiteId).subscribe());
64
        }
65
        this.url = this.properties.domain + this._router.url;
66
        this.seoService.createLinkForCanonicalURL(this.url);
67
        this.updateUrl(this.url);
68
        this.updateTitle(this.pageTitle);
69
        this.updateDescription("Contact us to learn more about OpenAIRE Connect Research Gateways");
70
        this.reset();
71
        //this.getDivContents();
72
        this.getPageContents();
73
        this.showLoading = false;
74

    
75
    }
76

    
77
    private getPageContents() {
78
        this.subscriptions.push(this.helper.getPageHelpContents(this.properties, 'connect', this._router.url).subscribe(contents => {
79
            this.pageContents = contents;
80
        }));
81
    }
82

    
83
    private getDivContents() {
84
        this.subscriptions.push(this.helper.getDivHelpContents(this.properties, 'connect', this._router.url).subscribe(contents => {
85
            this.divContents = contents;
86
        }));
87
    }
88

    
89
    public send(event) {
90
        HelperFunctions.scroll();
91
        if(event.valid === true) {
92
            this.sendMail(this.properties.admins);
93
        } else {
94
            this.errorMessage = 'Please fill in all the required fields!';
95
        }
96
    }
97

    
98
    public reset() {
99
        this.contactForm = this.fb.group( {
100
            name: this.fb.control('', Validators.required),
101
            surname: this.fb.control('', Validators.required),
102
            email: this.fb.control('', [Validators.required, Validators.email]),
103
            affiliation: this.fb.control('', Validators.required),
104
            community: this.fb.control('', Validators.required),
105
            message: this.fb.control('', Validators.required),
106
            recaptcha: this.fb.control('', Validators.required),
107
        });
108
        this.errorMessage = '';
109
    }
110

    
111
    private sendMail(admins: any) {
112
        this.showLoading = true;
113
        this.subscriptions.push(this._emailService.contact(this.properties,
114
            Composer.composeEmailForNewCommunity(this.contactForm.value, admins),
115
          this.contactForm.value.recaptcha).subscribe(
116
            res => {
117
                this.showLoading = false;
118
                if (res) {
119
                    this.reset();
120
                    this.modalOpen();
121
                } else {
122
                    this.errorMessage = 'Email sent failed! Please try again.';
123
                    this.contactForm.get('recaptcha').setValue('');
124
                }
125
            },
126
            error => {
127
                this.handleError('Email sent failed! Please try again.', error);
128
                this.showLoading = false;
129
                this.contactForm.get('recaptcha').setValue('');
130
            }
131
        ));
132
    }
133

    
134
    public modalOpen() {
135
        this.modal.okButton = true;
136
        this.modal.alertTitle = 'Your request has been successfully submitted';
137
        this.modal.alertMessage = false;
138
        this.modal.cancelButton = false;
139
        this.modal.okButtonLeft = false;
140
        this.modal.okButtonText = 'OK';
141
        this.modal.open();
142
    }
143

    
144
    handleError(message: string, error) {
145
        this.errorMessage = message;
146
        console.log('Server responded: ' + error);
147
        this.showLoading = false;
148
    }
149

    
150
    public goToHome() {
151
        this._router.navigate(['/']);
152
    }
153

    
154
    private updateDescription(description: string) {
155
        this._meta.updateTag({content: description}, "name='description'");
156
        this._meta.updateTag({content: description}, "property='og:description'");
157
    }
158

    
159
    private updateTitle(title: string) {
160
        var _title = ((title.length > 50) ? title.substring(0, 50) : title);
161
        this._title.setTitle(_title);
162
        this._meta.updateTag({content: _title}, "property='og:title'");
163
    }
164

    
165
    private updateUrl(url: string) {
166
        this._meta.updateTag({content: url}, "property='og:url'");
167
    }
168
}
(3-3/4)