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') 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
          
79
          this.subscriptions.push(this._communityService.getCommunityAsObservable().subscribe(
80
            community => {
81
              this.community = community;
82
              this.masterZenodoCommunityId = this.community.zenodoCommunity;
83
              if (this.masterZenodoCommunityId) {
84
                this.subscriptions.push(this._zenodoCommunitieService.getZenodoCommunityById(this.properties,
85
                  this.properties.zenodoCommunities + this.masterZenodoCommunityId,
86
                  null).subscribe(
87
                  result => {
88
                    this.masterZenodoCommunity = result;
89
                    
90
                  },
91
                  error => {
92
                    const emptyCommunity: ZenodoCommunityInfo = new ZenodoCommunityInfo();
93
                    emptyCommunity.id = this.masterZenodoCommunityId;
94
                    emptyCommunity.title = this.masterZenodoCommunityId;
95
                    this.masterZenodoCommunity = emptyCommunity;
96
                    // console.log("Master Zenodo community'"+this.masterZenodoCommunityId+"' couldn't be loaded");
97
                  }
98
                ));
99
              }
100
              this.zenodoSearchUtils.status = this.errorCodes.LOADING;
101
              this._searchZenodoCommunitiesService.searchZCommunities(this.properties, this.communityId).subscribe(
102
                result => {
103
                  this.selectedCommunityIds = result;
104
                  this.zenodoSearchUtils.totalResults = this.selectedCommunityIds.length;
105
                  if (this.selectedCommunityIds.length === 0) {
106
                    this.zenodoSearchUtils.status = this.errorCodes.NONE;
107
                  }
108
                  for (let i = 0; i < this.selectedCommunityIds.length; i++) {
109
                    this.getZenodoCommunityById(
110
                      this.selectedCommunityIds[i]['zenodoid'],
111
                      this.selectedCommunityIds[i]['id']);
112
                  }
113
                  
114
                },
115
                error => {
116
                  console.log('list of zenodo communities couldn\'t be loaded');
117
                  this.zenodoSearchUtils.status = this.errorCodes.ERROR;
118
                } // this.handleError('System error retrieving community profile', error)
119
              );
120
              
121
            },
122
            error => {
123
              console.log('Community couldn\'t be loaded');
124
              this.zenodoSearchUtils.status = this.errorCodes.ERROR;
125
            }
126
          ));
127
        }
128
      }
129
    });
130
    this.fullscreen.title = "Search and Add Zenodo Communities";
131
    this.fullscreen.okButtonText = "Done";
132
    this.fullscreen.okButton = true;
133
    
134
    
135
  }
136
  
137
  ngOnDestroy() {
138
    this.subscriptions.forEach(subscription => {
139
      if (subscription instanceof Subscription) {
140
        subscription.unsubscribe();
141
      }
142
    });
143
  }
144
  
145
  public toggleAction() {
146
    if (!Session.isLoggedIn()) {
147
      this._router.navigate(['/user-info'], {
148
        queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
149
      });
150
    } else {
151
      HelperFunctions.scroll();
152
      
153
      this.toggle = !this.toggle;
154
      if (this.toggle) {
155
        this.fullscreen.open();
156
        this.pageTitle = 'Manage zenodo communities';
157
      } else {
158
        this.manage.filterPreviewCommunities(this.manage.filterForm.value);
159
        this.updateCommunityProjectsOnToggle = false;
160
        this.pageTitle = 'Search zenodo communities';
161
      }
162
    }
163
  }
164
  
165
  getZenodoCommunityById(zenodoid, openaireId) {
166
    this.subscriptions.push(this._zenodoCommunitieService.getZenodoCommunityById(this.properties,
167
      this.properties.zenodoCommunities + zenodoid, openaireId).subscribe(
168
      result => {
169
        this.selectedCommunities.push(result);
170
        this.zenodocommunitiesloadedCount++;
171
        if (this.zenodocommunitiesloadedCount >= this.selectedCommunityIds.length) {
172
          this.zenodoSearchUtils.status = this.errorCodes.DONE;
173
        }
174
        
175
        
176
      },
177
      error => {
178
        const emptyCommunity: ZenodoCommunityInfo = new ZenodoCommunityInfo();
179
        emptyCommunity.id = zenodoid;
180
        emptyCommunity.openaireId = openaireId;
181
        emptyCommunity.title = zenodoid;
182
        this.selectedCommunities.push(emptyCommunity);
183
        this.zenodocommunitiesloadedCount++;
184
        if (this.zenodocommunitiesloadedCount >= this.selectedCommunityIds.length) {
185
          this.zenodoSearchUtils.status = this.errorCodes.DONE;
186
        }
187
        console.log('Zenodo community\'' + zenodoid + '\' couldn\'t be loaded');
188
      }
189
    ));
190
  }
191
}
(7-7/8)