Project

General

Profile

1
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
2
import {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 {HelperFunctions} from '../openaireLibrary/utils/HelperFunctions.class';
9
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
10
import {properties} from '../../environments/environment';
11
import {Subscriber, Subscription} from 'rxjs';
12
import {PiwikService} from '../openaireLibrary/utils/piwik/piwik.service';
13
import {SEOService} from '../openaireLibrary/sharedComponents/SEO/SEO.service';
14

    
15
@Component({
16
  selector: 'contact',
17
  templateUrl: './contact.component.html',
18
  styleUrls: ['contact.component.css']
19
})
20

    
21
export class ContactComponent implements OnInit, OnDestroy {
22
  public showLoading = true;
23
  public errorMessage = '';
24
  public email: Email;
25
  public properties: EnvProperties = properties;
26
  
27
  public contactForm: FormGroup;
28
  @ViewChild('AlertModal') modal;
29
  description = 'Any questions? Contact us ';
30
  title = 'OpenAIRE - UsageCounts |  Contact Us';
31
  subs: Subscription[] = [];
32
  
33
  constructor(private router: Router,
34
              private emailService: EmailService,
35
              private _title: Title, private _piwikService: PiwikService,
36
              private _meta: Meta, private seoService: SEOService,
37
              private fb: FormBuilder) {
38
  }
39
  
40
  ngOnInit() {
41
    this._title.setTitle(this.title);
42
    this._meta.updateTag({content: this.description}, 'name=\'description\'');
43
    this._meta.updateTag({content: this.description}, 'property=\'og:description\'');
44
    this._meta.updateTag({content: this.title}, 'property=\'og:title\'');
45
    this._title.setTitle(this.title);
46
    let url = this.properties.domain + this.properties.baseLink + this.router.url;
47
    this.seoService.createLinkForCanonicalURL(url, false);
48
    this._meta.updateTag({content: url}, 'property=\'og:url\'');
49
    if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
50
      this.subs.push(this._piwikService.trackView(this.properties, this.title).subscribe());
51
    }
52
    this.email = {body: '', subject: '', recipients: []};
53
    this.reset();
54
    this.showLoading = false;
55
  }
56
  
57
  public ngOnDestroy() {
58
    this.subs.forEach(subscription => {
59
      if (subscription instanceof Subscriber) {
60
        subscription.unsubscribe();
61
      }
62
    });
63
  }
64
  
65
  public send(event) {
66
    HelperFunctions.scroll();
67
    if (event.valid === true) {
68
      this.sendMail(this.properties.admins);
69
    } else {
70
      this.errorMessage = 'Please fill in all the required fields!';
71
    }
72
  }
73
  
74
  public reset() {
75
    this.contactForm = this.fb.group({
76
      name: this.fb.control('', Validators.required),
77
      email: this.fb.control('', [Validators.required, Validators.email]),
78
      affiliation: this.fb.control('', Validators.required),
79
      organization: this.fb.control('', Validators.required),
80
      description: this.fb.control('', Validators.required),
81
      recaptcha: this.fb.control('', Validators.required),
82
    });
83
    this.errorMessage = '';
84
  }
85
  
86
  private sendMail(admins: any) {
87
    this.showLoading = true;
88
    this.subs.push(this.emailService.contact(this.properties,
89
      Composer.composeEmailForUsageCounts(this.contactForm.value, admins),
90
      this.contactForm.value.recaptcha).subscribe(
91
      res => {
92
        this.showLoading = false;
93
        if (res) {
94
          this.reset();
95
          this.modalOpen();
96
        } else {
97
          this.errorMessage = 'Email sent failed! Please try again.';
98
          this.contactForm.get('recaptcha').setValue('');
99
        }
100
      },
101
      error => {
102
        this.handleError('Email sent failed! Please try again.', error);
103
        this.showLoading = false;
104
        this.contactForm.get('recaptcha').setValue('');
105
      }
106
    ));
107
  }
108
  
109
  public modalOpen() {
110
    this.modal.okButton = true;
111
    this.modal.alertTitle = 'Your request has been successfully submitted';
112
    this.modal.alertMessage = false;
113
    this.modal.cancelButton = false;
114
    this.modal.okButtonLeft = false;
115
    this.modal.okButtonText = 'OK';
116
    this.modal.open();
117
  }
118
  
119
  handleError(message: string, error) {
120
    this.errorMessage = message;
121
    console.log('Server responded: ' + error);
122
    this.showLoading = false;
123
  }
124
  
125
  public goToHome() {
126
    this.router.navigate(['/']);
127
  }
128
}
(3-3/4)