import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core'; import {Session, User} from '../../openaireLibrary/login/utils/helper.class'; import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties'; import {ActivatedRoute} from '@angular/router'; import {CommunitiesService} from '../../openaireLibrary/connect/communities/communities.service'; import {CommunityInfo} from '../../openaireLibrary/connect/community/communityInfo'; import {UserManagementService} from '../../openaireLibrary/services/user-management.service'; import {Title} from '@angular/platform-browser'; import {properties} from "../../../environments/environment"; import {Subscription} from "rxjs"; import {SearchInputComponent} from '../../openaireLibrary/sharedComponents/search-input/search-input.component'; import {FormBuilder, FormGroup} from '@angular/forms'; import {SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class'; import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes'; type Tab = 'all' | 'communities' | 'ris'; @Component({ selector: 'manage-communities', template: `
Admin Dashboard - Manage Communities

Research Communities & Initiatives

No communities to manage yet

Research Communities

No communities found

Research Initiatives

No initiatives found
` }) export class ManageCommunitiesComponent implements OnInit, OnDestroy { public properties: EnvProperties = properties; public communities: CommunityInfo[] = []; public filteredCommunities: CommunityInfo[] = []; public ris: CommunityInfo[] = []; public filteredRis: CommunityInfo[] = []; public loading: boolean; public user: User; public stickyPageHeader: boolean = false; public tab: Tab = "all"; private subscriptions: any[] = []; public communitySearchUtils: SearchUtilsClass = new SearchUtilsClass(); public errorCodes: ErrorCodes; // Search @ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent; filterForm: FormGroup; private searchText: string = ''; constructor(private route: ActivatedRoute, private _fb: FormBuilder, private communitiesService: CommunitiesService, private title: Title, private userManagementService: UserManagementService) { this.errorCodes = new ErrorCodes(); this.communitySearchUtils.status = this.errorCodes.LOADING; } ngOnInit() { this.communitySearchUtils.keyword = ""; this.filterForm = this._fb.group({ keyword: [''], }); this.subscriptions.push(this.filterForm.get('keyword').valueChanges.subscribe(value => { this.searchText = value ? value.toLowerCase() : ''; this.applyFilters(); })); this.loading = true; this.title.setTitle('Administrator Dashboard | Manage Communities'); this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => { this.user = user; if (this.user) { this.subscriptions.push(this.communitiesService.getCommunities(this.properties, this.properties.communityAPI + 'communities').subscribe( communities => { this.communities = []; this.filteredCommunities = []; this.ris = []; this.filteredRis = []; communities.forEach(community => { if (Session.isPortalAdministrator(this.user) || Session.isCommunityCurator(this.user) || Session.isManager('community', community.communityId, this.user)) { if (community.type === 'community') { this.communities.push(community); this.filteredCommunities.push(community); } else { this.ris.push(community); this.filteredRis.push(community); } } }) this.loading = false; })); } })); } ngOnDestroy() { this.subscriptions.forEach(subscription => { if (subscription instanceof Subscription) { subscription.unsubscribe(); } }); } public get noResults(): boolean { return (this.tab === "all" && this.communities.length === 0 && this.ris.length === 0) || (this.tab === "communities" && this.communities.length === 0) || (this.tab === "ris" && this.ris.length === 0); } public applyFilters() { this.filteredCommunities = this.communities.filter(community => community.shortTitle.toLowerCase().includes(this.searchText) || community.title.toLowerCase().includes(this.searchText) || community.communityId.toLowerCase().includes(this.searchText)); this.filteredRis = this.ris.filter(community => community.shortTitle.toLowerCase().includes(this.searchText) || community.title.toLowerCase().includes(this.searchText) || community.communityId.toLowerCase().includes(this.searchText)); } public onSearchClose() { this.communitySearchUtils.keyword = this.filterForm.get('keyword').value; } public resetInput() { this.communitySearchUtils.keyword = null; this.searchInputComponent.reset() } }