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.affiliations.length, this.page * this.affiliations.length);
123
    } else {
124
      return [];
125
    }
126
  }
127
  
128
  editAffiliation() {
129
    this.loading = true;
130
    this.affiliationService.updateAffiliation(this.properties.communityAPI + this.communityId + '/organizations',
131
      this.affiliationFb.value).subscribe((affiliation) => {
132
        if (affiliation) {
133
          if (this.index === -1) {
134
            this.affiliations.push(affiliation);
135
          } else {
136
            this.affiliations[this.index] = affiliation;
137
          }
138
          if (affiliation.id) {
139
            this.handleUpdateSuccess('Your organization has been updated successfully!');
140
          } else {
141
            this.handleUpdateSuccess('Your organization has been saved successfully!');
142
          }
143
        }
144
      },
145
      error => {
146
        this.handleUpdateError('An error has been occurred. Try again later!');
147
      });
148
  }
149
  
150
  removeAffiliation() {
151
    this.loading = true;
152
    this.affiliationService.deleteAffiliation(this.properties.communityAPI + this.communityId + '/organizations',
153
      this.affiliations[this.index].id).subscribe((deleteOK) => {
154
        this.affiliations.splice(this.index, 1);
155
        if (this.currentPage.length === 0) {
156
          this.page = 1;
157
        }
158
        this.handleUpdateSuccess('Organization has been deleted');
159
      },
160
      error => {
161
        this.handleUpdateError('An error has been occurred. Try again later!');
162
      });
163
  }
164
  
165
  private organizationsPageStatus() {
166
    this.helpContentService.getCommunityPagesByRoute(this.communityId, '/organizations', this.properties.adminToolsAPIURL).subscribe((page) => {
167
      this.organizationsPage = page;
168
    })
169
  }
170
  
171
  handleUpdateError(message: string) {
172
    UIkit.notification(message, {
173
      status: 'danger',
174
      timeout: 6000,
175
      pos: 'bottom-right'
176
    });
177
    this.loading = false;
178
  }
179
  
180
  handleUpdateSuccess(message) {
181
    UIkit.notification(message, {
182
      status: 'success',
183
      timeout: 6000,
184
      pos: 'bottom-right'
185
    });
186
    this.loading = false;
187
  }
188
  
189
  enableAffiliations() {
190
    this.helpContentService.togglePages(this.communityId, [this.organizationsPage._id], true, this.properties.adminToolsAPIURL).subscribe(() => {
191
      this.organizationsPage.isEnabled = true;
192
      UIkit.notification('Organizations Page has been <b>enabled successfully</b>', {
193
        status: 'success',
194
        timeout: 6000,
195
        pos: 'bottom-right'
196
      });
197
    }, error => {
198
      this.handleUpdateError('An error has been occurred. Try again later!');
199
    });
200
  }
201
}
(3-3/4)