[ACS-8779] Keep selections and question after going to the previous page (#4147)

This commit is contained in:
jacekpluta
2024-10-04 14:54:09 +02:00
committed by GitHub
parent 64dfc48468
commit 3ec6d0be32
26 changed files with 457 additions and 123 deletions

View File

@@ -30,7 +30,7 @@ import {
ShareDataRow,
UploadService
} from '@alfresco/adf-content-services';
import { ShowHeaderMode } from '@alfresco/adf-core';
import { ShowHeaderMode, UserPreferencesService } from '@alfresco/adf-core';
import { ContentActionRef, DocumentListPresetRef, SelectionState } from '@alfresco/adf-extensions';
import { OnDestroy, OnInit, OnChanges, ViewChild, SimpleChanges, Directive, inject, HostListener } from '@angular/core';
import { Store } from '@ngrx/store';
@@ -53,6 +53,7 @@ import { AutoDownloadService } from '../../services/auto-download.service';
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { Router } from '@angular/router';
import { AppSettingsService } from '../../services/app-settings.service';
import { NavigationHistoryService } from '../../services/navigation-history.service';
/* eslint-disable @angular-eslint/directive-class-suffix */
@Directive()
@@ -77,6 +78,7 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges {
createActions: ContentActionRef[] = [];
isSmallScreen = false;
selectedRowItemsCount = 0;
selectedNodesState: SelectionState;
protected documentListService = inject(DocumentListService);
protected settings = inject(AppSettingsService);
@@ -86,9 +88,11 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges {
protected breakpointObserver = inject(BreakpointObserver);
protected uploadService = inject(UploadService);
protected router = inject(Router);
protected userPreferencesService = inject(UserPreferencesService);
protected searchAiService = inject(SearchAiService);
private autoDownloadService = inject(AutoDownloadService, { optional: true });
private navigationHistoryService = inject(NavigationHistoryService);
protected searchAiService: SearchAiService = inject(SearchAiService);
protected subscriptions: Subscription[] = [];
private _searchAiInputState: SearchAiInputState = {
@@ -155,6 +159,8 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges {
this.searchAiService.toggleSearchAiInput$
.pipe(takeUntil(this.onDestroy$))
.subscribe((searchAiInputState) => (this._searchAiInputState = searchAiInputState));
this.setKnowledgeRetrievalState();
}
ngOnChanges(changes: SimpleChanges) {
@@ -233,6 +239,19 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges {
return obj.id;
}
private setKnowledgeRetrievalState() {
const nodes = this.userPreferencesService.get('knowledgeRetrievalNodes');
if (nodes && this.navigationHistoryService.shouldReturnLastSelection('/knowledge-retrieval')) {
this.selectedNodesState = JSON.parse(nodes);
}
if (!this.selectedNodesState && !this.router.url.startsWith('/knowledge-retrieval')) {
this.searchAiService.updateSearchAiInputState({
active: false
});
}
}
private isOutletPreviewUrl(): boolean {
return location.href.includes('viewer:view');
}

View File

@@ -30,11 +30,13 @@ import { NodeEntry, NodePaging } from '@alfresco/js-api';
import { DocumentBasePageService } from './document-base-page.service';
import { Store } from '@ngrx/store';
import { Component } from '@angular/core';
import { DiscoveryApiService, DocumentListComponent, DocumentListService } from '@alfresco/adf-content-services';
import { DiscoveryApiService, DocumentListComponent, DocumentListService, SearchAiService } from '@alfresco/adf-content-services';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { AuthModule } from '@alfresco/adf-core';
import { Subscription } from 'rxjs';
import { AuthModule, UserPreferencesService } from '@alfresco/adf-core';
import { of, Subscription } from 'rxjs';
import { MatDialogModule } from '@angular/material/dialog';
import { NavigationHistoryService } from '../../services/navigation-history.service';
import { Router } from '@angular/router';
@Component({
selector: 'aca-test',
@@ -53,23 +55,49 @@ class TestComponent extends PageComponent {
}
describe('PageComponent', () => {
const mockNodes = JSON.stringify({ node: 'mockNode' });
let component: TestComponent;
let store: Store<AppState>;
let fixture: ComponentFixture<TestComponent>;
let documentListService: DocumentListService;
let userPreferencesService: jasmine.SpyObj<UserPreferencesService>;
let navigationHistoryService: { shouldReturnLastSelection: jasmine.Spy };
let searchAiService: SearchAiService;
let router: { url: string };
beforeEach(() => {
userPreferencesService = jasmine.createSpyObj('UserPreferencesService', ['get', 'set']);
navigationHistoryService = jasmine.createSpyObj('NavigationHistoryService', ['shouldReturnLastSelection']);
router = { url: '/some-url' };
searchAiService = jasmine.createSpyObj('SearchAiService', ['updateSearchAiInputState', 'toggleSearchAiInput$']);
searchAiService.toggleSearchAiInput$ = of({ active: false });
TestBed.configureTestingModule({
imports: [LibTestingModule, AuthModule.forRoot(), MatDialogModule],
declarations: [TestComponent],
providers: [
{ provide: DocumentBasePageService, useClass: DocumentBasePageServiceMock },
{ provide: DiscoveryApiService, useValue: discoveryApiServiceMockValue },
AppExtensionService
AppExtensionService,
{
provide: UserPreferencesService,
useValue: userPreferencesService
},
{
provide: NavigationHistoryService,
useValue: navigationHistoryService
},
{
provide: Router,
useValue: router
},
{ provide: SearchAiService, useValue: searchAiService }
]
});
store = TestBed.inject(Store);
searchAiService = TestBed.inject(SearchAiService);
documentListService = TestBed.inject(DocumentListService);
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
@@ -191,6 +219,65 @@ describe('PageComponent', () => {
expect(store.dispatch).toHaveBeenCalledWith(new ViewNodeAction(id));
});
});
describe('setKnowledgeRetrievalState()', () => {
it('should set selectedNodesState when nodes exist and last selection is valid', () => {
userPreferencesService.get.and.returnValue(mockNodes);
navigationHistoryService.shouldReturnLastSelection.and.returnValue(true);
component.ngOnInit();
expect(component.selectedNodesState).toEqual(JSON.parse(mockNodes));
});
it('should not set selectedNodesState when nodes do not exist', () => {
userPreferencesService.get.and.returnValue(null);
navigationHistoryService.shouldReturnLastSelection.and.returnValue(true);
component.ngOnInit();
expect(component.selectedNodesState).toBeUndefined();
});
it('should not set selectedNodesState when shouldReturnLastSelection returns false', () => {
userPreferencesService.get.and.returnValue(mockNodes);
navigationHistoryService.shouldReturnLastSelection.and.returnValue(false);
component.ngOnInit();
expect(component.selectedNodesState).toBeUndefined();
});
it('should update searchAiInputState when selectedNodesState is undefined and url does not start with /knowledge-retrieval', () => {
userPreferencesService.get.and.returnValue(mockNodes);
navigationHistoryService.shouldReturnLastSelection.and.returnValue(false);
router.url = '/some-other-url';
component.ngOnInit();
expect(searchAiService.updateSearchAiInputState).toHaveBeenCalledWith({ active: false });
});
it('should not update searchAiInputState when url starts with /knowledge-retrieval', () => {
userPreferencesService.get.and.returnValue(undefined);
navigationHistoryService.shouldReturnLastSelection.and.returnValue(true);
router.url = '/knowledge-retrieval';
component.ngOnInit();
expect(searchAiService.updateSearchAiInputState).not.toHaveBeenCalled();
});
it('should not update searchAiInputState when selectedNodesState in not null', () => {
userPreferencesService.get.and.returnValue(mockNodes);
navigationHistoryService.shouldReturnLastSelection.and.returnValue(true);
router.url = '/other';
component.ngOnInit();
expect(searchAiService.updateSearchAiInputState).not.toHaveBeenCalled();
});
});
});
describe('Info Drawer state', () => {

View File

@@ -0,0 +1,122 @@
/*!
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Alfresco Example Content Application
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
*/
import { NavigationEnd, NavigationStart, Router } from '@angular/router';
import { TestBed } from '@angular/core/testing';
import { NavigationHistoryService } from './navigation-history.service';
import { Subject } from 'rxjs';
describe('NavigationHistoryService', () => {
let service: NavigationHistoryService;
let routerEvents$: Subject<NavigationEnd | NavigationStart>;
const triggerNavigationEnd = (id: number, url: string) => {
routerEvents$.next(new NavigationEnd(id, url, url));
};
beforeEach(() => {
routerEvents$ = new Subject();
TestBed.configureTestingModule({
providers: [NavigationHistoryService, { provide: Router, useValue: { events: routerEvents$.asObservable(), url: '/initial' } }]
});
service = TestBed.inject(NavigationHistoryService);
TestBed.inject(Router);
});
it('should store route changes in history', () => {
service.listenToRouteChanges().subscribe((event) => service.setHistory(event, 3));
triggerNavigationEnd(1, '/page1');
triggerNavigationEnd(2, '/page2');
expect(service.history).toEqual(['/initial', '/page1', '/page2']);
});
it('should not exceed the max history length', () => {
service.listenToRouteChanges().subscribe((event) => service.setHistory(event, 6));
triggerNavigationEnd(1, '/page1');
triggerNavigationEnd(2, '/page2');
triggerNavigationEnd(3, '/page3');
triggerNavigationEnd(4, '/page4');
triggerNavigationEnd(5, '/page5');
triggerNavigationEnd(6, '/page6');
expect(service.history).toEqual(['/page1', '/page2', '/page3', '/page4', '/page5', '/page6']);
});
it('should store different route changes in history', () => {
service.listenToRouteChanges().subscribe((event) => service.setHistory(event, 4));
triggerNavigationEnd(1, '/page1');
triggerNavigationEnd(2, '/page2');
triggerNavigationEnd(3, '/page1');
triggerNavigationEnd(4, '/page2');
triggerNavigationEnd(5, '/page4');
triggerNavigationEnd(6, '/page2');
triggerNavigationEnd(7, '/page1');
triggerNavigationEnd(8, '/page2');
expect(service.history).toEqual(['/page4', '/page2', '/page1', '/page2']);
});
it('should return true for a valid last selection', () => {
service.history = ['/page1', '/page2', '/page1'];
expect(service.shouldReturnLastSelection('/page2')).toBeTrue();
});
it('should return false for an invalid last selection', () => {
service.history = ['/page1', '/page3', '/page1'];
expect(service.shouldReturnLastSelection('/page2')).toBeFalse();
});
it('should initialize history with the current route', () => {
service.listenToRouteChanges().subscribe((event) => service.setHistory(event, 3));
expect(service.history).toEqual(['/initial']);
});
it('should only store NavigationEnd events in history', () => {
service.listenToRouteChanges().subscribe((event) => service.setHistory(event, 3));
routerEvents$.next(new NavigationStart(1, '/page-start'));
routerEvents$.next(new NavigationEnd(1, '/page1', '/page1'));
expect(service.history).toEqual(['/initial', '/page1']);
});
it('should return false if second-to-last URL does not match in shouldReturnLastSelection', () => {
service.history = ['/page1', '/page2', '/page3'];
expect(service.shouldReturnLastSelection('/page1')).toBeFalse();
});
it('should return false if first and third URL are not equal', () => {
service.history = ['/page1', '/page2', '/page3'];
expect(service.shouldReturnLastSelection('/page2')).toBeFalse();
});
it('should return false if the current URL does not match the last two URLs', () => {
service.history = ['/page1', '/page4', '/page1'];
expect(service.shouldReturnLastSelection('/page3')).toBeFalse();
});
});

View File

@@ -0,0 +1,53 @@
/*!
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Alfresco Example Content Application
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
*/
import { Injectable } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { filter, startWith } from 'rxjs/operators';
import { Observable } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class NavigationHistoryService {
history: string[] = [];
constructor(private router: Router) {}
listenToRouteChanges(): Observable<NavigationEnd> {
return this.router.events.pipe(
startWith(new NavigationEnd(0, this.router.url, this.router.url)),
filter((event: NavigationEnd) => event instanceof NavigationEnd)
);
}
shouldReturnLastSelection(url: string): boolean {
return this.history.length > 2 && this.history[1].startsWith(url) && this.history[0] === this.history[2];
}
setHistory(event: NavigationEnd, maxHistoryLength: number) {
this.history.push(event.urlAfterRedirects);
if (maxHistoryLength > 0 && this.history.length > maxHistoryLength) {
this.history.shift();
}
}
}

View File

@@ -58,6 +58,7 @@ export * from './lib/services/app-hook.service';
export * from './lib/services/auto-download.service';
export * from './lib/services/app-settings.service';
export * from './lib/services/user-profile.service';
export * from './lib/services/navigation-history.service';
export * from './lib/utils/node.utils';
export * from './lib/testing/lib-testing-module';