Skip to content

Commit 5f027e1

Browse files
authored
Merge pull request #3741 from atmire/w2p-122064_browse-pages-ignore-sort-config-fix-UI-main
browse pages should not ignore sort config from back end
2 parents 4bd2c3f + a9a9e97 commit 5f027e1

File tree

8 files changed

+72
-63
lines changed

8 files changed

+72
-63
lines changed

src/app/browse-by/browse-by-date/browse-by-date.component.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ describe('BrowseByDateComponent', () => {
8989
getBrowseEntriesFor: (options: BrowseEntrySearchOptions) => toRemoteData([]),
9090
getBrowseItemsFor: (value: string, options: BrowseEntrySearchOptions) => toRemoteData([firstItem]),
9191
getFirstItemFor: (definition: string, scope?: string, sortDirection?: SortDirection) => null,
92+
getConfiguredSortDirection: () => of(SortDirection.DESC),
9293
};
9394

9495
const mockDsoService = {

src/app/browse-by/browse-by-date/browse-by-date.component.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
} from '@angular/core';
1212
import {
1313
ActivatedRoute,
14-
Params,
1514
Router,
1615
} from '@angular/router';
1716
import {
@@ -27,7 +26,6 @@ import {
2726
import { DSpaceObjectDataService } from '@dspace/core/data/dspace-object-data.service';
2827
import { RemoteData } from '@dspace/core/data/remote-data';
2928
import { PaginationService } from '@dspace/core/pagination/pagination.service';
30-
import { PaginationComponentOptions } from '@dspace/core/pagination/pagination-component-options.model';
3129
import { Item } from '@dspace/core/shared/item.model';
3230
import { isValidDate } from '@dspace/shared/utils/date.util';
3331
import {
@@ -41,8 +39,8 @@ import {
4139
of,
4240
} from 'rxjs';
4341
import {
44-
distinctUntilChanged,
4542
map,
43+
switchMap,
4644
} from 'rxjs/operators';
4745
import { ThemedBrowseByComponent } from 'src/app/shared/browse-by/themed-browse-by.component';
4846

@@ -96,27 +94,23 @@ export class BrowseByDateComponent extends BrowseByMetadataComponent implements
9694
this.loading$ = of(false);
9795
return;
9896
}
99-
const sortConfig = new SortOptions('default', SortDirection.ASC);
97+
this.browseId = this.route.snapshot.params.id;
10098
this.startsWithType = StartsWithType.date;
101-
this.currentPagination$ = this.paginationService.getCurrentPagination(this.paginationConfig.id, this.paginationConfig);
102-
this.currentSort$ = this.paginationService.getCurrentSort(this.paginationConfig.id, sortConfig);
103-
const routeParams$: Observable<Params> = observableCombineLatest([
104-
this.route.params,
105-
this.route.queryParams,
106-
]).pipe(
107-
map(([params, queryParams]: [Params, Params]) => Object.assign({}, params, queryParams)),
108-
distinctUntilChanged((prev: Params, curr: Params) => prev.id === curr.id && prev.startsWith === curr.startsWith),
109-
);
99+
110100
this.subs.push(
111-
observableCombineLatest([
112-
routeParams$,
113-
this.scope$,
114-
this.currentPagination$,
115-
this.currentSort$,
116-
]).subscribe(([params, scope, currentPage, currentSort]: [Params, string, PaginationComponentOptions, SortOptions]) => {
101+
this.browseService.getConfiguredSortDirection(this.browseId, SortDirection.ASC).pipe(
102+
map((sortDir) => new SortOptions(this.browseId, sortDir)),
103+
switchMap((sortConfig) => {
104+
this.currentPagination$ = this.paginationService.getCurrentPagination(this.paginationConfig.id, this.paginationConfig);
105+
this.currentSort$ = this.paginationService.getCurrentSort(this.paginationConfig.id, sortConfig, false);
106+
return observableCombineLatest([this.route.params, this.route.queryParams, this.scope$, this.route.data, this.currentPagination$, this.currentSort$]).pipe(
107+
map(([routeParams, queryParams, scope, data, currentPage, currentSort]) => ({
108+
params: Object.assign({}, routeParams, queryParams, data), scope, currentPage, currentSort,
109+
})));
110+
})).subscribe(({ params, scope, currentPage, currentSort }) => {
117111
const metadataKeys = params.browseDefinition ? params.browseDefinition.metadataKeys : this.defaultMetadataKeys;
118-
this.browseId = params.id || this.defaultBrowseId;
119112
this.startsWith = +params.startsWith || params.startsWith;
113+
this.browseId = params.id || this.defaultBrowseId;
120114
const searchOptions = browseParamsToOptions(params, scope, currentPage, currentSort, this.browseId, this.fetchThumbnails);
121115
this.updatePageWithItems(searchOptions, this.value, undefined);
122116
this.updateStartsWithOptions(this.browseId, metadataKeys, params.scope);

src/app/browse-by/browse-by-metadata/browse-by-metadata.component.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ describe('BrowseByMetadataComponent', () => {
117117
const mockBrowseService = {
118118
getBrowseEntriesFor: (options: BrowseEntrySearchOptions) => toRemoteData(mockEntries),
119119
getBrowseItemsFor: (value: string, options: BrowseEntrySearchOptions) => toRemoteData(mockItems),
120+
getConfiguredSortDirection: () => of(SortDirection.ASC),
120121
};
121122

122123
const mockDsoService = {

src/app/browse-by/browse-by-metadata/browse-by-metadata.component.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
} from '@angular/core';
1515
import {
1616
ActivatedRoute,
17-
Params,
1817
Router,
1918
} from '@angular/router';
2019
import {
@@ -51,8 +50,8 @@ import {
5150
Subscription,
5251
} from 'rxjs';
5352
import {
54-
distinctUntilChanged,
5553
map,
54+
switchMap,
5655
} from 'rxjs/operators';
5756
import { ThemedBrowseByComponent } from 'src/app/shared/browse-by/themed-browse-by.component';
5857

@@ -215,24 +214,18 @@ export class BrowseByMetadataComponent implements OnInit, OnChanges, OnDestroy {
215214
this.loading$ = of(false);
216215
return;
217216
}
218-
const sortConfig = new SortOptions('default', SortDirection.ASC);
219-
this.currentPagination$ = this.paginationService.getCurrentPagination(this.paginationConfig.id, this.paginationConfig);
220-
this.currentSort$ = this.paginationService.getCurrentSort(this.paginationConfig.id, sortConfig);
221-
const routeParams$: Observable<Params> = observableCombineLatest([
222-
this.route.params,
223-
this.route.queryParams,
224-
]).pipe(
225-
map(([params, queryParams]: [Params, Params]) => Object.assign({}, params, queryParams)),
226-
distinctUntilChanged((prev: Params, curr: Params) => prev.id === curr.id && prev.authority === curr.authority && prev.value === curr.value && prev.startsWith === curr.startsWith),
227-
);
217+
this.browseId = this.route.snapshot.params.id;
228218
this.subs.push(
229-
observableCombineLatest([
230-
routeParams$,
231-
this.scope$,
232-
this.currentPagination$,
233-
this.currentSort$,
234-
]).subscribe(([params, scope, currentPage, currentSort]: [Params, string, PaginationComponentOptions, SortOptions]) => {
235-
this.browseId = params.id || this.defaultBrowseId;
219+
this.browseService.getConfiguredSortDirection(this.browseId, SortDirection.ASC).pipe(
220+
map((sortDir) => new SortOptions(this.browseId, sortDir)),
221+
switchMap((sortConfig) => {
222+
this.currentSort$ = this.paginationService.getCurrentSort(this.paginationConfig.id, sortConfig, false);
223+
this.currentPagination$ = this.paginationService.getCurrentPagination(this.paginationConfig.id, this.paginationConfig);
224+
return observableCombineLatest([this.route.params, this.route.queryParams, this.scope$, this.currentPagination$, this.currentSort$]).pipe(
225+
map(([routeParams, queryParams, scope, currentPage, currentSort]) => ({
226+
params: Object.assign({}, routeParams, queryParams), scope, currentPage, currentSort,
227+
})));
228+
})).subscribe(({ params, scope, currentPage, currentSort }) => {
236229
this.authority = params.authority;
237230

238231
if (typeof params.value === 'string') {
@@ -256,9 +249,8 @@ export class BrowseByMetadataComponent implements OnInit, OnChanges, OnDestroy {
256249
} else {
257250
this.updatePage(browseParamsToOptions(params, scope, currentPage, currentSort, this.browseId, false));
258251
}
252+
this.updateStartsWithTextOptions();
259253
}));
260-
this.updateStartsWithTextOptions();
261-
262254
}
263255

264256
ngOnChanges(changes: SimpleChanges): void {

src/app/browse-by/browse-by-title/browse-by-title.component.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import { RouterTestingModule } from '@angular/router/testing';
1818
import { APP_CONFIG } from '@dspace/config/app-config.interface';
1919
import { BrowseService } from '@dspace/core/browse/browse.service';
20+
import { SortDirection } from '@dspace/core/cache/models/sort-options.model';
2021
import { DSpaceObjectDataService } from '@dspace/core/data/dspace-object-data.service';
2122
import { ItemDataService } from '@dspace/core/data/item-data.service';
2223
import { PaginationService } from '@dspace/core/pagination/pagination.service';
@@ -76,6 +77,7 @@ describe('BrowseByTitleComponent', () => {
7677
const mockBrowseService = {
7778
getBrowseItemsFor: () => toRemoteData(mockItems),
7879
getBrowseEntriesFor: () => toRemoteData([]),
80+
getConfiguredSortDirection: () => of(SortDirection.ASC),
7981
};
8082

8183
const mockDsoService = {

src/app/browse-by/browse-by-title/browse-by-title.component.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@ import {
66
Component,
77
OnInit,
88
} from '@angular/core';
9-
import { Params } from '@angular/router';
109
import {
1110
SortDirection,
1211
SortOptions,
1312
} from '@dspace/core/cache/models/sort-options.model';
14-
import { PaginationComponentOptions } from '@dspace/core/pagination/pagination-component-options.model';
1513
import { TranslateModule } from '@ngx-translate/core';
1614
import {
1715
combineLatest as observableCombineLatest,
18-
Observable,
1916
of,
2017
} from 'rxjs';
2118
import {
22-
distinctUntilChanged,
2319
map,
20+
switchMap,
2421
} from 'rxjs/operators';
2522

2623
import { environment } from '../../../environments/environment';
@@ -52,28 +49,23 @@ export class BrowseByTitleComponent extends BrowseByMetadataComponent implements
5249
this.loading$ = of(false);
5350
return;
5451
}
55-
const sortConfig = new SortOptions('dc.title', SortDirection.ASC);
56-
this.currentPagination$ = this.paginationService.getCurrentPagination(this.paginationConfig.id, this.paginationConfig);
57-
this.currentSort$ = this.paginationService.getCurrentSort(this.paginationConfig.id, sortConfig);
58-
const routeParams$: Observable<Params> = observableCombineLatest([
59-
this.route.params,
60-
this.route.queryParams,
61-
]).pipe(
62-
map(([params, queryParams]: [Params, Params]) => Object.assign({}, params, queryParams)),
63-
distinctUntilChanged((prev: Params, curr: Params) => prev.id === curr.id && prev.startsWith === curr.startsWith),
64-
);
52+
this.browseId = this.route.snapshot.params.id;
6553
this.subs.push(
66-
observableCombineLatest([
67-
routeParams$,
68-
this.scope$,
69-
this.currentPagination$,
70-
this.currentSort$,
71-
]).subscribe(([params, scope, currentPage, currentSort]: [Params, string, PaginationComponentOptions, SortOptions]) => {
54+
this.browseService.getConfiguredSortDirection(this.browseId, SortDirection.ASC).pipe(
55+
map((sortDir) => new SortOptions(this.browseId, sortDir)),
56+
switchMap((sortConfig) => {
57+
this.currentSort$ = this.paginationService.getCurrentSort(this.paginationConfig.id, sortConfig, false);
58+
this.currentPagination$ = this.paginationService.getCurrentPagination(this.paginationConfig.id, this.paginationConfig);
59+
return observableCombineLatest([this.route.params, this.route.queryParams, this.scope$, this.currentPagination$, this.currentSort$]).pipe(
60+
map(([routeParams, queryParams, scope, currentPage, currentSort]) => ({
61+
params: Object.assign({}, routeParams, queryParams), scope, currentPage, currentSort,
62+
})),
63+
);
64+
})).subscribe(({ params, scope, currentPage, currentSort }) => {
7265
this.startsWith = +params.startsWith || params.startsWith;
73-
this.browseId = params.id || this.defaultBrowseId;
7466
this.updatePageWithItems(browseParamsToOptions(params, scope, currentPage, currentSort, this.browseId, this.fetchThumbnails), undefined, undefined);
67+
this.updateStartsWithTextOptions();
7568
}));
76-
this.updateStartsWithTextOptions();
7769
}
7870

7971
}

src/app/core/browse/browse.service.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,29 @@ export class BrowseService {
134134
return this.hrefOnlyDataService.findListByHref<BrowseEntry>(href$);
135135
}
136136

137+
/*
138+
* Get the sort direction for a browse index based on its unique id
139+
* @param browseId The unique id of the browse index
140+
* @param defaultDirection The default sort direction to return if the browse index has no sort direction configured
141+
* @returns {Observable<SortDirection>} The sort direction of the browse index
142+
*/
143+
getConfiguredSortDirection(browseId: string, defaultDirection: SortDirection): Observable<SortDirection> {
144+
return this.getBrowseDefinitions().pipe(
145+
getRemoteDataPayload(),
146+
getPaginatedListPayload(),
147+
map((browseDefinitions: BrowseDefinition[]) => browseDefinitions
148+
.find((def: BrowseDefinition) => def.id === browseId),
149+
),
150+
map((browseDef: BrowseDefinition) => {
151+
if (browseDef.order === SortDirection.ASC || browseDef.order === SortDirection.DESC) {
152+
return browseDef.order;
153+
} else {
154+
return defaultDirection;
155+
}
156+
}),
157+
);
158+
}
159+
137160
/**
138161
* Get all items linked to a certain metadata value
139162
* @param {string} filterValue metadata value to filter by (e.g. author's name)

src/app/core/shared/browse-definition.model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55

66
import { BrowseByDataType } from '../browse/browse-by-data-type';
77
import { CacheableObject } from '../cache/cacheable-object.model';
8+
import { SortDirection } from '../cache/models/sort-options.model';
89

910
/**
1011
* Base class for BrowseDefinition models
@@ -17,6 +18,9 @@ export abstract class BrowseDefinition extends CacheableObject {
1718
@autoserializeAs('metadata')
1819
metadataKeys: string[];
1920

21+
@autoserialize
22+
order: SortDirection;
23+
2024
/**
2125
* Get the render type of the BrowseDefinition model
2226
*/

0 commit comments

Comments
 (0)