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 {Meta, 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
import {SEOService} from "../openaireLibrary/sharedComponents/SEO/SEO.service";
14

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

    
20
export class ContactComponent implements OnInit {
21
  public url: string = null;
22
  public pageTitle: string = "OpenAIRE - Connect | Contact Us";
23

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

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

    
46
  constructor(private route: ActivatedRoute,
47
              private _router: Router,
48
              private _emailService: EmailService,
49
              private _meta: Meta,
50
              private _title: Title,
51
              private seoService: SEOService,
52
              private _piwikService: PiwikService,
53
              private helper: HelperService) {
54
  }
55

    
56
  ngOnInit() {
57
      this._title.setTitle('OpenAIRE-Connect | Contact Us');
58
      this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
59
          this.properties = data.envSpecific;
60
          this.email = {body: '', subject: '', recipients: []};
61

    
62
          if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
63
            this.piwiksub = this._piwikService.trackView(this.properties, this.pageTitle, this.properties.piwikSiteId).subscribe();
64
          }
65
          this.url = 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 - Connect, Community Gateway, research community");
70

    
71
          //this.getDivContents();
72
          this.getPageContents();
73
          HelperFunctions.scroll();
74
          this.showLoading = false;
75
      });
76
  }
77

    
78
  private getPageContents() {
79
      this.helper.getPageHelpContents(this._router.url, this.properties, 'connect').subscribe(contents => {
80
          this.pageContents = contents;
81
      })
82
  }
83

    
84
  private getDivContents() {
85
      this.helper.getDivHelpContents(this._router.url, this.properties, 'connect').subscribe(contents => {
86
          this.divContents = contents;
87
      })
88
  }
89

    
90
  public send() {
91
      HelperFunctions.scroll();
92
      if(!this.name.invalid && !this.surname.invalid && !this.sender.invalid &&
93
          this.contactForm.email.match('^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$') &&
94
          !this.affiliation.invalid && !this.message.invalid && this.contactForm.recaptcha != '') {
95
          this.sendMail(this.properties.admins);
96
      }
97
      else {
98
          this.errorMessage = 'Please fill in all the required fields!'
99
          this.isSubmitted = true;
100
      }
101
  }
102

    
103
  public reset() {
104
      this.contactForm = new ContactForm();
105
      this.isSubmitted = false;
106
      this.errorMessage = '';
107
      this.contactForm.recaptcha = '';
108
  }
109

    
110
   private sendMail(admins: any) {
111
       this.showLoading = true;
112
       this._emailService.contact(this.properties.adminToolsAPIURL + '/contact',
113
                                    Composer.composeEmailForNewCommunity(this.contactForm, admins), this.contactForm.recaptcha).subscribe(
114
             res => {
115
                 if(res) {
116
                     this.reset();
117
                     this.modalOpen();
118
                     this.showLoading = false;
119
                 }
120
             },
121
             error => {
122
                 this.handleError('Email sent failed! Please try again.', error);
123
                 this.showLoading = false;
124
                 this.contactForm.recaptcha = '';
125
             }
126
       );
127
   }
128

    
129
  public modalOpen() {
130
      this.modal.okButton = true;
131
      this.modal.alertTitle = 'Your request has been successfully submitted';
132
      this.modal.alertMessage = false;
133
      this.modal.cancelButton = false;
134
      this.modal.okButtonLeft = false;
135
      this.modal.okButtonText = 'OK';
136
      this.modal.open();
137
  }
138

    
139
  public handleRecaptcha(captchaResponse: string) {
140
      this.contactForm.recaptcha = captchaResponse;
141
  }
142

    
143

    
144
  handleError(message: string, error) {
145
      this.errorMessage = message;
146
      console.log('Server responded: ' + error);
147

    
148
      this.showLoading = false;
149
  }
150

    
151
  public goToHome(data: any) {
152
      this._router.navigate(['/']);
153
  }
154

    
155
  private updateDescription(description: string) {
156
    this._meta.updateTag({content: description}, "name='description'");
157
    this._meta.updateTag({content: description}, "property='og:description'");
158
  }
159

    
160
  private updateTitle(title: string) {
161
    var _title = ((title.length > 50) ? title.substring(0, 50) : title);
162
    this._title.setTitle(_title);
163
    this._meta.updateTag({content: _title}, "property='og:title'");
164
  }
165

    
166
  private updateUrl(url: string) {
167
    this._meta.updateTag({content: url}, "property='og:url'");
168
  }
169
}
(3-3/4)