1
|
import {Component, OnInit, Input} from '@angular/core';
|
2
|
import {SimpleChanges, OnChanges} from '@angular/core';
|
3
|
import {FormGroup, FormArray, FormBuilder, Validators} from "@angular/forms";
|
4
|
import {ActivatedRoute, Router} from '@angular/router';
|
5
|
|
6
|
import {Email} from '../../../openaireLibrary/utils/email/email';
|
7
|
import {Body} from '../../../openaireLibrary/utils/email/body';
|
8
|
|
9
|
import {EnvProperties} from '../../../openaireLibrary/utils/properties/env-properties';
|
10
|
import {EmailService} from '../../../openaireLibrary/utils/email/email.service';
|
11
|
|
12
|
@Component({
|
13
|
selector: 'invite',
|
14
|
templateUrl: './invite.component.html',
|
15
|
})
|
16
|
|
17
|
export class InviteComponent implements OnInit {
|
18
|
|
19
|
@Input() longView: boolean = true;
|
20
|
|
21
|
private properties: EnvProperties = null;
|
22
|
public email: Email;
|
23
|
public body: Body;
|
24
|
public recipients: string;
|
25
|
|
26
|
private ckeditorContent: string;
|
27
|
|
28
|
public defaultBody = '<p>OpenAIRE invites you to subscribe in <a href="https://beta.egi.openaire.eu">_Community_name_</a> dashboard. </p><p>The community dashboard is part of the <a href="https://beta.egi.openaire.eu">OpenAIRE-Connect</a> project.</p>';
|
29
|
|
30
|
@Input() communityId = null;
|
31
|
|
32
|
constructor (
|
33
|
private route: ActivatedRoute,
|
34
|
private _router: Router,
|
35
|
public _fb: FormBuilder,
|
36
|
private _emailService: EmailService) { }
|
37
|
|
38
|
|
39
|
public ngOnInit() {
|
40
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
41
|
this.properties = data.envSpecific;
|
42
|
this.body = {salutation: "Dear Sir/Madame,", fromMessage: "On behalf of ", fromName: "", paragraphs: this.defaultBody, closing: "Kind regards,", signature: "OpenAIRE team"};
|
43
|
this.email = {body: "", subject: "", recipients: []};
|
44
|
this.recipients = "";
|
45
|
|
46
|
});
|
47
|
}
|
48
|
|
49
|
public invite() {
|
50
|
if (this.recipients != "") {
|
51
|
if (this.validateEmails()) {
|
52
|
this.composeEmail();
|
53
|
console.log(this.email.body);
|
54
|
|
55
|
this._emailService.sendEmail(this.properties.sendMailUrl, this.email).subscribe(
|
56
|
res => console.log("The email has been sent successfully!")
|
57
|
,
|
58
|
error => console.log(error)
|
59
|
);
|
60
|
}
|
61
|
}
|
62
|
}
|
63
|
|
64
|
public validateEmails(): boolean {
|
65
|
if (this.parseEmails()) {
|
66
|
if (this.hasValidEmails()) {
|
67
|
return true;
|
68
|
}
|
69
|
}
|
70
|
return false;
|
71
|
}
|
72
|
|
73
|
public parseEmails(): boolean {
|
74
|
let email = new Array<string>();
|
75
|
|
76
|
// remove spaces
|
77
|
this.recipients = this.recipients.replace(/\s/g, '');
|
78
|
|
79
|
// remove commas
|
80
|
email = this.recipients.split(",");
|
81
|
|
82
|
// remove empty fields
|
83
|
for (let i = 0; i < email.length; i++) {
|
84
|
if (!(email[i] == "")) {
|
85
|
this.email.recipients.push(email[i]);
|
86
|
}
|
87
|
}
|
88
|
console.log(this.email.recipients);
|
89
|
return true;
|
90
|
}
|
91
|
|
92
|
private hasValidEmails(): boolean {
|
93
|
let length = this.email.recipients.length;
|
94
|
|
95
|
for(let i = 0; i < length; i++) {
|
96
|
if (!this.emailValidator(this.email.recipients[i])){
|
97
|
// TODO remove console message after final testing
|
98
|
console.log("INVALID EMAIL");
|
99
|
return false;
|
100
|
}
|
101
|
}
|
102
|
// TODO remove console message after final testing
|
103
|
console.log("ALL EMAILS ARE VALID");
|
104
|
return true;
|
105
|
}
|
106
|
|
107
|
private emailValidator(email : any): boolean {
|
108
|
if (email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"))
|
109
|
return true;
|
110
|
else
|
111
|
return false;
|
112
|
}
|
113
|
|
114
|
public composeEmail() {
|
115
|
this.email.subject = "[OpenAIRE-Connect] Community_name";
|
116
|
this.email.body = this.formatEmailBody();
|
117
|
}
|
118
|
|
119
|
public formatEmailBody(): string {
|
120
|
let fromMessageAndName = "";
|
121
|
|
122
|
if (this.body.fromName != "") {
|
123
|
fromMessageAndName = "<span>" + this.body.fromMessage + this.body.fromName + "</span>";
|
124
|
}
|
125
|
|
126
|
let formattedEmail = "<div>" + this.body.salutation + "<br><br>" +
|
127
|
fromMessageAndName + this.body.paragraphs +
|
128
|
"<p>" + this.body.closing + "<br>" + this.body.signature + "</p></div>";
|
129
|
|
130
|
return formattedEmail;
|
131
|
}
|
132
|
}
|