Project

General

Profile

1
import {Component, OnInit, Input, ElementRef}            from '@angular/core';
2
import {SimpleChanges, OnChanges}                        from '@angular/core';
3
import {FormGroup, FormArray, FormBuilder, Validators}   from "@angular/forms";
4
import {ActivatedRoute, Router}                          from '@angular/router';
5

    
6
import {ConnectHelper}                                   from '../../../openaireLibrary/connect/connectHelper';
7

    
8
import {Email}                                           from '../../../openaireLibrary/utils/email/email';
9
import {Body}                                            from '../../../openaireLibrary/utils/email/body';
10
import {Validator}                                       from '../../../openaireLibrary/utils/email/validator';
11
import {Composer}                                        from '../../../openaireLibrary/utils/email/composer';
12

    
13
import {EnvProperties}                                   from '../../../openaireLibrary/utils/properties/env-properties';
14
import {EmailService}                                    from '../../../openaireLibrary/utils/email/email.service';
15
import {CommunityService}                                from "../../../openaireLibrary/connect/community/community.service";
16
import {ErrorCodes}                                      from '../../../openaireLibrary/utils/properties/errorCodes';
17
import {Session} from '../../../openaireLibrary/login/utils/helper.class';
18

    
19
@Component({
20
    selector: 'invite',
21
    templateUrl: './invite.component.html',
22
})
23

    
24
export class InviteComponent implements OnInit {
25

    
26
  @Input() longView: boolean = true;
27
  @Input() communityId = null;
28

    
29
  private properties: EnvProperties = null;
30

    
31
  public community = null;
32
  //public showLoading: boolean = true;
33
  public errorMessage: string = '';
34
  public successfulSentMessage: string = '';
35
  public inviteErrorMessage: string = '';
36
  public missingCommunityId: string = '';
37
  public showAddRecipientMessage: boolean = false;
38
  public showAddNameMessage: boolean = false;
39

    
40
  public email: Email;
41
  public body: Body;
42
  public recipients: string;
43
  public fullname:string;
44

    
45
  public areValid: boolean = true;
46

    
47
  private ckeditorContent: string;
48

    
49
  // public defaultBody ='';
50

    
51
  public communityIdParam = {};
52
  public status: number = 1;
53
  public errorCodes: ErrorCodes;
54

    
55
  constructor (
56
      private element: ElementRef,
57
      private route: ActivatedRoute,
58
      private _router: Router,
59
      public _fb: FormBuilder,
60
      private _emailService: EmailService,
61
      private _communityService: CommunityService) {
62

    
63
        this.route.queryParams.subscribe(
64
          communityId => {
65
                this.communityId = communityId['communityId'];
66
                if(!this.communityId){
67
                  this.communityId  = ConnectHelper.getCommunityFromDomain(document.location.hostname);
68
                }
69
                this.communityIdParam = (ConnectHelper.isProduction(document.location.hostname))?{}:{communityId:this.communityId};
70
          });
71
          this.errorCodes = new ErrorCodes();
72
          this.status = this.errorCodes.LOADING;
73
      }
74

    
75

    
76
  public ngOnInit() {
77
      this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
78
          this.properties = data.envSpecific;
79
          this.errorMessage = "";
80
          this.missingCommunityId = "";
81
          this.status = this.errorCodes.LOADING;
82
          if (this.communityId != null && this.communityId != '') {
83
            this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
84
              community => {
85
                this.community = community;
86
                this.fullname = Session.getUserFullName();
87
                console.log("Fullname from session " + Session.getUserFullName());
88

    
89
                this.body = Composer.initializeInvitationsBody(this.communityId, this.community.title, this.fullname);
90
                this.email = Composer.initializeInvitationsEmail(community.title);
91
                this.recipients = "";
92

    
93
                this.status = this.errorCodes.DONE;
94
              },
95
              error => this.handleError(error)
96
            );
97
          } else {
98
            this.status = this.errorCodes.DONE;
99
            this.missingCommunityId = "There is no community selected!";
100
          }
101

    
102
          this.scroll();
103
      });
104
  }
105

    
106
  public scroll() {
107
    console.info("scroll into view");
108
    if (typeof document !== 'undefined') {
109
       this.element.nativeElement.scrollIntoView();
110
    }
111
  }
112

    
113
  public invite() {
114
      this.successfulSentMessage = "";
115
      this.inviteErrorMessage = "";
116
      this.status = this.errorCodes.LOADING;
117
      if (this.recipients != "" && this.body.fromName != "") {
118
        if (this.validateEmails()) {
119
          this.composeEmail();
120
          console.log(this.email.body);
121

    
122
          this._emailService.sendEmail(this.properties.adminToolsAPIURL + "/sendMail/", this.email).subscribe(
123
              res => {
124
                console.log("The email has been sent successfully!");
125
                this.status = this.errorCodes.DONE;
126
                console.log("Emails Sent: "+res);
127
                if(res > 1) {
128
                  this.successfulSentMessage = res + " emails sent successfully!";
129
                } else {
130
                  this.successfulSentMessage = res + " email sent successfully!";
131
                }
132

    
133
                this.body = Composer.initializeInvitationsBody(this.communityId, this.community.title, this.fullname);
134
                this.email = Composer.initializeInvitationsEmail(this.community.title);
135
                this.recipients = "";
136
              },
137
              error => {
138
                console.log(error);
139
                this.status = this.errorCodes.DONE;
140
                this.inviteErrorMessage = "There was an error sending emails. Please try again.";
141
              }
142
          );
143
        } else {
144
          this.scroll();
145
          this.status = this.errorCodes.DONE;
146
        }
147
      } else {
148
        this.showAddRecipientMessage = true;
149
        this.scroll();
150
        this.status = this.errorCodes.DONE;
151
      }
152
  }
153

    
154
  public resetMessages() {
155
      this.errorMessage = "";
156
      this.successfulSentMessage = "";
157
      this.inviteErrorMessage = "";
158
  }
159

    
160
  public validateEmails(): boolean {
161
      if (this.parseEmails()) {
162
          if (Validator.hasValidEmails(this.email.recipients)) {
163
            return this.areValid;
164
          }
165
      }
166
      this.areValid = false;
167
      return this.areValid;
168
  }
169

    
170
  public parseEmails(): boolean {
171
      let email = new Array<string>();
172

    
173
      // remove spaces
174
      this.recipients = this.recipients.replace(/\s/g, '');
175

    
176
      // remove commas
177
      email = this.recipients.split(",");
178

    
179
      // remove empty fields
180
      for (let i = 0; i < email.length; i++) {
181
        if (!(email[i] == "")) {
182
          this.email.recipients.push(email[i]);
183
        }
184
      }
185
      console.log(this.email.recipients);
186
      return true;
187
  }
188

    
189
  public composeEmail() {
190
      // this.email.subject = "[OpenAIRE-Connect] Community_name";
191
      this.email.body = Composer.formatEmailBodyForInvitation(this.body);
192
  }
193

    
194
  public handleError(error) {
195
    if(error.status == '401') {
196
      this.status = this.errorCodes.FORBIDDEN;
197
    } else if(error.status == '403') {
198
      this.status = this.errorCodes.FORBIDDEN;
199
    } else if(error.status == '404') {
200
      this.status = this.errorCodes.NOT_FOUND;
201
    } else if(error.status == '500') {
202
      this.status = this.errorCodes.ERROR;
203
    } else {
204
      this.status = this.errorCodes.NOT_AVAILABLE;
205
    }
206
      console.log('Server responded: ' + error);
207
  }
208
  allowEdit(){
209
    var email = Session.getUserEmail();
210
    var index =-1;
211
    if(email && this.community != null && this.community.managers != null){
212
        index = this.community.managers.indexOf(email);
213
    }
214

    
215
    return Session.isPortalAdministrator() || Session.isCommunityCurator()
216
  }
217
}
(3-3/4)