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