export interface Item { id: number;
name: string;
description: string;
};
export interface AppStore {
items: Item[];
selectedItem: Item;
};
export const selectedItem = (state: any = null, {type, payload}) => {
switch (type) {
case 'SELECT_ITEM':
return payload;
default:
return state;
}
}
import {App} from './src/app';
import {provideStore} from '@ngrx/store';
import {items} from './src/common/stores/items.store';
import {selectedItem} from './src/common/stores/selectedItem.store';
bootstrap(App, [
provideStore({items, selectedItem})
]);
// items component
this.selectedItem = store.select('selectedItem')
// items template
<item-detail
(saved)="saveItem($event)"
(cancelled)="resetItem($event)"
[item]="selectedItem | async">Select an Item</item-detail>
selectItem(item: Item) {
this.store.dispatch({type: 'SELECT_ITEM', payload: item})
}