Is there a way to watch the result of a rxMethod call in an angular 18 application with ngrx?

Discussão em 'Angular' iniciado por Darion Badlydone, Outubro 31, 2024.

  1. On my project with Angular 18 and ngrx I'm trying manage entities with @ngrx/signals and signalStore.

    Suppose that I'm handling the creation of a new entity within the WithMethods method and I'need to know, in the parent component, when the request is complete to change the route, show a tost, etc.

    I've done it by passing in the rxMethod the entity to create and a callback function, but I think there should be a propper way to do it:

    export const ToolsStore = signalStore(
    { providedIn: 'root' },
    withEntities(entityConfig),
    withMethods((store, entityService = inject(entityService)) => ({
    createEntity: rxMethod<{ entity: EntityDto; callback: (entity: EntityDto) => void }>(
    pipe(
    debounceTime(300),
    filter((model) => !store.entityIds().includes(model.entity.id!)),
    switchMap((model) => {
    return entityService.createEntity(model.entity).pipe(
    tapResponse({
    next: (entity) => {
    patchState(store, addEntity(entity, entityConfig));
    model.callback(entity);
    toast.info('Creating completed!');
    },
    error: console.error,
    }),
    );
    }),
    ),
    ),
    })),
    );


    In the component:

    this.toolsStore.createEntity({
    entity: entity,
    callback: (a) => {
    this.router.navigate(['tools', 'entitys', a.id]);
    this.createEntity.set(false);
    this.entityId.set(a.id || '');
    },
    });


    I saw the watchStore method but I would need more information to understand which entity have been created.

    Continue reading...

Compartilhe esta Página