connect/src/app/contact/contact.component.ts

137 lines
4.3 KiB
TypeScript
Raw Normal View History

import {Component, OnInit, Input, ElementRef, ViewChild} from '@angular/core';
import {FormGroup} from '@angular/forms';
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";
import {ContactForm} from "../openaireLibrary/utils/email/contact-form";
import {Composer} from "../openaireLibrary/utils/email/composer";
@Component({
selector: 'contact',
templateUrl: './contact.component.html',
})
export class ContactComponent implements OnInit {
@Input('group')
myForm: FormGroup;
public showLoading = true;
public errorMessage = '';
public isSubmitted = false;
public email: Email;
public note = '';
public properties: EnvProperties = null;
public contactForm: ContactForm = new ContactForm();
@ViewChild('AlertModal') modal;
@ViewChild('name') name;
@ViewChild('surname') surname;
@ViewChild('sender') sender;
@ViewChild('affiliation') affiliation;
@ViewChild('community') community;
@ViewChild('message') message;
@ViewChild('recaptcha') recaptcha;
constructor(private element: ElementRef,
private route: ActivatedRoute,
private _router: Router,
private _emailService: EmailService) {
}
ngOnInit() {
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
this.email = {body: '', subject: '', recipients: []};
this.scroll();
this.showLoading = false;
});
}
public scroll() {
if (typeof document !== 'undefined') {
this.element.nativeElement.scrollIntoView();
}
}
public send() {
this.isSubmitted = true;
if(!this.name.invalid && !this.surname.invalid && !this.sender.invalid &&
!this.affiliation.invalid && !this.message.invalid && this.contactForm.recaptcha != '') {
this.sendMail(this.properties.admins);
}
else {
this.errorMessage = 'Please fill all the required fields!'
}
}
public reset() {
this.contactForm = new ContactForm();
this.recaptcha.reset();
this.isSubmitted = false;
this.errorMessage = '';
}
private sendMail(admins: any) {
this._emailService.contact(this.properties.adminToolsAPIURL + '/contact',
Composer.composeEmailForNewCommunity(this.contactForm, admins), this.contactForm.recaptcha).subscribe(
res => {
if(res != null) {
this.modalOpen();
this.reset();
}
else {
this.handleError('Token has expired. Please complete the reCaptcha challenge again! ', null);
this.recaptcha.reset();
}
},
error => {
this.handleError('Email sent failed! Please try again.', error);
this.recaptcha.reset();
}
);
}
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;
this.modal.okButtonText = 'Ok';
this.modal.open();
}
public handleRecaptcha(captchaResponse: string) {
this.contactForm.recaptcha = captchaResponse;
}
handleError(message: string, error) {
this.errorMessage = message;
console.log('Server responded: ' + error);
this.showLoading = false;
}
getProductionPrefix():string{
return (this.properties.environment == "beta")?"beta.":""
}
isProduction():boolean{
return this.properties.environment!="development";
}
public goToHome(data: any) {
if(this.isProduction()) {
window.location.hostname = 'https://'+ this.getProductionPrefix() + 'connect.openaire.eu';
} else {
this._router.navigate(['/']);
}
}
}