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
import {HelperService} from "../openaireLibrary/utils/helper/helper.service";
13

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

    
19
export class ContactComponent implements OnInit {
20

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

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

    
43
    constructor(private route: ActivatedRoute,
44
                private _router: Router,
45
                private _emailService: EmailService,
46
                private _title: Title,
47
                private _piwikService: PiwikService,
48
                private helper: HelperService) {
49
    }
50

    
51

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

    
67
    private getPageContents() {
68
        this.helper.getPageHelpContents(this._router.url, this.properties, 'connect').subscribe(contents => {
69
            this.pageContents = contents;
70
        })
71
    }
72

    
73
    private getDivContents() {
74
        this.helper.getDivHelpContents(this._router.url, this.properties, 'connect').subscribe(contents => {
75
            this.divContents = contents;
76
        })
77
    }
78

    
79
    public send() {
80
        HelperFunctions.scroll();
81
        if(!this.name.invalid && !this.surname.invalid && !this.sender.invalid &&
82
            this.contactForm.email.match('^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$') &&
83
            !this.affiliation.invalid && !this.message.invalid && this.contactForm.recaptcha != '') {
84
            this.sendMail(this.properties.admins);
85
        }
86
        else {
87
            this.errorMessage = 'Please fill in all the required fields!'
88
            this.isSubmitted = true;
89
        }
90
    }
91

    
92
    public reset() {
93
        this.contactForm = new ContactForm();
94
        this.isSubmitted = false;
95
        this.errorMessage = '';
96
        this.contactForm.recaptcha = '';
97
    }
98

    
99
     private sendMail(admins: any) {
100
         this.showLoading = true;
101
         this._emailService.contact(this.properties.adminToolsAPIURL + '/contact',
102
                                      Composer.composeEmailForNewCommunity(this.contactForm, admins), this.contactForm.recaptcha).subscribe(
103
               res => {
104
                   if(res) {
105
                       this.reset();
106
                       this.modalOpen();
107
                       this.showLoading = false;
108
                   }
109
               },
110
               error => {
111
                   this.handleError('Email sent failed! Please try again.', error);
112
                   this.showLoading = false;
113
                   this.contactForm.recaptcha = '';
114
               }
115
         );
116
     }
117

    
118
    public modalOpen() {
119
        this.modal.okButton = true;
120
        this.modal.alertTitle = 'Your request has been successfully submitted';
121
        this.modal.alertMessage = false;
122
        this.modal.cancelButton = false;
123
        this.modal.okButtonLeft = false;
124
        this.modal.okButtonText = 'OK';
125
        this.modal.open();
126
    }
127

    
128
    public handleRecaptcha(captchaResponse: string) {
129
        this.contactForm.recaptcha = captchaResponse;
130
    }
131

    
132

    
133
    handleError(message: string, error) {
134
        this.errorMessage = message;
135
        console.log('Server responded: ' + error);
136

    
137
        this.showLoading = false;
138
    }
139

    
140
    public goToHome(data: any) {
141
        this._router.navigate(['/']);
142
    }
143

    
144
}
(3-3/4)