Project

General

Profile

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

    
4
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
5
import {Affiliation} from '../../openaireLibrary/utils/entities/CuratorInfo';
6
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
7
import {AffiliationService} from "../../openaireLibrary/connect/affiliations/affiliation.service";
8
import {HelpContentService} from "../../services/help-content.service";
9
import {Title} from '@angular/platform-browser';
10
import {StringUtils} from "../../openaireLibrary/utils/string-utils.class";
11
import {properties} from "../../../environments/environment";
12
import {Page} from "../../openaireLibrary/utils/entities/adminTool/page";
13
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
14
import {Subscription} from "rxjs";
15
import {CommunityService} from "../../openaireLibrary/connect/community/community.service";
16

    
17
declare var UIkit;
18

    
19
@Component({
20
  selector: 'affiliations',
21
  templateUrl: './affiliations.component.html',
22
})
23
export class AffiliationsComponent implements OnInit, OnDestroy {
24
  public loading = true;
25
  public properties: EnvProperties = properties;
26
  public index = 0;
27
  public affiliations: Affiliation[];
28
  public affiliationFb: FormGroup;
29
  public communityId: string;
30
  public organizationsPage: Page;
31
  public page: number = 1;
32
  public pageSize: number = 10;
33
  private subs: any[] = [];
34
  @ViewChild('affiliationModal') affiliationModal: AlertModal;
35
  @ViewChild('removeAffiliationModal') removeAffiliationModal: AlertModal;
36
  
37
  constructor(private route: ActivatedRoute,
38
              private router: Router,
39
              private title: Title,
40
              private fb: FormBuilder,
41
              private communityService: CommunityService,
42
              private affiliationService: AffiliationService,
43
              private helpContentService: HelpContentService) {
44
  }
45
  
46
  
47
  ngOnInit() {
48
    this.loading = true;
49
    this.subs.push(this.route.params.subscribe(params => {
50
      this.communityId = params['community'];
51
      this.title.setTitle(this.communityId.toUpperCase() + ' | Organizations');
52
      this.getAffiliations();
53
      this.organizationsPageStatus();
54
    }));
55
  }
56
  
57
  ngOnDestroy() {
58
    this.subs.forEach(sub => {
59
      if (sub instanceof Subscription) {
60
        sub.unsubscribe();
61
      }
62
    })
63
  }
64
  
65
  getAffiliations() {
66
    this.loading = true;
67
    this.affiliationService.initAffiliations(this.communityId);
68
    this.affiliationService.affiliations.subscribe(
69
      affiliations => {
70
        this.affiliations = affiliations;
71
        this.loading = false;
72
      },
73
      error => {
74
        console.error("Affiliations Component: Error getting affiliations for community with id: " + this.communityId, error);
75
        this.loading = false;
76
      }
77
    );
78
  }
79
  
80
  public get organizationsEnabled(): boolean {
81
    return !this.organizationsPage || this.organizationsPage.isEnabled;
82
  }
83
  
84
  editAffiliationOpen(index: number = -1) {
85
    let affiliation: Affiliation;
86
    this.index = index;
87
    if (index === -1) {
88
      affiliation = new Affiliation();
89
      affiliation.communityId = this.communityId;
90
      this.affiliationModal.alertTitle = 'Add Organization';
91
      this.affiliationModal.okButtonText = 'Add';
92
    } else {
93
      affiliation = this.affiliations[this.index];
94
      this.affiliationModal.alertTitle = 'Edit Organization';
95
      this.affiliationModal.okButtonText = 'Update';
96
    }
97
    this.affiliationFb = this.fb.group({
98
      communityId: this.fb.control(affiliation.communityId, Validators.required),
99
      id: this.fb.control(affiliation.id),
100
      name: this.fb.control(affiliation.name, Validators.required),
101
      logo_url: this.fb.control(affiliation.logo_url, [Validators.required, StringUtils.urlValidator()]),
102
      website_url: this.fb.control(affiliation.website_url, [Validators.required, StringUtils.urlValidator()])
103
    });
104
    this.affiliationModal.okButtonLeft = false;
105
    this.affiliationModal.cancelButtonText = 'Cancel';
106
    this.affiliationModal.open();
107
  }
108
  
109
  deleteAffiliationOpen(index: number) {
110
    this.index = index;
111
    let affiliation: Affiliation = this.affiliations[index];
112
    this.removeAffiliationModal.alertTitle = 'Delete Organization';
113
    this.removeAffiliationModal.message = 'Do you want to remove <b>' +
114
      affiliation.name + '</b> from Organizations?';
115
    this.removeAffiliationModal.okButtonText = 'Yes';
116
    this.removeAffiliationModal.cancelButtonText = 'No';
117
    this.removeAffiliationModal.open();
118
  }
119
  
120
  get currentPage(): Affiliation[] {
121
    if (this.affiliations) {
122
      return this.affiliations.slice((this.page - 1) * this.pageSize, this.page * this.pageSize);
123
    } else {
124
      return [];
125
    }
126
  }
127
  
128
  getIndex(index: number): number {
129
    return (this.page - 1)*this.pageSize + index;
130
  }
131
  
132
  public updatePage(event) {
133
    this.page = event.value;
134
  }
135
  
136
  editAffiliation() {
137
    this.loading = true;
138
    this.affiliationService.updateAffiliation(this.properties.communityAPI + this.communityId + '/organizations',
139
      this.affiliationFb.value).subscribe((affiliation) => {
140
        if (affiliation) {
141
          if (this.index === -1) {
142
            this.affiliations.push(affiliation);
143
            this.page = Math.ceil(this.affiliations.length/this.pageSize);
144
          } else {
145
            this.affiliations[this.index] = affiliation;
146
          }
147
          if (affiliation.id) {
148
            this.handleUpdateSuccess('Your organization has been updated successfully!');
149
          } else {
150
            this.handleUpdateSuccess('Your organization has been saved successfully!');
151
          }
152
        }
153
      },
154
      error => {
155
        this.handleUpdateError('An error has been occurred. Try again later!');
156
      });
157
  }
158
  
159
  removeAffiliation() {
160
    this.loading = true;
161
    this.affiliationService.deleteAffiliation(this.properties.communityAPI + this.communityId + '/organizations',
162
      this.affiliations[this.index].id).subscribe((deleteOK) => {
163
        this.affiliations.splice(this.index, 1);
164
        if (this.currentPage.length === 0) {
165
          this.page = 1;
166
        }
167
        this.handleUpdateSuccess('Organization has been deleted');
168
      },
169
      error => {
170
        this.handleUpdateError('An error has been occurred. Try again later!');
171
      });
172
  }
173
  
174
  private organizationsPageStatus() {
175
    this.helpContentService.getCommunityPagesByRoute(this.communityId, '/organizations', this.properties.adminToolsAPIURL).subscribe((page) => {
176
      this.organizationsPage = page;
177
    })
178
  }
179
  
180
  handleUpdateError(message: string) {
181
    UIkit.notification(message, {
182
      status: 'danger',
183
      timeout: 6000,
184
      pos: 'bottom-right'
185
    });
186
    this.loading = false;
187
  }
188
  
189
  handleUpdateSuccess(message) {
190
    UIkit.notification(message, {
191
      status: 'success',
192
      timeout: 6000,
193
      pos: 'bottom-right'
194
    });
195
    this.loading = false;
196
  }
197
  
198
  enableAffiliations() {
199
    this.helpContentService.togglePages(this.communityId, [this.organizationsPage._id], true, this.properties.adminToolsAPIURL).subscribe(() => {
200
      this.organizationsPage.isEnabled = true;
201
      UIkit.notification('Organizations Page has been <b>enabled successfully</b>', {
202
        status: 'success',
203
        timeout: 6000,
204
        pos: 'bottom-right'
205
      });
206
    }, error => {
207
      this.handleUpdateError('An error has been occurred. Try again later!');
208
    });
209
  }
210
}
(3-3/4)