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

    
15
@Component({
16
    selector: 'contact',
17
    templateUrl: './contact.component.html',
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
    public organizationTypes: string[] = [
30
        'Funding agency', 'University / Research Center',
31
        'Research Infrastructure', 'Government',
32
        'Non-profit', 'Industry', 'Other'
33
    ];
34

    
35
    public contactForm: FormGroup;
36
    @ViewChild('AlertModal') modal;
37
    @ViewChild('recaptcha') recaptcha;
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._title.setTitle('OpenAIRE-Monitor | Contact Us');
59
        this.subscriptions.push(this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
60
            this.properties = data.envSpecific;
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("OpenAIRE - Monitor, Community Gateway, research community - Contact Us");
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)