Skip to content

Commit

Permalink
docs(local-traits): fix missing local-traits docs
Browse files Browse the repository at this point in the history
Added the need to override the constructor and all this.traits.addEffects(this) if effects are added
to the LocalTraits service
  • Loading branch information
Gabriel Guerrero authored and gabrielguerrero committed Mar 31, 2022
1 parent d22d504 commit a7e4bf2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { map } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { Product, ProductFilter } from '../../../models';
import { Sort } from 'ngrx-traits/traits';
import { ProductsLocalTraits } from './products.traits';
import { ProductsLocalTraits } from './products.local-traits';

@Component({
selector: 'product-select-dialog',
Expand Down Expand Up @@ -47,9 +47,9 @@ import { ProductsLocalTraits } from './products.traits';
})
export class ProductSelectDialogComponent implements OnInit {
data$ = combineLatest([
this.store.select(this.localTraits.localSelectors.selectProductsList),
this.store.select(this.localTraits.localSelectors.isProductsLoading),
this.store.select(this.localTraits.localSelectors.selectProductSelected),
this.store.select(this.traits.localSelectors.selectProductsList),
this.store.select(this.traits.localSelectors.isProductsLoading),
this.store.select(this.traits.localSelectors.selectProductSelected),
]).pipe(
map(([products, isLoading, selectedProduct]) => ({
products,
Expand All @@ -58,22 +58,20 @@ export class ProductSelectDialogComponent implements OnInit {
}))
);

constructor(private store: Store, private localTraits: ProductsLocalTraits) {}
constructor(private store: Store, private traits: ProductsLocalTraits) {}

ngOnInit() {
this.store.dispatch(this.localTraits.localActions.loadProducts());
this.store.dispatch(this.traits.localActions.loadProducts());
}

select({ id }: Product) {
this.store.dispatch(this.localTraits.localActions.selectProduct({ id }));
this.store.dispatch(this.traits.localActions.selectProduct({ id }));
}

filter(filters: ProductFilter) {
this.store.dispatch(
this.localTraits.localActions.filterProducts({ filters })
);
this.store.dispatch(this.traits.localActions.filterProducts({ filters }));
}
sort(sort: Sort<Product>) {
this.store.dispatch(this.localTraits.localActions.sortProducts(sort));
this.store.dispatch(this.traits.localActions.sortProducts(sort));
}
}
20 changes: 10 additions & 10 deletions libs/ngrx-traits/src/lib/local-store/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ import { ProductService } from './product.service';

@Injectable()
export class ProductsLocalTraits extends TraitsLocalStore<typeof productFeatureFactory> {
// TraitsLocalStore adds a reference to the injector so to get a service reference you can
productService = this.injector.get(ProductService);
//Alternativally you could override the constructor to get a reference to your service
// constructor(
// injector: Injector,
// private productService: ProductService
// ) {
// super(injector);
// }

loadProducts$ = createEffect(() =>
this.actions$.pipe(
ofType(this.localActions.loadProducts),
Expand All @@ -69,6 +61,14 @@ export class ProductsLocalTraits extends TraitsLocalStore<typeof productFeatureF
)
);

constructor(
injector: Injector,
private productService: ProductService
) {
super(injector);
// IMPORTANT, if next line is not added the effects in this class dont get resgitered
this.traits.addEffects(this);
}
setup(): LocalTraitsConfig<typeof productFeature> {
return {
componentName: 'ProductsPickerComponent',
Expand All @@ -79,7 +79,7 @@ export class ProductsLocalTraits extends TraitsLocalStore<typeof productFeatureF
```

An important bit is `extends TraitLocalEffectsFactory<typeof traitsFactory>`
the _typeof traitsFactory_ gives the types for the localActions and localSelectors properties in the class.
the _typeof traitsFactory_ gives the types for the localActions and localSelectors properties in the class. Also you must override the constructor, one to inject either the service you want to call, and to add ` this.traits.addEffects(this)` that registers the effects in the current class otherwise the wont run

You can also add custom actions, selectors, reducers, and effects to your LocalTrait by creating a [Custom Traits](../../../traits/src/custom-traits.md), for this is just an extra effect we need and this should help with most of the cases.

Expand Down

0 comments on commit a7e4bf2

Please sign in to comment.