1

I want to create single store with multiple query state models. So in particular component or service wherever i want there select particular from store.

is this possible with Akita State Management.

for example:

@Injectable({ providedIn: 'root' })
export class Query1 extends QueryEntity<myState, School> {

  constructor(protected store: myStore) {
    super(store);
  }

}


@Injectable({ providedIn: 'root' })
export class Query2 extends QueryEntity<myState, Employee> {

  constructor(protected store: myStore) {
    super(store);
  }

}

so that in components if i query particular Query1 or Query2 service get corrosponding subscriptions.

SchoolComponent:

var query1 = Quer1.selectAll();

EmployeeComponent:

var query2 = Quer2.selectAll();

Any one suggest me correct approache to achieve same.

cj devin
  • 1,045
  • 3
  • 13
  • 48

1 Answers1

1

You could do var query1 = Quer1.selectAll({filterBy: (entity:any) =>(entity.staffDept === 'SomeDepartment') && (entity.staffId === someID}); , or the more proper way

@Injectable({
  providedIn: 'root'
})
export class DeliveryQuery extends QueryEntity<DeliveryState, Delivery> {
  selectVisibilityFilter$ = this.select(state => state.ui);
  selectVisibleDeliveries$ = combineLatest(
    this.selectVisibilityFilter$,
    this.selectAll(),
    this.getVisibleDelivery,

  );

  constructor(protected store: DeliveryStore) {
    super(store);
  }



  private getVisibleDelivery(filter, delivery): Delivery[] {
    switch (filter.filter) {
      case 'COMPLETED':
        switch (filter.drivername) {
          case 'All':
            return delivery.filter(d => (d.data.CHECKPOINT_CODE === 'DLC'));
          default:
            return delivery.filter(d => (d.data.CHECKPOINT_CODE === 'DLC') && (d.data.MOBILE_USER_ID === filter.drivername));
        }
      case 'FAILED':
        switch (filter.drivername) {
          case 'All':
            return delivery.filter(d => d.data.CHECKPOINT_CODE === 'DLP');
          default:
            return delivery.filter(d => (d.data.CHECKPOINT_CODE === 'DLP') && (d.data.MOBILE_USER_ID === filter.drivername));
        }

      case 'ASSIGNED':
        switch (filter.drivername) {
          case 'All':
            return delivery.filter(d => d.data.CHECKPOINT_CODE === 'ASSIGNED');
          default:
            return delivery.filter(d => (d.data.CHECKPOINT_CODE === 'ASSIGNED') && (d.data.MOBILE_USER_ID === filter.drivername));
        }

    }
  }
}

then you call it like this: this.list = this.deliveryQuery.selectVisibleDeliveries$

I'm not sure if this is the best method but this is what I get from modifying the method in documentation. Hope it could help. I also struggle with akita framework on my own :)

  • Thank you for reply.yes may be this is the one possible solution but i went with basic store...+1 vote. – cj devin Jul 26 '19 at 10:50