2019-04-10 00:09:38 +02:00
|
|
|
import {Component, OnInit, Input, ElementRef, ViewChild} from '@angular/core';
|
2019-04-12 10:54:55 +02:00
|
|
|
import {FormGroup} from '@angular/forms';
|
2019-04-10 00:09:38 +02:00
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {EmailService} from "../openaireLibrary/utils/email/email.service";
|
|
|
|
import {Email} from "../openaireLibrary/utils/email/email";
|
|
|
|
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
|
2019-04-12 12:17:07 +02:00
|
|
|
import {ContactForm} from "../openaireLibrary/utils/email/contact-form";
|
2019-04-10 20:53:39 +02:00
|
|
|
import {Composer} from "../openaireLibrary/utils/email/composer";
|
2019-04-12 13:54:49 +02:00
|
|
|
import {Title} from "@angular/platform-browser";
|
|
|
|
import {PiwikService} from "../openaireLibrary/utils/piwik/piwik.service";
|
2019-04-10 00:09:38 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'contact',
|
|
|
|
templateUrl: './contact.component.html',
|
|
|
|
})
|
|
|
|
|
2019-04-10 20:53:39 +02:00
|
|
|
export class ContactComponent implements OnInit {
|
2019-04-10 00:09:38 +02:00
|
|
|
|
|
|
|
@Input('group')
|
|
|
|
myForm: FormGroup;
|
2019-04-12 13:54:49 +02:00
|
|
|
public piwiksub: any;
|
2019-04-10 00:09:38 +02:00
|
|
|
public showLoading = true;
|
2019-04-10 20:53:39 +02:00
|
|
|
public errorMessage = '';
|
|
|
|
public isSubmitted = false;
|
2019-04-10 00:09:38 +02:00
|
|
|
public email: Email;
|
|
|
|
public note = '';
|
|
|
|
public properties: EnvProperties = null;
|
|
|
|
|
2019-04-10 20:53:39 +02:00
|
|
|
public contactForm: ContactForm = new ContactForm();
|
2019-04-12 10:54:55 +02:00
|
|
|
@ViewChild('AlertModal') modal;
|
2019-04-10 20:53:39 +02:00
|
|
|
@ViewChild('name') name;
|
|
|
|
@ViewChild('surname') surname;
|
|
|
|
@ViewChild('sender') sender;
|
|
|
|
@ViewChild('affiliation') affiliation;
|
|
|
|
@ViewChild('community') community;
|
|
|
|
@ViewChild('message') message;
|
|
|
|
@ViewChild('recaptcha') recaptcha;
|
2019-04-10 00:09:38 +02:00
|
|
|
|
2019-04-10 20:53:39 +02:00
|
|
|
constructor(private element: ElementRef,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private _router: Router,
|
2019-04-12 13:54:49 +02:00
|
|
|
private _emailService: EmailService,
|
|
|
|
private _title: Title,
|
|
|
|
private _piwikService: PiwikService) {
|
2019-04-10 20:53:39 +02:00
|
|
|
}
|
2019-04-10 00:09:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
2019-04-15 09:46:44 +02:00
|
|
|
this._title.setTitle('OpenAIRE-Connect | Contact Us');
|
2019-04-10 00:09:38 +02:00
|
|
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
this.email = {body: '', subject: '', recipients: []};
|
2019-04-12 13:54:49 +02:00
|
|
|
if(this.properties.enablePiwikTrack && (typeof document !== 'undefined')){
|
|
|
|
this.piwiksub = this._piwikService.trackView(this.properties, "OpenAIRE Connect|Contact Us", this.properties.piwikSiteId).subscribe();
|
|
|
|
}
|
2019-04-10 00:09:38 +02:00
|
|
|
this.scroll();
|
|
|
|
this.showLoading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public scroll() {
|
2019-04-10 20:53:39 +02:00
|
|
|
if (typeof document !== 'undefined') {
|
|
|
|
this.element.nativeElement.scrollIntoView();
|
2019-04-10 00:09:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 20:53:39 +02:00
|
|
|
public send() {
|
2019-04-16 13:20:28 +02:00
|
|
|
this.scroll();
|
2019-04-10 20:53:39 +02:00
|
|
|
if(!this.name.invalid && !this.surname.invalid && !this.sender.invalid &&
|
2019-04-13 20:35:01 +02:00
|
|
|
this.contactForm.email.match('^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$') &&
|
2019-04-12 10:54:55 +02:00
|
|
|
!this.affiliation.invalid && !this.message.invalid && this.contactForm.recaptcha != '') {
|
2019-04-10 20:53:39 +02:00
|
|
|
this.sendMail(this.properties.admins);
|
2019-04-10 00:09:38 +02:00
|
|
|
}
|
2019-04-12 10:54:55 +02:00
|
|
|
else {
|
2019-04-15 09:46:44 +02:00
|
|
|
this.errorMessage = 'Please fill in all the required fields!'
|
2019-04-16 13:20:28 +02:00
|
|
|
this.isSubmitted = true;
|
2019-04-12 10:54:55 +02:00
|
|
|
}
|
2019-04-10 00:09:38 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 20:53:39 +02:00
|
|
|
public reset() {
|
|
|
|
this.contactForm = new ContactForm();
|
2019-04-12 10:54:55 +02:00
|
|
|
this.isSubmitted = false;
|
|
|
|
this.errorMessage = '';
|
2019-04-16 13:20:28 +02:00
|
|
|
this.contactForm.recaptcha = '';
|
2019-04-10 00:09:38 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 20:53:39 +02:00
|
|
|
private sendMail(admins: any) {
|
2019-04-16 13:20:28 +02:00
|
|
|
this.showLoading = true;
|
2019-04-11 13:31:23 +02:00
|
|
|
this._emailService.contact(this.properties.adminToolsAPIURL + '/contact',
|
2019-04-11 13:27:26 +02:00
|
|
|
Composer.composeEmailForNewCommunity(this.contactForm, admins), this.contactForm.recaptcha).subscribe(
|
2019-04-10 20:53:39 +02:00
|
|
|
res => {
|
2019-04-16 13:30:51 +02:00
|
|
|
if(res) {
|
2019-04-12 10:54:55 +02:00
|
|
|
this.reset();
|
2019-04-16 13:20:28 +02:00
|
|
|
this.modalOpen();
|
|
|
|
this.showLoading = false;
|
2019-04-12 10:54:55 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-04-16 13:20:28 +02:00
|
|
|
this.handleError('Token has expired. Please complete the reCaptcha challenge again! ', null)
|
|
|
|
this.showLoading = false;
|
|
|
|
this.contactForm.recaptcha = '';
|
2019-04-12 10:54:55 +02:00
|
|
|
}
|
2019-04-10 20:53:39 +02:00
|
|
|
},
|
2019-04-12 10:54:55 +02:00
|
|
|
error => {
|
|
|
|
this.handleError('Email sent failed! Please try again.', error);
|
2019-04-16 13:20:28 +02:00
|
|
|
this.showLoading = false;
|
|
|
|
this.contactForm.recaptcha = '';
|
2019-04-12 10:54:55 +02:00
|
|
|
}
|
2019-04-10 20:53:39 +02:00
|
|
|
);
|
|
|
|
}
|
2019-04-10 00:09:38 +02:00
|
|
|
|
2019-04-12 10:54:55 +02:00
|
|
|
public modalOpen() {
|
|
|
|
this.modal.okButton = true;
|
|
|
|
this.modal.alertTitle = 'Your request has been successfully submitted';
|
|
|
|
this.modal.alertMessage = false;
|
|
|
|
this.modal.cancelButton = false;
|
|
|
|
this.modal.okButtonLeft = false;
|
2019-04-12 16:56:40 +02:00
|
|
|
this.modal.okButtonText = 'OK';
|
2019-04-12 10:54:55 +02:00
|
|
|
this.modal.open();
|
|
|
|
}
|
2019-04-10 00:09:38 +02:00
|
|
|
|
2019-04-10 20:53:39 +02:00
|
|
|
public handleRecaptcha(captchaResponse: string) {
|
2019-04-11 13:27:26 +02:00
|
|
|
this.contactForm.recaptcha = captchaResponse;
|
2019-04-10 00:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
handleError(message: string, error) {
|
|
|
|
this.errorMessage = message;
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
2019-04-12 10:54:55 +02:00
|
|
|
public goToHome(data: any) {
|
2019-04-13 20:21:51 +02:00
|
|
|
this._router.navigate(['/']);
|
2019-04-12 10:54:55 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 00:09:38 +02:00
|
|
|
}
|