Skip to content

Commit 048f26b

Browse files
Simone-Ramundialisaismailati
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-1932 (pull request DSpace#2279)
Task/dspace cris 2023 02 x/DSC-1932 Approved-by: Alisa Ismailati
2 parents f93eb16 + 7a82a86 commit 048f26b

6 files changed

Lines changed: 121 additions & 26 deletions

File tree

src/app/shared/entity-dropdown/entity-dropdown.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
{{'submission.sections.general.no-entity' | translate}}
1414
</button>
1515
</li>
16-
<li *ngFor="let listItem of searchListEntity" class="entity-item text-primary">
16+
<li *ngFor="let listItem of searchListEntity | dsSort: 'translatedLabel'" class="entity-item text-primary">
1717
<button class="dropdown-item"
1818
role="menuitem"
19-
title="{{ listItem.label }}"
19+
title="{{ listItem.translatedLabel }}"
2020
(click)="onSelect(listItem)">
21-
<span class="text-truncate font-weight-bold">{{ listItem.label.toLowerCase() + '.listelement.badge' | translate }}</span>
21+
<span class="text-truncate font-weight-bold">{{ listItem.translatedLabel }}</span>
2222
</button>
2323
</li>
2424
<li *ngIf="(isLoadingList | async)">

src/app/shared/entity-dropdown/entity-dropdown.component.spec.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@ import { EntityDropdownComponent } from './entity-dropdown.component';
33
import { getTestScheduler } from 'jasmine-marbles';
44
import { createSuccessfulRemoteDataObject$ } from '../remote-data.utils';
55
import { ItemType } from '../../core/shared/item-relationships/item-type.model';
6-
import { ChangeDetectorRef, NO_ERRORS_SCHEMA, Pipe, PipeTransform } from '@angular/core';
6+
import { ChangeDetectorRef, NO_ERRORS_SCHEMA } from '@angular/core';
77
import { EntityTypeDataService } from '../../core/data/entity-type-data.service';
88
import { TestScheduler } from 'rxjs/testing';
99
import { By } from '@angular/platform-browser';
1010
import { createPaginatedList } from '../testing/utils.test';
1111
import { ItemExportFormatService } from '../../core/itemexportformat/item-export-format.service';
1212
import { of } from 'rxjs/internal/observable/of';
1313
import { ItemExportFormat, ItemExportFormatMap } from '../../core/itemexportformat/model/item-export-format.model';
14-
15-
// eslint-disable-next-line @angular-eslint/pipe-prefix
16-
@Pipe({ name: 'translate' })
17-
class MockTranslatePipe implements PipeTransform {
18-
transform(value: string): string {
19-
return value;
20-
}
21-
}
14+
import { TranslateService } from '@ngx-translate/core';
15+
import { SortPipe } from '../utils/sort.pipe';
2216

2317
const entities: ItemType[] = [
2418
Object.assign(new ItemType(), {
@@ -91,18 +85,23 @@ describe('EntityDropdownComponent', () => {
9185
byEntityTypeAndMolteplicity: jasmine.createSpy('byEntityTypeAndMolteplicity')
9286
});
9387

94-
let translatePipeSpy: jasmine.Spy;
88+
const translateServiceMock: any = {
89+
instant(name) {
90+
return 'Statistics';
91+
}
92+
};
9593

9694
const paginatedEntities = createPaginatedList(entities);
9795
const paginatedEntitiesRD$ = createSuccessfulRemoteDataObject$(paginatedEntities);
9896

9997
beforeEach(waitForAsync(() => {
10098
TestBed.configureTestingModule({
10199
imports: [],
102-
declarations: [EntityDropdownComponent, MockTranslatePipe],
100+
declarations: [EntityDropdownComponent, SortPipe],
103101
providers: [
104102
{ provide: EntityTypeDataService, useValue: entityTypeServiceMock },
105103
{ provide: ItemExportFormatService, useValue: itemExportFormatServiceMock },
104+
{ provide: TranslateService, useValue: translateServiceMock },
106105
ChangeDetectorRef
107106
],
108107
schemas: [NO_ERRORS_SCHEMA]
@@ -119,15 +118,6 @@ describe('EntityDropdownComponent', () => {
119118
componentAsAny.entityTypeService.getAllAuthorizedRelationshipTypeImport.and.returnValue(paginatedEntitiesRD$);
120119
componentAsAny.itemExportFormatService.byEntityTypeAndMolteplicity.and.returnValue(of(entityFormatList));
121120
component.isSubmission = true;
122-
123-
translatePipeSpy = spyOn(MockTranslatePipe.prototype, 'transform');
124-
});
125-
126-
it('should translate entries', () => {
127-
scheduler.schedule(() => fixture.detectChanges());
128-
scheduler.flush();
129-
130-
expect(translatePipeSpy).toHaveBeenCalledWith('entity_1.listelement.badge');
131121
});
132122

133123
it('should init component with entities list', () => {

src/app/shared/entity-dropdown/entity-dropdown.component.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from '../../core/itemexportformat/item-export-format.service';
2424
import { createSuccessfulRemoteDataObject } from '../remote-data.utils';
2525
import { FindListOptions } from '../../core/data/find-list-options.model';
26+
import { TranslateService } from '@ngx-translate/core';
2627

2728
@Component({
2829
selector: 'ds-entity-dropdown',
@@ -91,12 +92,14 @@ export class EntityDropdownComponent implements OnInit, OnDestroy {
9192
* @param {EntityTypeDataService} entityTypeService
9293
* @param {ItemExportFormatService} itemExportFormatService
9394
* @param {ElementRef} el
95+
* @param {TranslateService} translate
9496
*/
9597
constructor(
9698
private changeDetectorRef: ChangeDetectorRef,
9799
private entityTypeService: EntityTypeDataService,
98100
private itemExportFormatService: ItemExportFormatService,
99-
private el: ElementRef
101+
private el: ElementRef,
102+
private translate: TranslateService
100103
) { }
101104

102105
/**
@@ -194,12 +197,21 @@ export class EntityDropdownComponent implements OnInit, OnDestroy {
194197
}
195198
this.searchListEntity$ = searchListEntity$.pipe(
196199
switchMap((entityType: RemoteData<PaginatedList<ItemType>>) => entityType.payload.page),
200+
map((item: ItemType) => {
201+
return {
202+
...item,
203+
translatedLabel: this.translate.instant(`${item.label?.toLowerCase()}.listelement.badge`)
204+
};
205+
}
206+
),
197207
reduce((acc: any, value: any) => [...acc, value], []),
198208
startWith([])
199209
);
200210
this.subs.push(
201211
this.searchListEntity$.subscribe({
202-
next: (result: ItemType[]) => { this.searchListEntity.push(...result); },
212+
next: (result: ItemType[]) => {
213+
this.searchListEntity = [...this.searchListEntity, ...result];
214+
},
203215
complete: () => { this.hideShowLoader(false); this.changeDetectorRef.detectChanges(); }
204216
})
205217
);

src/app/shared/shared.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ import { MetadataLinkViewAvatarPopoverComponent } from './metadata-link-view/met
359359
import { MetadataLinkViewOrcidComponent } from './metadata-link-view/metadata-link-view-orcid/metadata-link-view-orcid.component';
360360
import { SwitchComponent } from './switch/switch.component';
361361
import {StickyPopoverDirective} from './metadata-link-view/sticky-popover.directive';
362+
import { SortPipe } from './utils/sort.pipe';
362363

363364
const MODULES = [
364365
CommonModule,
@@ -405,7 +406,8 @@ const PIPES = [
405406
ConsolePipe,
406407
ObjNgFor,
407408
BrowserOnlyPipe,
408-
ShortNumberPipe
409+
ShortNumberPipe,
410+
SortPipe,
409411
];
410412

411413
const COMPONENTS = [
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { SortPipe } from './sort.pipe';
2+
3+
describe('SortPipe', () => {
4+
let pipe: SortPipe;
5+
6+
beforeEach(() => {
7+
pipe = new SortPipe();
8+
});
9+
10+
it('should return the original value if value is null or undefined', () => {
11+
expect(pipe.transform(null)).toBeNull();
12+
expect(pipe.transform(undefined)).toBeUndefined();
13+
});
14+
15+
it('should return the original value if no order is invalid', () => {
16+
const inputArray = [3, 1, 2];
17+
expect(pipe.transform(inputArray, '', null)).toEqual(inputArray);
18+
});
19+
20+
it('should sort the array in ascending order if no column is provided', () => {
21+
const inputArray = [3, 1, 2];
22+
const sortedArray = [1, 2, 3];
23+
expect(pipe.transform(inputArray, '')).toEqual(sortedArray);
24+
});
25+
26+
it('should sort the array in descending order if no column is provided and order is desc', () => {
27+
const inputArray = [3, 1, 2];
28+
const sortedArray = [3, 2, 1];
29+
expect(pipe.transform(inputArray, '', 'desc')).toEqual(sortedArray);
30+
});
31+
32+
it('should return the original array if it contains one or fewer elements', () => {
33+
const inputArray = [1];
34+
expect(pipe.transform(inputArray)).toEqual(inputArray);
35+
expect(pipe.transform([])).toEqual([]);
36+
});
37+
38+
it('should sort the array by a specific column in ascending order', () => {
39+
const inputArray = [
40+
{ label: 'banana' },
41+
{ label: 'apple' },
42+
{ label: 'cherry' }
43+
];
44+
const sortedArray = [
45+
{ label: 'apple' },
46+
{ label: 'banana' },
47+
{ label: 'cherry' }
48+
];
49+
expect(pipe.transform(inputArray, 'label')).toEqual(sortedArray);
50+
});
51+
52+
it('should sort the array by a specific column in descending order', () => {
53+
const inputArray = [
54+
{ label: 'banana' },
55+
{ label: 'apple' },
56+
{ label: 'cherry' }
57+
];
58+
const sortedArray = [
59+
{ label: 'cherry' },
60+
{ label: 'banana' },
61+
{ label: 'apple' }
62+
];
63+
expect(pipe.transform(inputArray, 'label', 'desc')).toEqual(sortedArray);
64+
});
65+
});

src/app/shared/utils/sort.pipe.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
import orderBy from 'lodash/orderBy';
3+
4+
@Pipe({
5+
name: 'dsSort'
6+
})
7+
export class SortPipe implements PipeTransform {
8+
9+
transform(value: any[], column: string = '', order: 'asc' | 'desc' = 'asc'): any[] {
10+
if (!value || !order) {
11+
return value;
12+
}
13+
if (!column || column === '') {
14+
if (order === 'asc') {
15+
return value.sort();
16+
} else {
17+
return value.sort().reverse();
18+
}
19+
}
20+
if (value.length <= 1) {
21+
return value;
22+
}
23+
24+
return orderBy(value, [column], [order]);
25+
}
26+
}

0 commit comments

Comments
 (0)