Project

General

Profile

« Previous | Next » 

Revision 60968

[Library | Trunk]: Fix server errors. Input add validators for chips. Subscriber-invite: Change To field to chips, add short view

View differences:

subscriber-invite.component.ts
1 1
import {Component, Input, OnDestroy, OnInit} from "@angular/core";
2
import {AbstractControl, FormBuilder, FormGroup, ValidationErrors, Validators} from "@angular/forms";
2
import {AbstractControl, FormBuilder, FormGroup, ValidationErrors, ValidatorFn, Validators} from "@angular/forms";
3 3
import {Subscriber} from "rxjs";
4 4
import {StringUtils} from "../../utils/string-utils.class";
5 5
import {Email} from "../../utils/email/email";
......
16 16
@Component({
17 17
  selector: 'subscriber-invite',
18 18
  template: `
19
    <div class="uk-grid uk-child-width-1-1" uk-grid [formGroup]="inviteForm">
19
    <div *ngIf="longView" class="uk-grid uk-child-width-1-1" uk-grid [formGroup]="inviteForm">
20 20
      <div dashboard-input [formInput]="inviteForm.get('name')" [gridSmall]="true">
21 21
        <div class="uk-text-bold field-label">
22 22
          From:
23 23
        </div>
24 24
      </div>
25
      <div dashboard-input [formInput]="inviteForm.get('recipients')" [gridSmall]="true">
26
        <div class="uk-text-bold field-label uk-margin-bottom">
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 27
          To *:
28 28
        </div>
29
        <span note>Separate multiple emails with a comma</span>
30 29
      </div>
31 30
      <div class="uk-grid uk-grid-small" uk-grid>
32 31
        <div class="uk-text-bold field-label">
......
34 33
        </div>
35 34
        <div class="uk-width-expand">
36 35
          <ckeditor *ngIf="isManager" class="form-control" formControlName="message" id="message"
37
                    debounce="400"
36
                    debounce="400" (ready)="loading = false"
38 37
                    [config]="{ extraAllowedContent: '* [uk-*](*) ; span', disallowedContent: 'script; *[on*]', removeButtons: 'Save,NewPage,DocProps,Preview,Print',
39 38
                                                                   extraPlugins: 'divarea'}"></ckeditor>
40 39
          <div *ngIf="!isManager" [innerHTML]="body.paragraphs"></div>
......
52 51
        </div>
53 52
      </div>
54 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>
55 57
  `,
56 58
  styleUrls: ['subscriber-invite.component.css']
57 59
})
58 60
export class SubscriberInviteComponent implements OnInit, OnDestroy {
59 61
  @Input()
60 62
  public user: User;
61
  public community: CommunityInfo
63
  @Input()
64
  public longView: boolean = true;
65
  public community: CommunityInfo;
62 66
  public inviteForm: FormGroup;
63 67
  public email: Email;
64 68
  public body: Body;
65
  public loading: boolean = false;
69
  public validators: ValidatorFn[] = [Validators.email, Validators.required];
70
  public loading: boolean = true;
66 71
  private subscriptions: any[] = [];
67 72
  
68 73
  constructor(private fb: FormBuilder,
......
71 76
  }
72 77
  
73 78
  ngOnInit() {
79
    this.loading = this.longView;
74 80
    this.reset();
75 81
  }
76 82
  
......
90 96
    this.unsubscribe();
91 97
    this.inviteForm = this.fb.group({
92 98
      name: this.fb.control('', Validators.required),
93
      recipients: this.fb.control('', [Validators.required, this.emailsValidator]),
99
      recipients: this.fb.array([], Validators.required),
94 100
      message: this.fb.control('', Validators.required)
95 101
    });
96 102
    this.subscriptions.push(this.communityService.getCommunityAsObservable().subscribe(community => {
......
102 108
      this.email = Composer.initializeInvitationsEmail(community.title);
103 109
      this.inviteForm.get('message').setValue(this.body.paragraphs);
104 110
    }));
105
  }
106
  
107
  emailsValidator(control: AbstractControl): ValidationErrors | null {
108
    if (control.value === '' || !control.value || StringUtils.validateEmails(control.value)) {
109
      return null;
111
    if(!this.isManager) {
112
      this.loading = false;
110 113
    }
111
    return { emails: true };
112 114
  }
113 115
  
114 116
  invite() {
115 117
    this.loading = true;
116
    this.parseRecipients();
117 118
    this.body.paragraphs = this.inviteForm.value.message;
118 119
    this.email.body = Composer.formatEmailBodyForInvitation(this.body);
120
    this.email.recipients = this.inviteForm.get('recipients').value;
119 121
    this.subscriptions.push(this.emailService.sendEmail(properties, this.email).subscribe(res => {
120 122
      if(res['success']) {
121 123
        UIkit.notification('Invitation to subscribe has been <b>sent</b>', {
......
130 132
          pos: 'bottom-right'
131 133
        });
132 134
      }
135
      this.reset();
133 136
      this.loading = false;
134 137
    },error => {
135 138
      UIkit.notification('An error has occurred. Please try again later', {
......
137 140
        timeout: 6000,
138 141
        pos: 'bottom-right'
139 142
      });
143
      this.reset();
140 144
      this.loading = false;
141 145
    }));
142 146
  }
143 147
  
144
  public parseRecipients() {
145
    // remove spaces
146
    let emails = this.inviteForm.get('recipients').value.replace(/\s/g, '');
147
    // remove commas
148
    emails = emails.split(",");
149
    
150
    // remove empty fields
151
    for (let i = 0; i < emails.length; i++) {
152
      if (!(emails[i] == "")) {
153
        this.email.recipients.push(emails[i]);
154
      }
155
    }
156
  }
157
  
158 148
  get isManager() {
159 149
    return Session.isPortalAdministrator(this.user) || Session.isCurator('community', this.user) || Session.isManager('community', this.community.communityId, this.user);
160 150
  }
161 151
  
162 152
  get valid() {
163
    return this.inviteForm && this.inviteForm.valid;
153
    return !this.loading && this.inviteForm && this.inviteForm.valid;
164 154
  }
165 155
}

Also available in: Unified diff