1
|
import {Component, Input, OnDestroy, OnInit} from "@angular/core";
|
2
|
import {AbstractControl, FormBuilder, FormGroup, ValidationErrors, Validators} from "@angular/forms";
|
3
|
import {Subscriber} from "rxjs";
|
4
|
import {StringUtils} from "../../utils/string-utils.class";
|
5
|
import {Email} from "../../utils/email/email";
|
6
|
import {Body} from "../../utils/email/body";
|
7
|
import {CommunityService} from "../../connect/community/community.service";
|
8
|
import {Composer} from "../../utils/email/composer";
|
9
|
import {User} from "../../login/utils/helper.class";
|
10
|
import {EmailService} from "../../utils/email/email.service";
|
11
|
import {properties} from "../../../../environments/environment";
|
12
|
|
13
|
declare var UIkit;
|
14
|
|
15
|
@Component({
|
16
|
selector: 'subscriber-invite',
|
17
|
template: `
|
18
|
<div class="uk-grid uk-child-width-1-1" uk-grid [formGroup]="inviteForm">
|
19
|
<div dashboard-input [formInput]="inviteForm.get('name')" [gridSmall]="true">
|
20
|
<div class="uk-text-bold field-label">
|
21
|
From *:
|
22
|
</div>
|
23
|
</div>
|
24
|
<div dashboard-input [formInput]="inviteForm.get('recipients')" [gridSmall]="true" note="Separate multiple emails with a comma">
|
25
|
<div class="uk-text-bold field-label uk-margin-bottom">
|
26
|
To *:
|
27
|
</div>
|
28
|
</div>
|
29
|
<div class="uk-grid uk-grid-small" uk-grid>
|
30
|
<div class="uk-text-bold field-label">
|
31
|
Message *:
|
32
|
</div>
|
33
|
<div class="uk-width-expand">
|
34
|
<ckeditor class="form-control" formControlName="message" id="message"
|
35
|
debounce="400"
|
36
|
[config]="{ extraAllowedContent: '* [uk-*](*) ; span', disallowedContent: 'script; *[on*]', removeButtons: 'Save,NewPage,DocProps,Preview,Print',
|
37
|
extraPlugins: 'divarea'}"></ckeditor>
|
38
|
<div class="uk-margin-top">
|
39
|
{{body.signature}}
|
40
|
<span *ngIf="body.fromName == ''" class="uk-text-muted">
|
41
|
<i>{{body.fromMessage}}...</i>
|
42
|
</span>
|
43
|
<span *ngIf="body.fromName !=''">
|
44
|
{{body.fromMessage}}
|
45
|
<b>{{body.fromName}}</b>
|
46
|
</span><br>
|
47
|
<a href="https://www.openaire.eu">www.openaire.eu</a>
|
48
|
</div>
|
49
|
</div>
|
50
|
</div>
|
51
|
</div>
|
52
|
`,
|
53
|
styleUrls: ['subscriber-invite.component.css']
|
54
|
})
|
55
|
export class SubscriberInviteComponent implements OnInit, OnDestroy {
|
56
|
@Input()
|
57
|
public user: User;
|
58
|
public inviteForm: FormGroup;
|
59
|
public email: Email;
|
60
|
public body: Body;
|
61
|
public loading: boolean = false;
|
62
|
private subscriptions: any[] = [];
|
63
|
|
64
|
constructor(private fb: FormBuilder,
|
65
|
private emailService: EmailService,
|
66
|
private communityService: CommunityService) {
|
67
|
}
|
68
|
|
69
|
ngOnInit() {
|
70
|
this.reset();
|
71
|
}
|
72
|
|
73
|
ngOnDestroy() {
|
74
|
this.unsubscribe();
|
75
|
}
|
76
|
|
77
|
unsubscribe() {
|
78
|
this.subscriptions.forEach(subscription => {
|
79
|
if(subscription instanceof Subscriber) {
|
80
|
subscription.unsubscribe();
|
81
|
}
|
82
|
});
|
83
|
}
|
84
|
|
85
|
reset() {
|
86
|
this.unsubscribe();
|
87
|
this.inviteForm = this.fb.group({
|
88
|
name: this.fb.control('', Validators.required),
|
89
|
recipients: this.fb.control('', [Validators.required, this.emailsValidator]),
|
90
|
message: this.fb.control('', Validators.required)
|
91
|
});
|
92
|
this.subscriptions.push(this.communityService.getCommunityAsObservable().subscribe(community => {
|
93
|
this.inviteForm.get('name').enable();
|
94
|
this.inviteForm.get('name').setValue(this.user.fullname);
|
95
|
this.inviteForm.get('name').disable();
|
96
|
this.body = Composer.initializeInvitationsBody(community.communityId, community.title, this.user.fullname);
|
97
|
this.email = Composer.initializeInvitationsEmail(community.title);
|
98
|
this.inviteForm.get('message').setValue(this.body.paragraphs);
|
99
|
}));
|
100
|
}
|
101
|
|
102
|
emailsValidator(control: AbstractControl): ValidationErrors | null {
|
103
|
if (control.value === '' || !control.value || StringUtils.validateEmails(control.value)) {
|
104
|
return null;
|
105
|
}
|
106
|
return { emails: true };
|
107
|
}
|
108
|
|
109
|
invite() {
|
110
|
this.loading = true;
|
111
|
this.parseRecipients();
|
112
|
this.body.paragraphs = this.inviteForm.value.message;
|
113
|
this.email.body = Composer.formatEmailBodyForInvitation(this.body);
|
114
|
this.subscriptions.push(this.emailService.sendEmail(properties, this.email).subscribe(res => {
|
115
|
if(res['success']) {
|
116
|
UIkit.notification('Invitation to subscribe has been <b>sent</b>', {
|
117
|
status: 'success',
|
118
|
timeout: 6000,
|
119
|
pos: 'bottom-right'
|
120
|
});
|
121
|
} else {
|
122
|
UIkit.notification('An error has occurred. Please try again later', {
|
123
|
status: 'danger',
|
124
|
timeout: 6000,
|
125
|
pos: 'bottom-right'
|
126
|
});
|
127
|
}
|
128
|
this.loading = false;
|
129
|
},error => {
|
130
|
UIkit.notification('An error has occurred. Please try again later', {
|
131
|
status: 'danger',
|
132
|
timeout: 6000,
|
133
|
pos: 'bottom-right'
|
134
|
});
|
135
|
this.loading = false;
|
136
|
}));
|
137
|
}
|
138
|
|
139
|
public parseRecipients() {
|
140
|
// remove spaces
|
141
|
let emails = this.inviteForm.get('recipients').value.replace(/\s/g, '');
|
142
|
// remove commas
|
143
|
emails = emails.split(",");
|
144
|
|
145
|
// remove empty fields
|
146
|
for (let i = 0; i < emails.length; i++) {
|
147
|
if (!(emails[i] == "")) {
|
148
|
this.email.recipients.push(emails[i]);
|
149
|
}
|
150
|
}
|
151
|
}
|
152
|
|
153
|
get valid() {
|
154
|
return this.inviteForm && this.inviteForm.valid;
|
155
|
}
|
156
|
}
|