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