2021-01-21 16:15:53 +01:00
|
|
|
import {Component, Input, OnDestroy, OnInit} from "@angular/core";
|
2021-04-23 18:25:52 +02:00
|
|
|
import {AbstractControl, FormBuilder, FormGroup, ValidationErrors, ValidatorFn, Validators} from "@angular/forms";
|
2021-01-21 16:15:53 +01:00
|
|
|
import {Subscriber} from "rxjs";
|
|
|
|
import {StringUtils} from "../../utils/string-utils.class";
|
|
|
|
import {Email} from "../../utils/email/email";
|
|
|
|
import {Body} from "../../utils/email/body";
|
|
|
|
import {CommunityService} from "../../connect/community/community.service";
|
|
|
|
import {Composer} from "../../utils/email/composer";
|
2021-01-21 17:22:16 +01:00
|
|
|
import {Session, User} from "../../login/utils/helper.class";
|
2021-01-21 16:15:53 +01:00
|
|
|
import {EmailService} from "../../utils/email/email.service";
|
|
|
|
import {properties} from "../../../../environments/environment";
|
2021-01-21 17:22:16 +01:00
|
|
|
import {CommunityInfo} from "../../connect/community/communityInfo";
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
declare var UIkit;
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'subscriber-invite',
|
|
|
|
template: `
|
2021-04-23 18:25:52 +02:00
|
|
|
<div *ngIf="longView" class="uk-grid uk-child-width-1-1" uk-grid [formGroup]="inviteForm">
|
2021-01-21 16:15:53 +01:00
|
|
|
<div dashboard-input [formInput]="inviteForm.get('name')" [gridSmall]="true">
|
|
|
|
<div class="uk-text-bold field-label">
|
2021-01-22 19:09:44 +01:00
|
|
|
From:
|
2021-01-21 16:15:53 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-04-23 18:25:52 +02:00
|
|
|
<div dashboard-input [formInput]="inviteForm.get('recipients')" type="chips" placeholder="Write email(s)" [addExtraChips]="true" [validators]="validators" [gridSmall]="true">
|
|
|
|
<div class="uk-text-bold field-label">
|
2021-01-21 16:15:53 +01:00
|
|
|
To *:
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="uk-grid uk-grid-small" uk-grid>
|
|
|
|
<div class="uk-text-bold field-label">
|
|
|
|
Message *:
|
|
|
|
</div>
|
|
|
|
<div class="uk-width-expand">
|
2021-01-21 17:22:16 +01:00
|
|
|
<ckeditor *ngIf="isManager" class="form-control" formControlName="message" id="message"
|
2021-04-23 18:25:52 +02:00
|
|
|
debounce="400" (ready)="loading = false"
|
2021-05-10 19:56:10 +02:00
|
|
|
[config]="{ extraAllowedContent: '* [uk-*](*) ; span', disallowedContent: 'script; *[on*]',
|
|
|
|
removeButtons: 'Save,NewPage,DocProps,Preview,Print,' +
|
|
|
|
'Form,Checkbox,Radio,TextField,Textarea,Select,Button,ImageButton,HiddenField,' +
|
|
|
|
'CreateDiv,Flash,PageBreak,' +
|
|
|
|
'Subscript,Superscript,Anchor,Smiley,Iframe,Styles,Font,About,Language',
|
|
|
|
extraPlugins: 'divarea'}">
|
|
|
|
</ckeditor>
|
2021-01-21 17:22:16 +01:00
|
|
|
<div *ngIf="!isManager" [innerHTML]="body.paragraphs"></div>
|
2021-01-21 16:15:53 +01:00
|
|
|
<div class="uk-margin-top">
|
|
|
|
{{body.signature}}
|
|
|
|
<span *ngIf="body.fromName == ''" class="uk-text-muted">
|
|
|
|
<i>{{body.fromMessage}}...</i>
|
|
|
|
</span>
|
|
|
|
<span *ngIf="body.fromName !=''">
|
|
|
|
{{body.fromMessage}}
|
|
|
|
<b>{{body.fromName}}</b>
|
|
|
|
</span><br>
|
|
|
|
<a href="https://www.openaire.eu">www.openaire.eu</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-04-23 18:25:52 +02:00
|
|
|
<div *ngIf="!longView">
|
|
|
|
<div dashboard-input [formInput]="inviteForm.get('recipients')" type="chips" placeholder="Write email(s)" [addExtraChips]="true" [validators]="validators" [gridSmall]="true"></div>
|
|
|
|
</div>
|
2021-01-21 16:15:53 +01:00
|
|
|
`,
|
|
|
|
styleUrls: ['subscriber-invite.component.css']
|
|
|
|
})
|
|
|
|
export class SubscriberInviteComponent implements OnInit, OnDestroy {
|
|
|
|
@Input()
|
|
|
|
public user: User;
|
2021-04-23 18:25:52 +02:00
|
|
|
@Input()
|
|
|
|
public longView: boolean = true;
|
|
|
|
public community: CommunityInfo;
|
2021-01-21 16:15:53 +01:00
|
|
|
public inviteForm: FormGroup;
|
|
|
|
public email: Email;
|
|
|
|
public body: Body;
|
2021-04-23 18:25:52 +02:00
|
|
|
public validators: ValidatorFn[] = [Validators.email, Validators.required];
|
|
|
|
public loading: boolean = true;
|
2021-01-21 16:15:53 +01:00
|
|
|
private subscriptions: any[] = [];
|
|
|
|
|
|
|
|
constructor(private fb: FormBuilder,
|
|
|
|
private emailService: EmailService,
|
|
|
|
private communityService: CommunityService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2021-04-23 18:25:52 +02:00
|
|
|
this.loading = this.longView;
|
2021-01-21 16:15:53 +01:00
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe() {
|
|
|
|
this.subscriptions.forEach(subscription => {
|
|
|
|
if(subscription instanceof Subscriber) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.unsubscribe();
|
|
|
|
this.inviteForm = this.fb.group({
|
|
|
|
name: this.fb.control('', Validators.required),
|
2021-04-23 18:25:52 +02:00
|
|
|
recipients: this.fb.array([], Validators.required),
|
2021-01-21 16:15:53 +01:00
|
|
|
message: this.fb.control('', Validators.required)
|
|
|
|
});
|
|
|
|
this.subscriptions.push(this.communityService.getCommunityAsObservable().subscribe(community => {
|
2021-01-21 17:22:16 +01:00
|
|
|
this.community = community;
|
2021-01-21 16:15:53 +01:00
|
|
|
this.inviteForm.get('name').enable();
|
|
|
|
this.inviteForm.get('name').setValue(this.user.fullname);
|
|
|
|
this.inviteForm.get('name').disable();
|
|
|
|
this.body = Composer.initializeInvitationsBody(community.communityId, community.title, this.user.fullname);
|
|
|
|
this.email = Composer.initializeInvitationsEmail(community.title);
|
|
|
|
this.inviteForm.get('message').setValue(this.body.paragraphs);
|
|
|
|
}));
|
2021-04-23 18:25:52 +02:00
|
|
|
if(!this.isManager) {
|
|
|
|
this.loading = false;
|
2021-01-21 16:15:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
invite() {
|
|
|
|
this.loading = true;
|
|
|
|
this.body.paragraphs = this.inviteForm.value.message;
|
|
|
|
this.email.body = Composer.formatEmailBodyForInvitation(this.body);
|
2021-04-23 18:25:52 +02:00
|
|
|
this.email.recipients = this.inviteForm.get('recipients').value;
|
2021-01-21 16:15:53 +01:00
|
|
|
this.subscriptions.push(this.emailService.sendEmail(properties, this.email).subscribe(res => {
|
|
|
|
if(res['success']) {
|
|
|
|
UIkit.notification('Invitation to subscribe has been <b>sent</b>', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
}
|
2021-04-23 18:25:52 +02:00
|
|
|
this.reset();
|
2021-01-21 16:15:53 +01:00
|
|
|
this.loading = false;
|
|
|
|
},error => {
|
|
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
2021-04-23 18:25:52 +02:00
|
|
|
this.reset();
|
2021-01-21 16:15:53 +01:00
|
|
|
this.loading = false;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-01-21 17:22:16 +01:00
|
|
|
get isManager() {
|
2021-04-21 11:15:38 +02:00
|
|
|
return Session.isPortalAdministrator(this.user) || Session.isCurator('community', this.user) || Session.isManager('community', this.community.communityId, this.user);
|
2021-01-21 17:22:16 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 16:15:53 +01:00
|
|
|
get valid() {
|
2021-04-23 18:25:52 +02:00
|
|
|
return !this.loading && this.inviteForm && this.inviteForm.valid;
|
2021-01-21 16:15:53 +01:00
|
|
|
}
|
|
|
|
}
|