Project

General

Profile

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*]', removeButtons: 'Save,NewPage,DocProps,Preview,Print',
38
                                                                   extraPlugins: 'divarea'}"></ckeditor>
39
          <div *ngIf="!isManager" [innerHTML]="body.paragraphs"></div>
40
          <div class="uk-margin-top">
41
            {{body.signature}}
42
            <span *ngIf="body.fromName == ''" class="uk-text-muted">
43
              <i>{{body.fromMessage}}...</i>
44
            </span>
45
            <span *ngIf="body.fromName !=''">
46
              {{body.fromMessage}}
47
              <b>{{body.fromName}}</b>
48
            </span><br>
49
            <a href="https://www.openaire.eu">www.openaire.eu</a>
50
          </div>
51
        </div>
52
      </div>
53
    </div>
54
    <div *ngIf="!longView">
55
      <div dashboard-input [formInput]="inviteForm.get('recipients')" type="chips" placeholder="Write email(s)" [addExtraChips]="true" [validators]="validators" [gridSmall]="true"></div>
56
    </div>
57
  `,
58
  styleUrls: ['subscriber-invite.component.css']
59
})
60
export class SubscriberInviteComponent implements OnInit, OnDestroy {
61
  @Input()
62
  public user: User;
63
  @Input()
64
  public longView: boolean = true;
65
  public community: CommunityInfo;
66
  public inviteForm: FormGroup;
67
  public email: Email;
68
  public body: Body;
69
  public validators: ValidatorFn[] = [Validators.email, Validators.required];
70
  public loading: boolean = true;
71
  private subscriptions: any[] = [];
72
  
73
  constructor(private fb: FormBuilder,
74
              private emailService: EmailService,
75
              private communityService: CommunityService) {
76
  }
77
  
78
  ngOnInit() {
79
    this.loading = this.longView;
80
    this.reset();
81
  }
82
  
83
  ngOnDestroy() {
84
    this.unsubscribe();
85
  }
86
  
87
  unsubscribe() {
88
    this.subscriptions.forEach(subscription => {
89
      if(subscription instanceof Subscriber) {
90
        subscription.unsubscribe();
91
      }
92
    });
93
  }
94
  
95
  reset() {
96
    this.unsubscribe();
97
    this.inviteForm = this.fb.group({
98
      name: this.fb.control('', Validators.required),
99
      recipients: this.fb.array([], Validators.required),
100
      message: this.fb.control('', Validators.required)
101
    });
102
    this.subscriptions.push(this.communityService.getCommunityAsObservable().subscribe(community => {
103
      this.community = community;
104
      this.inviteForm.get('name').enable();
105
      this.inviteForm.get('name').setValue(this.user.fullname);
106
      this.inviteForm.get('name').disable();
107
      this.body = Composer.initializeInvitationsBody(community.communityId, community.title, this.user.fullname);
108
      this.email = Composer.initializeInvitationsEmail(community.title);
109
      this.inviteForm.get('message').setValue(this.body.paragraphs);
110
    }));
111
    if(!this.isManager) {
112
      this.loading = false;
113
    }
114
  }
115
  
116
  invite() {
117
    this.loading = true;
118
    this.body.paragraphs = this.inviteForm.value.message;
119
    this.email.body = Composer.formatEmailBodyForInvitation(this.body);
120
    this.email.recipients = this.inviteForm.get('recipients').value;
121
    this.subscriptions.push(this.emailService.sendEmail(properties, this.email).subscribe(res => {
122
      if(res['success']) {
123
        UIkit.notification('Invitation to subscribe has been <b>sent</b>', {
124
          status: 'success',
125
          timeout: 6000,
126
          pos: 'bottom-right'
127
        });
128
      } else {
129
        UIkit.notification('An error has occurred. Please try again later', {
130
          status: 'danger',
131
          timeout: 6000,
132
          pos: 'bottom-right'
133
        });
134
      }
135
      this.reset();
136
      this.loading = false;
137
    },error => {
138
      UIkit.notification('An error has occurred. Please try again later', {
139
        status: 'danger',
140
        timeout: 6000,
141
        pos: 'bottom-right'
142
      });
143
      this.reset();
144
      this.loading = false;
145
    }));
146
  }
147
  
148
  get isManager() {
149
    return Session.isPortalAdministrator(this.user) || Session.isCurator('community', this.user) || Session.isManager('community', this.community.communityId, this.user);
150
  }
151
  
152
  get valid() {
153
    return !this.loading && this.inviteForm && this.inviteForm.valid;
154
  }
155
}
(2-2/3)