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

    
37
    public contactForm: FormGroup;
38
    @ViewChild('AlertModal') modal;
39
    @ViewChild('recaptcha') recaptcha;
40

    
41
    constructor(private route: ActivatedRoute,
42
                private _router: Router,
43
                private _emailService: EmailService,
44
                private _meta: Meta,
45
                private _title: Title,
46
                private seoService: SEOService,
47
                private _piwikService: PiwikService,
48
                private fb: FormBuilder,
49
                private helper: HelperService) {
50
    }
51
    subscriptions = [];
52
    ngOnDestroy() {
53
        this.subscriptions.forEach(subscription => {
54
            if (subscription instanceof Subscriber) {
55
                subscription.unsubscribe();
56
            }
57
        });
58
    }
59
    ngOnInit() {
60
        this.properties = properties;
61
        this.email = {body: '', subject: '', recipients: []};
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.properties.baseLink + this._router.url;
66
        this.seoService.createLinkForCanonicalURL(this.url);
67
        this.updateUrl(this.url);
68
        this.updateTitle(this.pageTitle);
69
        this.updateDescription(this.description);
70
        this.reset();
71
        //this.getDivContents();
72
        // this.getPageContents();
73
        HelperFunctions.scroll();
74
        this.showLoading = false;
75

    
76
    }
77

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

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

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

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

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

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

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

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

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

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