import { signalStore, withState, withMethods, patchState, type, withProps, withComputed,…
- User
import { signalStore, withState, withMethods, patchState, type, withProps, withComputed, } from '@ngrx/signals'; import { entityConfig, removeEntity, setAllEntities, setEntity, withEntities, } from '@ngrx/signals/entities'; import { inject } from '@angular/core'; import { finalize } from 'rxjs'; import { ToastService } from '../services/toast.service'; import { FlightChecklistService } from '../services/flight-checklist.service'; import { TaskGroup } from '../models/task-group.model';
type ChecklistState = { isLoading: boolean; };
const initialState: ChecklistState = { isLoading: false, };
const checklistEntityConfig = entityConfig({ entity: type(), collection: 'checklist', selectId: (checklist) => checklist.id, });
export const ChecklistStore = signalStore( { providedIn: 'root' }, withState(initialState), withEntities(checklistEntityConfig), withComputed((store) => ({})), withProps(() => ({ _checklistService: inject(FlightChecklistService), _toastService: inject(ToastService), })), withMethods((store) => ({ getChecklists: () => { patchState(store, { isLoading: true });
store._checklistService .getAllChecklists() .pipe( finalize(() => { patchState(store, { isLoading: false }); }), ) .subscribe({ next: (items) => { patchState(store, setAllEntities(items, checklistEntityConfig)); }, error: (err) => store._toastService.error(err.error.detail), }); }, addChecklist: (name: string) => { store._checklistService.createChecklist(name).subscribe({ next: (item) => { patchState(store, setEntity(item, checklistEntityConfig)); }, error: (err) => store._toastService.error(err.error.detail), }); }, deleteChecklist: (checklist: TaskGroup) => { store._checklistService.deleteChecklist(checklist).subscribe({ next: (item) => { patchState(store, removeEntity(checklist.id, checklistEntityConfig)); }, error: (err) => store._toastService.error(err.error.detail), }); },})), );
public createChecklist(name: string): Observable { const url = this.configService.config?.oneuiDOPBaseUrl + "/api/v1/taskgroups";
const payload = { name }; return this.httpClient.post<TaskGroup>(url, payload);}
addChecklist is setting latest item before post call have 13 records after post call store is updated with just 1 object but checklistEntities() have 14 objects
Assistant
The issue you're describing suggests that when you call
addChecklist, the store is being updated incorrectly, showing only 1 object in the main state while the entities collection actually contains all 14 objects. This is likely happening because of how thesetEntitymethod works in combination with your state management.Here's what's happening and how to fix it:
The Problem:
setEntityis designed to update or add a single entity to the collection while preserving all other entities.- However, if your state structure isn't properly maintaining the entities collection, you might be seeing inconsistent behavior.
Solution:
-
First, let's ensure your state structure is correct. The
withEntitiesfeature should be properly maintaining the entities collection. -
The issue might be in how you're observing the state. When you say "store is updated with just 1 object", you might be looking at the wrong part of the state. The entities are stored in the collection managed by
withEntities. -
Here's how to properly access all entities:
// To get all entities as an array: const allChecklists = store.checklistEntities(); // To get the count: const count = store.checklistEntities().length;- If you're still seeing issues, you might want to modify your
addChecklistmethod to ensure it's working with the entities collection correctly:
addChecklist: (name: string) => { store._checklistService.createChecklist(name).subscribe({ next: (newItem) => { // This will add the new item while preserving all existing ones patchState(store, setEntity(newItem, checklistEntityConfig)); // Alternatively, you could do this to be more explicit: // const currentEntities = store.checklistEntities(); // patchState(store, setAllEntities([...currentEntities, newItem], checklistEntityConfig)); }, error: (err) => store._toastService.error(err.error.detail), }); },- If you're still having issues, you might want to add some debugging to see what's actually in your store:
withComputed((store) => ({ debugInfo: { entityCount: store.checklistEntities().length, allEntities: store.checklistEntities(), // Add other debug info as needed } }))Additional Recommendations:
- Make sure you're not accidentally overwriting your state elsewhere in your application.
- Verify that your
checklistEntityConfigis properly configured and consistent throughout your application. - Consider adding more comprehensive error handling and logging to help diagnose the issue.
The behavior you're describing suggests that the entities collection is working correctly (since it shows 14 objects), but something in your state observation or display logic might be showing only the most recent item. Double-check how you're displaying or using the store data in your components.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.