Project

General

Profile

1
import {Component, OnInit, Input, ElementRef, ViewChild} from '@angular/core';
2
import {FormGroup} from '@angular/forms';
3
import {ActivatedRoute, Router} from '@angular/router';
4
import {EmailService} from "../openaireLibrary/utils/email/email.service";
5
import {Email} from "../openaireLibrary/utils/email/email";
6
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
7
import {ContactForm} from "../openaireLibrary/utils/email/contact-form";
8
import {Composer} from "../openaireLibrary/utils/email/composer";
9
import {Title} from "@angular/platform-browser";
10
import {PiwikService} from "../openaireLibrary/utils/piwik/piwik.service";
11
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
12

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

    
18
export class ContactComponent implements OnInit {
19

    
20
    @Input('group')
21
    myForm: FormGroup;
22
    public piwiksub: any;
23
    public showLoading = true;
24
    public errorMessage = '';
25
    public isSubmitted = false;
26
    public email: Email;
27
    public note = '';
28
    public properties: EnvProperties = null;
29

    
30
    public contactForm: ContactForm = new ContactForm();
31
    @ViewChild('AlertModal') modal;
32
    @ViewChild('name') name;
33
    @ViewChild('surname') surname;
34
    @ViewChild('sender') sender;
35
    @ViewChild('affiliation') affiliation;
36
    @ViewChild('community') community;
37
    @ViewChild('message') message;
38
    @ViewChild('recaptcha') recaptcha;
39

    
40
    constructor(private route: ActivatedRoute,
41
                private _router: Router,
42
                private _emailService: EmailService,
43
                private _title: Title,
44
                private _piwikService: PiwikService) {
45
    }
46

    
47

    
48
    ngOnInit() {
49
        this._title.setTitle('OpenAIRE-Connect | Contact Us');
50
        this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
51
            this.properties = data.envSpecific;
52
            this.email = {body: '', subject: '', recipients: []};
53
            if(this.properties.enablePiwikTrack && (typeof document !== 'undefined')){
54
                this.piwiksub = this._piwikService.trackView(this.properties, "OpenAIRE Connect|Contact Us", this.properties.piwikSiteId).subscribe();
55
            }
56
            HelperFunctions.scroll();
57
            this.showLoading = false;
58
        });
59
    }
60

    
61
    public send() {
62
        HelperFunctions.scroll();
63
        if(!this.name.invalid && !this.surname.invalid && !this.sender.invalid &&
64
            this.contactForm.email.match('^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$') &&
65
            !this.affiliation.invalid && !this.message.invalid && this.contactForm.recaptcha != '') {
66
            this.sendMail(this.properties.admins);
67
        }
68
        else {
69
            this.errorMessage = 'Please fill in all the required fields!'
70
            this.isSubmitted = true;
71
        }
72
    }
73

    
74
    public reset() {
75
        this.contactForm = new ContactForm();
76
        this.isSubmitted = false;
77
        this.errorMessage = '';
78
        this.contactForm.recaptcha = '';
79
    }
80

    
81
     private sendMail(admins: any) {
82
         this.showLoading = true;
83
         this._emailService.contact(this.properties.adminToolsAPIURL + '/contact',
84
                                      Composer.composeEmailForNewCommunity(this.contactForm, admins), this.contactForm.recaptcha).subscribe(
85
               res => {
86
                   if(res) {
87
                       this.reset();
88
                       this.modalOpen();
89
                       this.showLoading = false;
90
                   }
91
                   else {
92
                       this.handleError('Token has expired. Please complete the reCaptcha challenge again! ', null)
93
                       this.showLoading = false;
94
                       this.contactForm.recaptcha = '';
95
                   }
96
               },
97
               error => {
98
                   this.handleError('Email sent failed! Please try again.', error);
99
                   this.showLoading = false;
100
                   this.contactForm.recaptcha = '';
101
               }
102
         );
103
     }
104

    
105
    public modalOpen() {
106
        this.modal.okButton = true;
107
        this.modal.alertTitle = 'Your request has been successfully submitted';
108
        this.modal.alertMessage = false;
109
        this.modal.cancelButton = false;
110
        this.modal.okButtonLeft = false;
111
        this.modal.okButtonText = 'OK';
112
        this.modal.open();
113
    }
114

    
115
    public handleRecaptcha(captchaResponse: string) {
116
        this.contactForm.recaptcha = captchaResponse;
117
    }
118

    
119

    
120
    handleError(message: string, error) {
121
        this.errorMessage = message;
122
        console.log('Server responded: ' + error);
123

    
124
        this.showLoading = false;
125
    }
126

    
127
    public goToHome(data: any) {
128
        this._router.navigate(['/']);
129
    }
130

    
131
}
(3-3/4)