Skip to content

Commit

Permalink
Moved home COAR logic to separate component to be able to easily drop…
Browse files Browse the repository at this point in the history
… the component when the feature is unused and improve first page load
  • Loading branch information
alexandrevryghem committed Apr 10, 2024
1 parent 97a96f2 commit fbcacf2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 75 deletions.
92 changes: 92 additions & 0 deletions src/app/home-page/home-coar/home-coar.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { isPlatformServer } from '@angular/common';
import {
Component,
Inject,
OnDestroy,
OnInit,
PLATFORM_ID,
} from '@angular/core';
import {
of as observableOf,
Subscription,
} from 'rxjs';
import { switchMap } from 'rxjs/operators';

import { NotifyInfoService } from '../../core/coar-notify/notify-info/notify-info.service';
import {
LinkDefinition,
LinkHeadService,
} from '../../core/services/link-head.service';
import { ServerResponseService } from '../../core/services/server-response.service';
import { isNotEmpty } from '../../shared/empty.util';

@Component({
selector: 'ds-home-coar',
template: '',
standalone: true,
})
export class HomeCoarComponent implements OnInit, OnDestroy {

/**
* An array of LinkDefinition objects representing inbox links for the home page.
*/
inboxLinks: LinkDefinition[] = [];

subs: Subscription[] = [];

constructor(
protected linkHeadService: LinkHeadService,
protected notifyInfoService: NotifyInfoService,
protected responseService: ServerResponseService,
@Inject(PLATFORM_ID) protected platformId: string,
) {
}

ngOnInit(): void {
// Get COAR REST API URLs from REST configuration
// only if COAR configuration is enabled
this.subs.push(this.notifyInfoService.isCoarConfigEnabled().pipe(
switchMap((coarLdnEnabled: boolean) => coarLdnEnabled ? this.notifyInfoService.getCoarLdnLocalInboxUrls() : observableOf([])),
).subscribe((coarRestApiUrls: string[]) => {
if (coarRestApiUrls.length > 0) {
this.initPageLinks(coarRestApiUrls);
}
}));
}

/**
* It removes the inbox links from the head of the html.
*/
ngOnDestroy(): void {
this.subs.forEach((sub: Subscription) => sub.unsubscribe());
this.inboxLinks.forEach((link: LinkDefinition) => {
this.linkHeadService.removeTag(`href='${link.href}'`);
});
}

/**
* Initializes page links for COAR REST API URLs.
* @param coarRestApiUrls An array of COAR REST API URLs.
*/
private initPageLinks(coarRestApiUrls: string[]): void {
const rel = this.notifyInfoService.getInboxRelationLink();
let links = '';
coarRestApiUrls.forEach((coarRestApiUrl: string) => {
// Add link to head
const tag: LinkDefinition = {
href: coarRestApiUrl,
rel: rel,
};
this.inboxLinks.push(tag);
this.linkHeadService.addTag(tag);

links = links + (isNotEmpty(links) ? ', ' : '') + `<${coarRestApiUrl}> ; rel="${rel}"`;
});

if (isPlatformServer(this.platformId)) {
// Add link to response header
this.responseService.setHeader('Link', links);
}
}

}
1 change: 1 addition & 0 deletions src/app/home-page/home-page.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<ds-home-coar></ds-home-coar>
<ds-themed-home-news></ds-themed-home-news>
<div [ngClass]="showDiscoverFilters ? 'container-fluid' : 'container'">
<ds-page-with-sidebar [sidebarContent]="sidebar" [sideBarWidth]="showDiscoverFilters ? 3 : 0" [class]="showDiscoverFilters ? 'row mx-3' : ''">
Expand Down
79 changes: 5 additions & 74 deletions src/app/home-page/home-page.component.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,32 @@
import {
AsyncPipe,
isPlatformServer,
NgClass,
NgIf,
} from '@angular/common';
import {
Component,
Inject,
OnDestroy,
OnInit,
PLATFORM_ID,
} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { TranslateModule } from '@ngx-translate/core';
import {
Observable,
of as observableOf,
Subscription,
} from 'rxjs';
import {
map,
switchMap,
} from 'rxjs/operators';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import {
APP_CONFIG,
AppConfig,
} from 'src/config/app-config.interface';

import { NotifyInfoService } from '../core/coar-notify/notify-info/notify-info.service';
import {
LinkDefinition,
LinkHeadService,
} from '../core/services/link-head.service';
import { ServerResponseService } from '../core/services/server-response.service';
import { Site } from '../core/shared/site.model';
import { SuggestionsPopupComponent } from '../notifications/suggestions-popup/suggestions-popup.component';
import { ConfigurationSearchPageComponent } from '../search-page/configuration-search-page.component';
import { ThemedConfigurationSearchPageComponent } from '../search-page/themed-configuration-search-page.component';
import { isNotEmpty } from '../shared/empty.util';
import { HostWindowService } from '../shared/host-window.service';
import { ThemedSearchFormComponent } from '../shared/search-form/themed-search-form.component';
import { PageWithSidebarComponent } from '../shared/sidebar/page-with-sidebar.component';
import { SidebarService } from '../shared/sidebar/sidebar.service';
import { ViewTrackerComponent } from '../statistics/angulartics/dspace/view-tracker.component';
import { HomeCoarComponent } from './home-coar/home-coar.component';
import { ThemedHomeNewsComponent } from './home-news/themed-home-news.component';
import { RecentItemListComponent } from './recent-item-list/recent-item-list.component';
import { ThemedTopLevelCommunityListComponent } from './top-level-community-list/themed-top-level-community-list.component';
Expand All @@ -52,30 +36,20 @@ import { ThemedTopLevelCommunityListComponent } from './top-level-community-list
styleUrls: ['./home-page.component.scss'],
templateUrl: './home-page.component.html',
standalone: true,
imports: [ThemedHomeNewsComponent, NgIf, ViewTrackerComponent, ThemedSearchFormComponent, ThemedTopLevelCommunityListComponent, RecentItemListComponent, AsyncPipe, TranslateModule, NgClass, ConfigurationSearchPageComponent, SuggestionsPopupComponent, ThemedConfigurationSearchPageComponent, PageWithSidebarComponent],
imports: [ThemedHomeNewsComponent, NgIf, ViewTrackerComponent, ThemedSearchFormComponent, ThemedTopLevelCommunityListComponent, RecentItemListComponent, AsyncPipe, TranslateModule, NgClass, ConfigurationSearchPageComponent, SuggestionsPopupComponent, ThemedConfigurationSearchPageComponent, PageWithSidebarComponent, HomeCoarComponent],
})
export class HomePageComponent implements OnInit, OnDestroy {
export class HomePageComponent implements OnInit {

site$: Observable<Site>;
isXsOrSm$: Observable<boolean>;
recentSubmissionspageSize: number;
showDiscoverFilters: boolean;
/**
* An array of LinkDefinition objects representing inbox links for the home page.
*/
inboxLinks: LinkDefinition[] = [];

subs: Subscription[] = [];

constructor(
@Inject(APP_CONFIG) protected appConfig: AppConfig,
protected route: ActivatedRoute,
protected responseService: ServerResponseService,
protected notifyInfoService: NotifyInfoService,
protected linkHeadService: LinkHeadService,
protected sidebarService: SidebarService,
protected windowService: HostWindowService,
@Inject(PLATFORM_ID) protected platformId: string,
) {
this.recentSubmissionspageSize = this.appConfig.homePage.recentSubmissions.pageSize;
this.showDiscoverFilters = this.appConfig.homePage.showDiscoverFilters;
Expand All @@ -86,49 +60,6 @@ export class HomePageComponent implements OnInit, OnDestroy {
this.site$ = this.route.data.pipe(
map((data) => data.site as Site),
);
// Get COAR REST API URLs from REST configuration
// only if COAR configuration is enabled
this.subs.push(this.notifyInfoService.isCoarConfigEnabled().pipe(
switchMap((coarLdnEnabled: boolean) => coarLdnEnabled ? this.notifyInfoService.getCoarLdnLocalInboxUrls() : observableOf([])),
).subscribe((coarRestApiUrls: string[]) => {
if (coarRestApiUrls.length > 0) {
this.initPageLinks(coarRestApiUrls);
}
}));
}

/**
* Initializes page links for COAR REST API URLs.
* @param coarRestApiUrls An array of COAR REST API URLs.
*/
private initPageLinks(coarRestApiUrls: string[]): void {
const rel = this.notifyInfoService.getInboxRelationLink();
let links = '';
coarRestApiUrls.forEach((coarRestApiUrl: string) => {
// Add link to head
const tag: LinkDefinition = {
href: coarRestApiUrl,
rel: rel,
};
this.inboxLinks.push(tag);
this.linkHeadService.addTag(tag);

links = links + (isNotEmpty(links) ? ', ' : '') + `<${coarRestApiUrl}> ; rel="${rel}"`;
});

if (isPlatformServer(this.platformId)) {
// Add link to response header
this.responseService.setHeader('Link', links);
}
}

/**
* It removes the inbox links from the head of the html.
*/
ngOnDestroy(): void {
this.subs.forEach((sub: Subscription) => sub.unsubscribe());
this.inboxLinks.forEach((link: LinkDefinition) => {
this.linkHeadService.removeTag(`href='${link.href}'`);
});
}
}
3 changes: 2 additions & 1 deletion src/themes/custom/app/home-page/home-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { Component } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';

import { HomeCoarComponent } from '../../../../app/home-page/home-coar/home-coar.component';
import { ThemedHomeNewsComponent } from '../../../../app/home-page/home-news/themed-home-news.component';
import { HomePageComponent as BaseComponent } from '../../../../app/home-page/home-page.component';
import { RecentItemListComponent } from '../../../../app/home-page/recent-item-list/recent-item-list.component';
Expand All @@ -24,7 +25,7 @@ import { ViewTrackerComponent } from '../../../../app/statistics/angulartics/dsp
// templateUrl: './home-page.component.html'
templateUrl: '../../../../app/home-page/home-page.component.html',
standalone: true,
imports: [ThemedHomeNewsComponent, NgIf, ViewTrackerComponent, ThemedSearchFormComponent, ThemedTopLevelCommunityListComponent, RecentItemListComponent, AsyncPipe, TranslateModule, NgClass, ConfigurationSearchPageComponent, SuggestionsPopupComponent, ThemedConfigurationSearchPageComponent, PageWithSidebarComponent],
imports: [ThemedHomeNewsComponent, NgIf, ViewTrackerComponent, ThemedSearchFormComponent, ThemedTopLevelCommunityListComponent, RecentItemListComponent, AsyncPipe, TranslateModule, NgClass, ConfigurationSearchPageComponent, SuggestionsPopupComponent, ThemedConfigurationSearchPageComponent, PageWithSidebarComponent, HomeCoarComponent],
})
export class HomePageComponent extends BaseComponent {

Expand Down

0 comments on commit fbcacf2

Please sign in to comment.