Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Better support for extending NgxsDataEntityCollectionsRepository #360

Closed
bitflut opened this issue Apr 29, 2020 · 6 comments · Fixed by #368
Closed

Better support for extending NgxsDataEntityCollectionsRepository #360

bitflut opened this issue Apr 29, 2020 · 6 comments · Fixed by #368
Labels
feature ready to release Extra attention is needed
Milestone

Comments

@bitflut
Copy link
Contributor

bitflut commented Apr 29, 2020

We recently switched from ngrx to nxgs with an app that heavily used ngrx/data. Right now we are trying to automate crud operations with our api in a similar way we did with ngrx/data. The route we've chosen is to extend NgxsDataEntityCollectionsRepository to include getAll getOne etc. for now.

Just to share what we are experiencing so far: If we are adding custom fields to the NgxsDataEntityCollectionsRepository reducer, we need to override get snapshot() and getState(). It'd be very nice, if there could be a third generic allowing to specify a type for the NgxsEntityCollections.

@splincode
Copy link
Member

Give examples of expected behavior so that I can implement this

@bitflut
Copy link
Contributor Author

bitflut commented Apr 29, 2020

I will put together a minimal example of what we're trying to achieve later today.

@bitflut
Copy link
Contributor Author

bitflut commented Apr 29, 2020

Let's say we wanted additional properties in our EntityCollectionsReducer like loading and loaded:

export class MyCollectionRepository<V, K extends EntityIdType = EntityIdType>
    extends NgxsDataEntityCollectionsRepository<V, K> {

    @Computed()
    public get loading(): boolean {
        return this.snapshot.loading;
    }

    @Computed()
    public get loaded(): boolean {
        return this.snapshot.loaded;
    }

    @DataAction()
    public setLoading(@Payload('loading') loading: boolean): void {
        const state = this.getState();
        this.setEntitiesState({
            ...state,
            loading,
        });
    }

    @DataAction()
    public setLoaded(@Payload('loaded') loaded: boolean): void {
        const state = this.getState();
        this.setEntitiesState({
            ...state,
            loaded,
        });
    }
}

In order to get this working, there would be a few things we need to adjust. The most important is a new class type generic:

export class NgxsDataEntityCollectionsRepository<
    V,
    K extends number | string = EntityIdType,
    C extends NgxsEntityCollections<V, K> = NgxsEntityCollections<V, K>
>

Then we could simply use this in our Collection:

export class MyCollectionRepository<V, K extends EntityIdType = EntityIdType>
    extends NgxsDataEntityCollectionsRepository<V, K, MyCollection<V, K>>

There might be another caveat... for all the operations used in NgxsDataEntityCollectionsRepository, the rest of the state would need to be persisted. So addEntityOne would use this call to setEntitiesState:

        this.setEntitiesState({
            ...state,
            ids: [...state.ids, id],
            entities: { ...state.entities, [id]: entity },
        });

If this is hard to follow along, I can make an example repository with the overrides in place.

Edit:
Or would you rather suggest we implement something from scratch for this? Possibly NgxsDataEntityCollectionsRepository just wasn't supposed to support this case.

@splincode
Copy link
Member

I understand you, yes, I think no problem will add this soon

@splincode splincode added this to the v3.0.0 milestone Apr 29, 2020
@splincode splincode added the ready to release Extra attention is needed label Apr 30, 2020
@splincode
Copy link
Member

### Extending NgxsDataEntityCollectionsRepository

move to Entity.md

@splincode
Copy link
Member

released in v3.0.0-rc.14

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature ready to release Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant