Project

General

Profile

1
import {Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3

    
4
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
5

    
6
import {Session} from '../../openaireLibrary/login/utils/helper.class';
7
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
8
import {Affiliation} from '../../openaireLibrary/utils/entities/CuratorInfo';
9
import {HelperFunctions} from '../../openaireLibrary/utils/HelperFunctions.class';
10
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
11
import {AffiliationService} from "../../openaireLibrary/connect/affiliations/affiliation.service";
12
import {HelpContentService} from "../../services/help-content.service";
13
import {Title} from '@angular/platform-browser';
14
import {StringUtils} from "../../openaireLibrary/utils/string-utils.class";
15

    
16
@Component({
17
  selector: 'affiliations',
18
  templateUrl: './affiliations.component.html',
19
})
20

    
21
export class AffiliationsComponent implements OnInit {
22

    
23
  @ViewChild('affiliationModal') affiliationModal: AlertModal;
24
  @ViewChild('removeAffiliationModal') removeAffiliationModal: AlertModal;
25
  public showLoading = false;
26
  public message = '';
27
  public messageType = '';
28

    
29
  public affiliation: Affiliation = new Affiliation();
30
  public properties: EnvProperties = null;
31

    
32
  private index = 0;
33
  private maxCharacters = 70;
34

    
35
  @Input() hasChanged: boolean = false;
36
  @Input() curatorAffiliations: boolean = false;
37
  @Input() public affiliations: Affiliation[] = [];
38
  @Output() affiliationsChange: EventEmitter<boolean> = new EventEmitter();
39
  @Output() resetCuratorMessages: EventEmitter<boolean> = new EventEmitter();
40
  public communityId: string;
41
  public organizationsPageId: string;
42
  public organizationsEnabled = false;
43

    
44
  constructor(private element: ElementRef,
45
              private route: ActivatedRoute,
46
              private _router: Router,
47
              private title: Title,
48
              private affiliationService: AffiliationService,
49
              private _helpContentService: HelpContentService) {
50
  }
51

    
52

    
53
  ngOnInit() {
54
    this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
55
      this.properties = data.envSpecific;
56
      if(!this.curatorAffiliations) {
57
        this.title.setTitle('Administration Dashboard | Related Organizations');
58
      }
59
      if (!Session.isLoggedIn()) {
60
        this._router.navigate(['/user-info'], {
61
          queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
62
        });
63
      } else {
64
        this.showLoading = true;
65
        this.message = '';
66

    
67
        this.route.queryParams.subscribe(
68
          communityId => {
69
            this.communityId = communityId['communityId'];
70

    
71
            if(!this.curatorAffiliations) {
72
              this.getAffiliations();
73
            }
74
            this.organizationsPageStatus();
75
          }
76
        );
77
      }
78
    });
79
  }
80

    
81
  getAffiliations() {
82
    this.affiliationService.initAffiliations(this.communityId);
83
    this.affiliationService.affiliations.subscribe(
84
      affiliations => {
85
        this.affiliations = affiliations;
86
        this.getOrganizationsPageId();
87
        //this.showLoading = false;
88
      },
89
      error => {
90
        console.error("Affiliations Component: Error getting affiliations for community with id: "+this.communityId, error);
91
        this.showLoading = false;
92
      }
93
    );
94
  }
95

    
96
  getOrganizationsPageId() {
97
    this._helpContentService.getCommunityPageByRoute("/organizations", this.properties.adminToolsAPIURL, this.communityId).subscribe(
98
      page => {
99
        if(page) {
100
          this.organizationsPageId = page._id;
101
        }
102
        this.showLoading = false;
103
      },
104
      error => {
105
        console.error("Affiliations Component: Error getting page with route '/organizations' for community with id: "+this.communityId, error);
106
        this.showLoading = false;
107
      }
108
    );
109
  }
110

    
111
  initAffiliation(affiliation: Affiliation = null) {
112
    this.resetMessages();
113

    
114
    if (affiliation) {
115
      this.affiliation = {...affiliation};
116
      if(!this.curatorAffiliations) {
117
        this.affiliation.communityId = affiliation.communityId;
118
        this.affiliation.id = affiliation.id;
119
      }
120
      if(this.curatorAffiliations) {
121
        this.affiliationModal.okButtonText = 'OK';
122
      } else {
123
        this.affiliationModal.okButtonText = 'Save Affiliation';
124
      }
125
    } else {
126
      this.index = -1;
127
      this.affiliation = new Affiliation();
128
      if(!this.curatorAffiliations) {
129
        this.affiliation.communityId = this.communityId;
130
      }
131
    }
132
    this.affiliationModal.okButtonLeft = false;
133
    if(this.curatorAffiliations) {
134
      this.affiliationModal.okButtonText = 'OK';
135
    } else {
136
      this.affiliationModal.okButtonText = 'Save Affiliation';
137
    }
138
    this.affiliationModal.open();
139
  }
140

    
141
  public chooseAffiliation(index: number, action: string = 'delete') {
142
    this.resetMessages();
143
    this.index = index;
144
    const affiliation: Affiliation = this.affiliations[index];
145
    if (action === 'delete') {
146
      this.removeAffiliationModal.message = 'Do you want to remove ' +
147
        affiliation.name +  ' from your Affiliations?';
148
      this.removeAffiliationModal.okButtonText = 'Yes';
149
      this.removeAffiliationModal.cancelButtonText = 'No';
150
      this.removeAffiliationModal.open();
151
    } else if (action === 'edit') {
152
      this.initAffiliation(affiliation);
153
    }
154
  }
155

    
156
  updateCommunityAffiliations(index: number) {
157
    if (!Session.isLoggedIn()) {
158
      this._router.navigate(['/user-info'], {
159
        queryParams:
160
          {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
161
      });
162
    } else {
163
      HelperFunctions.scroll();
164

    
165
        this.showLoading = true;
166

    
167
        this.affiliationService.updateAffiliation(this.properties.communityAPI + this.communityId + '/organizations',
168
          this.affiliation).subscribe((affiliation) => {
169
            if (affiliation) {
170
              if (index === -1) {
171
                this.affiliations.push(affiliation);
172
              } else {
173
                this.affiliations[index] = affiliation;
174
              }
175
              if(this.affiliation.id) {
176
                this.handleUpdateSuccess('Your organization has been updated successfully!');
177
              } else {
178
                this.handleUpdateSuccess('Your organization has been saved successfully!');
179
              }
180
            }
181
          },
182
          error => {
183
            if(this.affiliation.id) {
184
              this.handleUpdateError('Your organization could not be updated. Try again later!', error);
185
            } else {
186
              this.handleUpdateError('Organization could not be saved. Try again later!', error);
187
            }
188
          });
189
    }
190
  }
191

    
192
  addAffiliation() {
193
    if (!this.isEmptyAffiliation()) {
194
          if (!this.curatorAffiliations) {
195
            this.updateCommunityAffiliations(this.index);
196
          } else {
197
            if (this.index === -1) {
198
              this.affiliations.push(this.affiliation);
199
            } else {
200
              this.affiliations[this.index] = this.affiliation;
201
            }
202
          }
203
          this.change();
204
    }
205
  }
206

    
207
  removeAffiliation() {
208
    if(!this.curatorAffiliations) {
209
      HelperFunctions.scroll();
210

    
211
      this.showLoading = true;
212
      this.affiliationService.deleteAffiliation(this.properties.communityAPI + this.communityId + '/organizations',
213
        this.affiliations[this.index].id).subscribe((deleteOK) => {
214
          this.affiliations.splice(this.index, 1);
215
          this.handleUpdateSuccess('Organization has been deleted');
216
        },
217
        error => {
218
          this.handleUpdateError('Organization could not be deleted. Try again later!', error);
219
        }
220
      );
221
    } else {
222
      this.affiliations.splice(this.index, 1);
223
      this.change();
224
    }
225
  }
226

    
227
  private organizationsPageStatus() {
228
    this._helpContentService.getCommunityFull(this.communityId, this.properties.adminToolsAPIURL).subscribe((community) => {
229
      for(let page of community.pages) {
230
        if(page['route'] === '/organizations') {
231
          this.organizationsEnabled = page['isEnabled'];
232
          return;
233
        }
234
      }
235
      this.organizationsEnabled = false;
236
    })
237
  }
238

    
239
  private change() {
240
    this.hasChanged = true;
241
    if(this.curatorAffiliations) {
242
      this.affiliationsChange.emit(this.hasChanged);
243
    }
244
  }
245

    
246
  private resetMessages() {
247
    this.message = '';
248
    if(this.curatorAffiliations) {
249
      this.resetCuratorMessages.emit(true);
250
    }
251
  }
252

    
253
  handleUpdateError(message: string, error) {
254
    this.resetMessages();
255
    this.message = message;
256
    this.messageType = "warning";
257
    console.log('Server responded: ', error);
258

    
259
    this.showLoading = false;
260
  }
261

    
262
  handleUpdateSuccess(message) {
263
    this.resetMessages();
264
    this.message = message;
265
    this.messageType = "success";
266
    this.showLoading = false;
267
  }
268

    
269
  isEmptyAffiliation(): boolean {
270
    return ((!this.affiliation.name || this.affiliation.name === '') ||
271
      (!this.affiliation.logo_url || this.affiliation.logo_url === '') ||
272
      (!this.affiliation.website_url || this.affiliation.website_url === ''));
273
  }
274

    
275
  _format(name: string){
276
    if(name) {
277
      return (((name).length > this.maxCharacters) ? (name.substring(0, (this.maxCharacters - ('...').length)) + '...') : name);
278
    } else {
279
      return null;
280
    }
281
  }
282

    
283
  public urlPrefix(url: string): string {
284
    return StringUtils.urlPrefix(url);
285
  }
286

    
287
}
(3-3/4)