Project

General

Profile

1 60693 k.triantaf
import {
2
  Component,
3
  EventEmitter,
4
  Input,
5
  OnChanges,
6
  OnDestroy,
7
  OnInit,
8
  Output, SimpleChanges,
9
  ViewChild,
10
  ViewEncapsulation
11
} from '@angular/core';
12 60565 argiro.kok
import {FormBuilder, FormControl} from '@angular/forms';
13
import {ActivatedRoute, Router} from '@angular/router';
14
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
15
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
16
import {SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
17 53735 konstantin
import {Session} from '../../openaireLibrary/login/utils/helper.class';
18
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
19 60565 argiro.kok
import {ManageZenodoCommunitiesService} from '../../services/manageZenodoCommunities.service';
20
import {SearchInputComponent} from '../../openaireLibrary/sharedComponents/search-input/search-input.component';
21
import {Subscription} from 'rxjs';
22 53735 konstantin
23 60638 argiro.kok
declare var UIkit;
24 60693 k.triantaf
25 53600 argiro.kok
@Component({
26 60693 k.triantaf
  selector: 'manage-zenodo-communities',
27
  templateUrl: './manage-zenodo-communities.component.html',
28
  encapsulation: ViewEncapsulation.None // this used in order styles to work
29 53600 argiro.kok
})
30
31 60693 k.triantaf
export class ManageZenodoCommunitiesComponent implements OnInit, OnDestroy {
32
  @Input() properties: EnvProperties = null;
33
  @Input() communityId = null;
34
35
  @Input() searchUtils: SearchUtilsClass = null;
36
37
  errorCodes: ErrorCodes;
38
  public rowsOnPage = 10;
39
  @Input() masterCommunity = null;
40
  @Input() selectedCommunities = [];
41
  previewCommunities = [];
42
43
  @ViewChild('AlertModalDeleteCommunity') alertModalDeleteCommunity;
44
  selectedToDelete = null;
45
  @Output() toggleView: EventEmitter<any> = new EventEmitter();
46
  page = 1;
47
  size = 10;
48
  @ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent;
49
  public filterForm: FormControl;
50
  private subscriptions: any[] = [];
51
52
  constructor(private route: ActivatedRoute,
53
              private _router: Router,
54
              public _fb: FormBuilder,
55
              private _manageZenodoCommunitiesService: ManageZenodoCommunitiesService) {
56
57
    this.errorCodes = new ErrorCodes();
58
59
  }
60
61
  ngOnInit() {
62
    this.init();
63
  }
64
65 60565 argiro.kok
  ngOnDestroy() {
66
    this.subscriptions.forEach(subscription => {
67
      if (subscription instanceof Subscription) {
68
        subscription.unsubscribe();
69
      }
70
    });
71
  }
72 60693 k.triantaf
73
  private init() {
74
    this.filterForm = this._fb.control('');
75
    this.filterPreviewCommunities("");
76
    this.subscriptions.push(this.filterForm.valueChanges.subscribe(value => {
77
      this.page = 1;
78
      this.filterPreviewCommunities(value);
79
    }));
80
    this.searchUtils.keyword = "";
81
    this.searchUtils.totalResults = this.selectedCommunities.length;
82
  }
83
84
  public filterPreviewCommunities(value: string) {
85
    this.previewCommunities = this.selectedCommunities.filter(community => {
86
      return community.title.toLowerCase().indexOf(value.toLowerCase()) != -1
87
    });
88
    if (this.previewCommunities.slice((this.page - 1) * this.rowsOnPage, this.page * this.rowsOnPage).length == 0) {
89
      this.page = 1;
90 60638 argiro.kok
    }
91 60693 k.triantaf
  }
92
93
  public confirmedDeleteCommunity(data: any) {
94
    if (!Session.isLoggedIn()) {
95
      this._router.navigate(['/user-info'], {
96
        queryParams: {
97
          "errorCode": LoginErrorCodes.NOT_VALID,
98
          "redirectUrl": this._router.url
99
        }
100
      });
101
    } else {
102
      this.subscriptions.push(this._manageZenodoCommunitiesService.removeZCommunity(this.properties, this.communityId, this.selectedToDelete.openaireId).subscribe(
103
        data => {
104
          var pos = -1;
105
          for (var i = 0; i < this.selectedCommunities.length; i++) {
106
            if (this.selectedCommunities[i].id == this.selectedToDelete.id) {
107
              pos = i;
108
              break;
109 53905 argiro.kok
            }
110 60565 argiro.kok
          }
111 60693 k.triantaf
          if (pos != -1) {
112
            this.selectedCommunities.splice(pos, 1);
113
            this.searchUtils.totalResults = this.selectedCommunities.length;
114
          }
115
          this.searchUtils.totalResults = this.selectedCommunities.length;
116
          this.filterPreviewCommunities(this.filterForm.value);
117
          UIkit.notification('Community has been <b>successfully removed</b>!', {
118
            status: 'success',
119
            timeout: 6000,
120
            pos: 'bottom-right'
121
          });
122
        },
123
        err => {
124
          this.handleError('An error has been occurred. Try again later!');
125
          console.log(err.status);
126
        }
127
      ));
128 53600 argiro.kok
    }
129 60693 k.triantaf
  }
130
131
  public removeCommunity(comm) {
132
    if (!Session.isLoggedIn()) {
133
      this._router.navigate(['/user-info'], {
134
        queryParams: {
135
          "errorCode": LoginErrorCodes.NOT_VALID,
136
          "redirectUrl": this._router.url
137 53905 argiro.kok
        }
138 60693 k.triantaf
      });
139
    } else {
140
      this.selectedToDelete = comm;
141
      this.alertModalDeleteCommunity.cancelButton = true;
142
      this.alertModalDeleteCommunity.okButton = true;
143
      this.alertModalDeleteCommunity.alertTitle = "Remove zenodo community?";
144
      let title = "";
145
      if (comm.title) {
146
        title = comm.title;
147 53735 konstantin
      }
148 60693 k.triantaf
149
150
      this.alertModalDeleteCommunity.message = "Zenodo community";
151
      if (title) {
152
        this.alertModalDeleteCommunity.message += " '" + title + "' ";
153
      }
154
      this.alertModalDeleteCommunity.message += "will be removed from your community. Are you sure?";
155
      this.alertModalDeleteCommunity.okButtonText = "Yes";
156
      this.alertModalDeleteCommunity.open();
157 53600 argiro.kok
    }
158 60693 k.triantaf
  }
159
160
161
  totalPages(): number {
162
    let totalPages: any = this.searchUtils.totalResults / (this.rowsOnPage);
163
    if (!(Number.isInteger(totalPages))) {
164
      totalPages = (parseInt(totalPages, 10) + 1);
165 53925 argiro.kok
    }
166 60693 k.triantaf
    return totalPages;
167
  }
168
169 60565 argiro.kok
  addNew() {
170
    this.toggleView.emit(null);
171
  }
172 60693 k.triantaf
173 60565 argiro.kok
  public onSearchClose() {
174
    this.searchUtils.keyword = this.filterForm.value;
175
  }
176 60693 k.triantaf
177 60565 argiro.kok
  public resetInput() {
178
    this.searchUtils.keyword = null;
179
    this.searchInputComponent.reset()
180
  }
181 60693 k.triantaf
182 60682 konstantin
  handleError(message: string) {
183
    UIkit.notification(message, {
184
      status: 'danger',
185
      timeout: 6000,
186
      pos: 'bottom-right'
187
    });
188
  }
189 53600 argiro.kok
}