Project

General

Profile

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

    
4
import {Session} from '../../openaireLibrary/login/utils/helper.class';
5
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
6
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
7
import {ZenodoCommunitiesService} from '../../openaireLibrary/connect/zenodoCommunities/zenodo-communities.service';
8
import {SearchZenodoCommunitiesService} from '../../openaireLibrary/connect/zenodoCommunities/searchZenodoCommunities.service';
9

    
10
import {CommunityService} from '../../openaireLibrary/connect/community/community.service';
11
import {CommunityInfo} from '../../openaireLibrary/connect/community/communityInfo';
12
import {ZenodoCommunityInfo} from '../../openaireLibrary/connect/zenodoCommunities/zenodoCommunityInfo';
13
import {SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
14
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
15
import {HelperFunctions} from '../../openaireLibrary/utils/HelperFunctions.class';
16
import {Title} from '@angular/platform-browser';
17
import {properties} from '../../../environments/environment';
18
import {Subscription} from 'rxjs';
19
import {FullScreenModalComponent} from '../../openaireLibrary/utils/modal/full-screen-modal/full-screen-modal.component';
20
import {ManageZenodoCommunitiesComponent} from './manage-zenodo-communities.component';
21

    
22
@Component({
23
  selector: 'zenodo-communities',
24
  template: `
25
    <div *ngIf="zenodoSearchUtils.status == errorCodes.LOADING" page-content>
26
      <div header>
27
        <community-info tab="zenodo-communities"></community-info>
28
      </div>
29
      <div inner>
30
        <div class="uk-position-center">
31
          <loading></loading>
32
        </div>
33
      </div>
34
    </div>
35
    <ng-container *ngIf="zenodoSearchUtils.status != errorCodes.LOADING">
36
      <manage-zenodo-communities #manage *ngIf="communityId && zenodoSearchUtils" [masterCommunity]=masterZenodoCommunity [selectedCommunities]=selectedCommunities
37
                                 [properties]=properties [communityId]=communityId [searchUtils]=zenodoSearchUtils
38
                                 (toggleView)="toggleAction()"></manage-zenodo-communities>
39
    </ng-container>
40
    <fs-modal #fsModal (cancelEmitter)="toggleAction()">
41
      <add-zenodo-communities *ngIf="zenodoSearchUtils.status != errorCodes.LOADING" [masterCommunity]=masterZenodoCommunity [selectedCommunities]=selectedCommunities
42
                              [properties]=properties [communityId]=communityId></add-zenodo-communities>
43
    </fs-modal>
44
  `
45
})
46
export class ZenodoCommunitiesComponent implements OnInit, OnDestroy {
47
  public communityId: string = null;
48
  private community: CommunityInfo = null;
49
  public toggle = false;
50
  public updateCommunityProjectsOnToggle = false;
51
  public pageTitle = 'Manage zenodo communities';
52
  masterZenodoCommunityId = null;
53
  masterZenodoCommunity = null;
54
  public properties: EnvProperties = null;
55
  selectedCommunityIds = null;
56
  selectedCommunities = [];
57
  zenodocommunitiesloadedCount = 0;
58
  zenodoSearchUtils: SearchUtilsClass = new SearchUtilsClass();
59
  public errorCodes: ErrorCodes = new ErrorCodes();
60
  subscriptions = [];
61
  @ViewChild('fsModal', { static: true }) fullscreen: FullScreenModalComponent;
62
  @ViewChild('manage') manage: ManageZenodoCommunitiesComponent;
63
  
64
  constructor(private element: ElementRef, private route: ActivatedRoute, private _router: Router,
65
              private _zenodoCommunitieService: ZenodoCommunitiesService, private title: Title,
66
              private _communityService: CommunityService,
67
              private _searchZenodoCommunitiesService: SearchZenodoCommunitiesService) {
68
  }
69
  
70
  ngOnInit() {
71
    this.zenodoSearchUtils.status = this.errorCodes.LOADING;
72
    this.properties = properties;
73
    this.route.params.subscribe(params => {
74
      this.communityId = params['community'];
75
      if (this.communityId) {
76
        this.title.setTitle(this.communityId.toUpperCase() + ' | Zenodo Communities');
77
        if (this.communityId != null && this.communityId !== '') {
78
          this.subscriptions.push(this._communityService.getCommunityAsObservable().subscribe(
79
            community => {
80
              this.community = community;
81
              this.masterZenodoCommunityId = this.community.zenodoCommunity;
82
              if (this.masterZenodoCommunityId) {
83
                this.subscriptions.push(this._zenodoCommunitieService.getZenodoCommunityById(this.properties,
84
                  this.properties.zenodoCommunities + this.masterZenodoCommunityId,
85
                  null).subscribe(
86
                  result => {
87
                    this.masterZenodoCommunity = result;
88
                  },
89
                  error => {
90
                    const emptyCommunity: ZenodoCommunityInfo = new ZenodoCommunityInfo();
91
                    emptyCommunity.id = this.masterZenodoCommunityId;
92
                    emptyCommunity.title = this.masterZenodoCommunityId;
93
                    this.masterZenodoCommunity = emptyCommunity;
94
                    // console.log("Master Zenodo community'"+this.masterZenodoCommunityId+"' couldn't be loaded");
95
                  }
96
                ));
97
              }
98
              this.zenodoSearchUtils.status = this.errorCodes.LOADING;
99
              this._searchZenodoCommunitiesService.searchZCommunities(this.properties, this.communityId).subscribe(
100
                result => {
101
                  this.selectedCommunityIds = result;
102
                  this.zenodoSearchUtils.totalResults = this.selectedCommunityIds.length;
103
                  if (this.selectedCommunityIds.length === 0) {
104
                    this.zenodoSearchUtils.status = this.errorCodes.NONE;
105
                  }
106
                  for (let i = 0; i < this.selectedCommunityIds.length; i++) {
107
                    this.getZenodoCommunityById(
108
                      this.selectedCommunityIds[i]['zenodoid'],
109
                      this.selectedCommunityIds[i]['id']);
110
                  }
111
                  
112
                },
113
                error => {
114
                  console.log('list of zenodo communities couldn\'t be loaded');
115
                  this.zenodoSearchUtils.status = this.errorCodes.DONE;
116
                } // this.handleError('System error retrieving community profile', error)
117
              );
118
            }));
119
        }
120
      }
121
    });
122
    this.fullscreen.title = "Search and Add Zenodo Communities";
123
    this.fullscreen.okButtonText = "Done";
124
    this.fullscreen.okButton = true;
125
  }
126
  
127
  ngOnDestroy() {
128
    this.subscriptions.forEach(subscription => {
129
      if (subscription instanceof Subscription) {
130
        subscription.unsubscribe();
131
      }
132
    });
133
  }
134
  
135
  public toggleAction() {
136
    if (!Session.isLoggedIn()) {
137
      this._router.navigate(['/user-info'], {
138
        queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
139
      });
140
    } else {
141
      HelperFunctions.scroll();
142
      
143
      this.toggle = !this.toggle;
144
      if (this.toggle) {
145
        this.fullscreen.open();
146
        this.pageTitle = 'Manage zenodo communities';
147
      } else {
148
        this.manage.filterPreviewCommunities(this.manage.filterForm.value);
149
        this.updateCommunityProjectsOnToggle = false;
150
        this.pageTitle = 'Search zenodo communities';
151
      }
152
    }
153
  }
154
  
155
  getZenodoCommunityById(zenodoid, openaireId) {
156
    this.subscriptions.push(this._zenodoCommunitieService.getZenodoCommunityById(this.properties,
157
      this.properties.zenodoCommunities + zenodoid, openaireId).subscribe(
158
      result => {
159
        this.selectedCommunities.push(result);
160
        this.zenodocommunitiesloadedCount++;
161
        if (this.zenodocommunitiesloadedCount >= this.selectedCommunityIds.length) {
162
          this.zenodoSearchUtils.status = this.errorCodes.DONE;
163
        }
164
        
165
        
166
      },
167
      error => {
168
        const emptyCommunity: ZenodoCommunityInfo = new ZenodoCommunityInfo();
169
        emptyCommunity.id = zenodoid;
170
        emptyCommunity.openaireId = openaireId;
171
        emptyCommunity.title = zenodoid;
172
        this.selectedCommunities.push(emptyCommunity);
173
        this.zenodocommunitiesloadedCount++;
174
        if (this.zenodocommunitiesloadedCount >= this.selectedCommunityIds.length) {
175
          this.zenodoSearchUtils.status = this.errorCodes.DONE;
176
        }
177
        console.log('Zenodo community\'' + zenodoid + '\' couldn\'t be loaded');
178
      }
179
    ));
180
  }
181
}
(7-7/8)