I am using NgRx component store in the angular application as below
export interface ProductCategoryState {
categories: CategoryModel[];
category: CategoryModel;
}
@Injectable()
export class ProductCategoryStore extends ComponentStore<ProductCategoryState> {
constructor(private iGenericHttpClient: IGenericHttpClient<any>, private globalFacade: GlobalFacade) {
super(initialState);
}
readonly getCategories$: Observable<CategoryModel[]> = this.select((state) => state.categories);
private readonly setCategories = this.updater((state, categories: CategoryModel[]) => ({
...state,
categories: categories
}));
readonly delete = this.effect((id$: Observable<string>) => {
return id$.pipe(
withLatestFrom(this.getCategories$),
switchMap(([id, categories]) => {
return this.iGenericHttpClient.delete(`/category/${id}`).pipe(
tapResponse(
() => {
const index = categories.findIndex((x) => x.id == id);
categories.splice(index, 1);
this.setCategories(categories);
}
)
);
})
);
});
}
On the delete operation, find the index and deleted the record, and update the state, however, the update is not detected on Subscribe in one of the components as shown below.
@Component({
.......,
providers: [ProductCategoryStore]
})
export class ProductCategoryComponent implements OnInit {
constructor(private productCategoryStore: ProductCategoryStore) {}
ngOnInit(): void {
this.productCategoryStore.getCategories$.subscribe((dataSource) => {
console.log(dataSource);
});
}
}