Enhance return search store with new methods and hooks for entity management.

-  **Feature**: Added method to remove entities by process ID
-  **Feature**: Implemented onInit hook to clear entities based on process IDs
- 🛠️ **Refactor**: Updated imports for clarity and organization
- 🛠️ **Refactor**: Improved entity management logic in return search store
This commit is contained in:
Lorenz Hilpert
2025-04-07 19:34:18 +02:00
parent bd1e4f36e1
commit 62d0783e88
2 changed files with 26 additions and 4 deletions

View File

@@ -1,15 +1,22 @@
import { patchState, signalStore, type, withMethods } from '@ngrx/signals'; import { patchState, signalStore, type, withHooks, withMethods } from '@ngrx/signals';
import { rxMethod } from '@ngrx/signals/rxjs-interop'; import { rxMethod } from '@ngrx/signals/rxjs-interop';
import { addEntity, entityConfig, updateEntity, withEntities } from '@ngrx/signals/entities'; import {
addEntity,
entityConfig,
setAllEntities,
updateEntity,
withEntities,
} from '@ngrx/signals/entities';
import { pipe, switchMap, tap } from 'rxjs'; import { pipe, switchMap, tap } from 'rxjs';
import { ReturnSearchService } from './return-search.service'; import { ReturnSearchService } from './return-search.service';
import { tapResponse } from '@ngrx/operators'; import { tapResponse } from '@ngrx/operators';
import { inject } from '@angular/core'; import { effect, inject } from '@angular/core';
import { QueryTokenSchema } from './schemas'; import { QueryTokenSchema } from './schemas';
import { Callback, ListResponseArgs } from '@isa/common/result'; import { Callback, ListResponseArgs } from '@isa/common/result';
import { ReceiptListItem } from './models'; import { ReceiptListItem } from './models';
import { Query } from '@isa/shared/filter'; import { Query } from '@isa/shared/filter';
import { SessionStorageProvider, withStorage } from '@isa/core/storage'; import { SessionStorageProvider, withStorage } from '@isa/core/storage';
import { ProcessService } from '@isa/core/process';
/** /**
* Enum representing the status of a return search process. * Enum representing the status of a return search process.
@@ -54,6 +61,10 @@ export const ReturnSearchStore = signalStore(
getEntity(processId: number): ReturnSearchEntity | undefined { getEntity(processId: number): ReturnSearchEntity | undefined {
return store.entities().find((e) => e.processId === processId); return store.entities().find((e) => e.processId === processId);
}, },
removeAllEntitiesByProcessId(processId: number) {
const entities = store.entities().filter((entity) => entity.processId !== processId);
patchState(store, setAllEntities(entities, config));
},
})), })),
withMethods((store) => ({ withMethods((store) => ({
/** /**
@@ -197,4 +208,15 @@ export const ReturnSearchStore = signalStore(
), ),
), ),
})), })),
withHooks((store, processService = inject(ProcessService)) => ({
onInit() {
effect(() => {
const processIds = processService.ids();
const entities = store.entities().find((entity) => !processIds.includes(entity.processId));
if (entities) {
store.removeAllEntitiesByProcessId(entities.processId);
}
});
},
})),
); );

View File

@@ -68,7 +68,7 @@ export function injectRestoreScrollPosition(): () => Promise<void> {
if (position) { if (position) {
// wait for the next tick to ensure the DOM is ready // wait for the next tick to ensure the DOM is ready
await new Promise((r) => setTimeout(r, delay)); await new Promise((r) => setTimeout(r, delay));
sessionStorage.clear(url);
viewportScroller.scrollToPosition(position as [number, number]); viewportScroller.scrollToPosition(position as [number, number]);
} }
}; };