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 {AbstractControl, FormBuilder, FormGroup, ValidatorFn, Validators} from "@angular/forms";
13
import {Subscriber} from "rxjs";
14
import {properties} from "../../environments/environment";
15

    
16
@Component({
17
    selector: 'contact',
18
    templateUrl: './contact.component.html',
19
})
20
export class ContactComponent implements OnInit {
21
    public url: string = null;
22
    public pageTitle: string = "OpenAIRE - Monitor | Contact Us";
23
    public description: string = "OpenAIRE - Monitor . Any Questions? Contact us to learn more";
24
    public piwiksub: any;
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 organizationTypes: string[] = [
32
        'Funding agency', 'University / Research Center',
33
        'Research Infrastructure', 'Government',
34
        'Non-profit', 'Industry', 'Other'
35
    ];
36
    public contactForm: FormGroup;
37
    @ViewChild('AlertModal') modal;
38

    
39
    constructor(private route: ActivatedRoute,
40
                private _router: Router,
41
                private _emailService: EmailService,
42
                private _meta: Meta,
43
                private _title: Title,
44
                private seoService: SEOService,
45
                private _piwikService: PiwikService,
46
                private fb: FormBuilder,
47
                private helper: HelperService) {
48
    }
49
    subscriptions = [];
50
    ngOnDestroy() {
51
        this.subscriptions.forEach(subscription => {
52
            if (subscription instanceof Subscriber) {
53
                subscription.unsubscribe();
54
            }
55
        });
56
    }
57
    ngOnInit() {
58
        this.properties = properties;
59
        this.email = {body: '', subject: '', recipients: []};
60
        if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
61
            this.subscriptions.push(this._piwikService.trackView(this.properties, this.pageTitle, this.properties.piwikSiteId).subscribe());
62
        }
63
        this.url = this.properties.domain + this.properties.baseLink + this._router.url;
64
        this.seoService.createLinkForCanonicalURL(this.url);
65
        this.updateUrl(this.url);
66
        this.updateTitle(this.pageTitle);
67
        this.updateDescription(this.description);
68
        this.reset();
69
        //this.getDivContents();
70
        // this.getPageContents();
71
        HelperFunctions.scroll();
72
        this.showLoading = false;
73

    
74
    }
75

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

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

    
88
    public send(event) {
89
        HelperFunctions.scroll();
90
        if(event.valid === true) {
91
            this.sendMail(this.properties.admins);
92
        } else {
93
            this.errorMessage = 'Please fill in all the required fields!';
94
        }
95
    }
96
    
97
    private validatorType(options: string[]): ValidatorFn  {
98
        return  (control: AbstractControl): { [key: string]: boolean } | null => {
99
            if (options.filter(type => type === control.value).length === 0) {
100
                return {'type': false};
101
            }
102
            return null;
103
        }
104
    }
105

    
106
    public reset() {
107
        this.contactForm = this.fb.group( {
108
            name: this.fb.control('', Validators.required),
109
            surname: this.fb.control('', Validators.required),
110
            email: this.fb.control('', [Validators.required, Validators.email]),
111
            job: this.fb.control('', Validators.required),
112
            organization: this.fb.control('', Validators.required),
113
            organizationType: this.fb.control('', [Validators.required, this.validatorType(this.organizationTypes)]),
114
            message: this.fb.control('', Validators.required),
115
            recaptcha: this.fb.control('', Validators.required),
116
        });
117
        this.errorMessage = '';
118
    }
119
    
120
    private sendMail(admins: any) {
121
        this.showLoading = true;
122
        this.subscriptions.push(this._emailService.contact(this.properties,
123
            Composer.composeEmailForMonitor(this.contactForm.value, admins),
124
          this.contactForm.value.recaptcha).subscribe(
125
            res => {
126
                this.showLoading = false;
127
                if (res) {
128
                    this.reset();
129
                    this.modalOpen();
130
                } else {
131
                    this.errorMessage = 'Email sent failed! Please try again.';
132
                    this.contactForm.get('recaptcha').setValue('');
133
                }
134
            },
135
            error => {
136
                this.handleError('Email sent failed! Please try again.', error);
137
                this.showLoading = false;
138
                this.contactForm.get('recaptcha').setValue('');
139
            }
140
        ));
141
    }
142

    
143
    public modalOpen() {
144
        this.modal.okButton = true;
145
        this.modal.alertTitle = 'Your request has been successfully submitted';
146
        this.modal.alertMessage = false;
147
        this.modal.cancelButton = false;
148
        this.modal.okButtonLeft = false;
149
        this.modal.okButtonText = 'OK';
150
        this.modal.open();
151
    }
152

    
153
    handleError(message: string, error) {
154
        this.errorMessage = message;
155
        console.log('Server responded: ' + error);
156
        this.showLoading = false;
157
    }
158

    
159
    public goToHome() {
160
        this._router.navigate(['/']);
161
    }
162

    
163
    private updateDescription(description: string) {
164
        this._meta.updateTag({content: description}, "name='description'");
165
        this._meta.updateTag({content: description}, "property='og:description'");
166
    }
167

    
168
    private updateTitle(title: string) {
169
        var _title = ((title.length > 50) ? title.substring(0, 50) : title);
170
        this._title.setTitle(_title);
171
        this._meta.updateTag({content: _title}, "property='og:title'");
172
    }
173

    
174
    private updateUrl(url: string) {
175
        this._meta.updateTag({content: url}, "property='og:url'");
176
    }
177
}
(3-3/4)