-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #669 from geonetwork/ME-persist-records-selection
[libs, ME]: persist records selection
- Loading branch information
Showing
8 changed files
with
255 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
libs/api/repository/src/lib/gn4/selection/selection.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { SelectionsApiService } from '@geonetwork-ui/data-access/gn4' | ||
import { SelectionService } from './selection.service' | ||
import { firstValueFrom, of } from 'rxjs' | ||
import { CatalogRecord } from '@geonetwork-ui/common/domain/record' | ||
|
||
function record(uuid: string): CatalogRecord { | ||
return { | ||
uniqueIdentifier: uuid, | ||
} as CatalogRecord | ||
} | ||
|
||
class SelectionsServiceMock { | ||
private selected = ['001', '002', '003'] | ||
add = jest.fn((bucket, ids) => { | ||
this.selected.push(...ids) | ||
return of(undefined) | ||
}) | ||
clear = jest.fn((bucket, ids) => { | ||
this.selected = this.selected.filter( | ||
(id) => !!ids && ids.indexOf(id) === -1 | ||
) | ||
return of(undefined) | ||
}) | ||
get = jest.fn(() => of(this.selected)) | ||
} | ||
|
||
describe('SelectionService', () => { | ||
let service: SelectionService | ||
let selectionsService: SelectionsApiService | ||
|
||
beforeEach(async () => { | ||
selectionsService = new SelectionsServiceMock() as any | ||
service = new SelectionService(selectionsService) | ||
}) | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy() | ||
}) | ||
|
||
describe('#selectRecords', () => { | ||
let selectedRecords | ||
beforeEach(async () => { | ||
service.selectedRecordsIdentifiers$.subscribe((value) => { | ||
selectedRecords = value | ||
}) | ||
await firstValueFrom( | ||
service.selectRecords([record('abcd'), record('efgh'), record('001')]) | ||
) | ||
}) | ||
it('calls the corresponding API', () => { | ||
expect(selectionsService.add).toHaveBeenCalledWith('gnui', [ | ||
'abcd', | ||
'efgh', | ||
'001', | ||
]) | ||
}) | ||
it('emits new records in selectedRecordsIdentifiers$', () => { | ||
expect(selectedRecords).toEqual(['001', '002', '003', 'abcd', 'efgh']) | ||
}) | ||
}) | ||
|
||
describe('#deselectRecords', () => { | ||
let selectedRecords | ||
beforeEach(async () => { | ||
service.selectedRecordsIdentifiers$.subscribe((value) => { | ||
selectedRecords = value | ||
}) | ||
await firstValueFrom( | ||
service.deselectRecords([record('abcd'), record('efgh'), record('001')]) | ||
) | ||
}) | ||
it('calls the corresponding API', () => { | ||
expect(selectionsService.clear).toHaveBeenCalledWith('gnui', [ | ||
'abcd', | ||
'efgh', | ||
'001', | ||
]) | ||
}) | ||
it('emits new records in selectedRecordsIdentifiers$', () => { | ||
expect(selectedRecords).toEqual(['002', '003']) | ||
}) | ||
}) | ||
|
||
describe('#clearSelection', () => { | ||
let selectedRecords | ||
beforeEach(async () => { | ||
service.selectedRecordsIdentifiers$.subscribe((value) => { | ||
selectedRecords = value | ||
}) | ||
await firstValueFrom(service.clearSelection()) | ||
}) | ||
it('calls the corresponding API', () => { | ||
expect(selectionsService.get).toHaveBeenCalledWith('gnui') | ||
|
||
expect(selectionsService.clear).toHaveBeenCalledWith('gnui', [ | ||
'001', | ||
'002', | ||
'003', | ||
]) | ||
}) | ||
it('emits new records in selectedRecordsIdentifiers$', () => { | ||
expect(selectedRecords).toEqual([]) | ||
}) | ||
}) | ||
}) |
80 changes: 80 additions & 0 deletions
80
libs/api/repository/src/lib/gn4/selection/selection.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Injectable } from '@angular/core' | ||
import { CatalogRecord } from '@geonetwork-ui/common/domain/record' | ||
import { SelectionsApiService } from '@geonetwork-ui/data-access/gn4' | ||
import { BehaviorSubject, Observable, Subscription, map, tap } from 'rxjs' | ||
|
||
const BUCKET_ID = 'gnui' | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class SelectionService { | ||
selectedRecordsIdentifiers$: BehaviorSubject<string[]> = new BehaviorSubject( | ||
[] | ||
) | ||
subscription: Subscription | ||
|
||
constructor(private selectionsApi: SelectionsApiService) { | ||
this.selectionsApi.get(BUCKET_ID).subscribe((selectedIds) => { | ||
this.addIdsToSelected(Array.from(selectedIds)) | ||
}) | ||
} | ||
|
||
private addIdsToSelected(ids: string[]) { | ||
const currentIds = this.selectedRecordsIdentifiers$.value | ||
const uniqueSet = new Set([...currentIds, ...ids]) | ||
this.selectedRecordsIdentifiers$.next([...uniqueSet]) | ||
} | ||
|
||
private removeIdsFromSelected(ids: string[]) { | ||
const filtered = this.selectedRecordsIdentifiers$.value.filter( | ||
(value) => !ids.includes(value) | ||
) | ||
this.selectedRecordsIdentifiers$.next(filtered) | ||
} | ||
|
||
selectRecords(records: CatalogRecord[]): Observable<void> { | ||
const newIds = [] | ||
records.map((record) => { | ||
newIds.push(record.uniqueIdentifier) | ||
}) | ||
const apiResponse = this.selectionsApi.add(BUCKET_ID, newIds) | ||
return apiResponse.pipe( | ||
tap(() => { | ||
this.addIdsToSelected(newIds) | ||
}), | ||
map(() => undefined) | ||
) | ||
} | ||
|
||
deselectRecords(records: CatalogRecord[]): Observable<void> { | ||
const idsToBeRemoved = [] | ||
records.map((record) => { | ||
idsToBeRemoved.push(record.uniqueIdentifier) | ||
}) | ||
const apiResponse = this.selectionsApi.clear(BUCKET_ID, idsToBeRemoved) | ||
return apiResponse.pipe( | ||
tap(() => { | ||
this.removeIdsFromSelected(idsToBeRemoved) | ||
}), | ||
map(() => undefined) | ||
) | ||
} | ||
|
||
clearSelection(): Observable<void> { | ||
const currentSelectedResponse = this.selectionsApi.get(BUCKET_ID) | ||
let currentSelection | ||
this.subscription = currentSelectedResponse.subscribe((value) => { | ||
currentSelection = [...value] | ||
}) | ||
this.selectionsApi.clear(BUCKET_ID, currentSelection) | ||
const apiResponse = this.selectionsApi.clear(BUCKET_ID, currentSelection) | ||
|
||
return apiResponse.pipe( | ||
tap(() => { | ||
this.removeIdsFromSelected(currentSelection) | ||
}), | ||
map(() => undefined) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters