mirror of
https://github.com/fergalmoran/ngrx-demo.git
synced 2025-12-25 11:07:55 +00:00
Rudimentary selectors
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
import { Action } from '@ngrx/store';
|
import { Action } from '@ngrx/store';
|
||||||
|
|
||||||
export const LOAD = '[Podcasts] Load';
|
export const LOAD = '[Podcasts] Load';
|
||||||
export const LOAD_SUCCESS = '[Podcasts] Load Success';
|
export const LOAD_SUCCESS = '[Podcasts] Load Success';
|
||||||
export const LOAD_FAIL = '[Podcasts] Load Fail';
|
export const LOAD_FAIL = '[Podcasts] Load Fail';
|
||||||
|
export const SELECT_ITEM = '[Podcasts] Select Item';
|
||||||
|
|
||||||
export class LoadAction implements Action {
|
export class LoadAction implements Action {
|
||||||
readonly type = LOAD;
|
readonly type = LOAD;
|
||||||
@@ -20,7 +21,13 @@ export class LoadFailAction implements Action {
|
|||||||
constructor(public payload: any) { }
|
constructor(public payload: any) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class SelectItemAction implements Action {
|
||||||
|
readonly type = SELECT_ITEM;
|
||||||
|
constructor(public payload: any) { }
|
||||||
|
}
|
||||||
|
|
||||||
export type Actions =
|
export type Actions =
|
||||||
| LoadAction
|
| LoadAction
|
||||||
| LoadSuccessAction
|
| LoadSuccessAction
|
||||||
| LoadFailAction;
|
| LoadFailAction
|
||||||
|
| SelectItemAction;
|
||||||
|
|||||||
@@ -1,10 +1,17 @@
|
|||||||
<!--The content below is only a placeholder and can be replaced.-->
|
<!--The content below is only a placeholder and can be replaced.-->
|
||||||
<div style="text-align:center">
|
<div style="text-align:center">
|
||||||
<h1>
|
<h1>
|
||||||
Hello, {{title}}!
|
Hello, {{title}}!
|
||||||
</h1>
|
</h1>
|
||||||
<div>
|
<div class="col-md-6">
|
||||||
<ul class="list-unstyled">
|
<h1>List of things</h1>
|
||||||
<li *ngFor="let p of podcasts$ | async">{{p.title}}</li>
|
<ul class="list-unstyled">
|
||||||
</ul>
|
<li *ngFor="let p of podcasts$ | async" (click)="onSelect(p)">
|
||||||
|
{{p.title}}
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-6">
|
||||||
|
<h1>Details are here</h1>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { Component } from '@angular/core';
|
|||||||
|
|
||||||
import { Store } from '@ngrx/store';
|
import { Store } from '@ngrx/store';
|
||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
import { LoadAction } from 'app/actions/podcasts.actions';
|
import { LoadAction, SelectItemAction } from 'app/actions/podcasts.actions';
|
||||||
import { State } from 'app/reducers';
|
import { State } from 'app/reducers';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@@ -15,8 +15,13 @@ export class AppComponent {
|
|||||||
podcasts$: Observable<any>;
|
podcasts$: Observable<any>;
|
||||||
|
|
||||||
constructor(private store: Store<State>){
|
constructor(private store: Store<State>){
|
||||||
console.log('AppComponent', 'ctor', store);
|
console.log('AppComponent', 'ctor', store);
|
||||||
this.podcasts$ = this.store.select(p => p.podcasts.result);
|
this.podcasts$ = this.store.select(p => p.podcasts.result);
|
||||||
this.store.dispatch(new LoadAction());
|
this.store.dispatch(new LoadAction());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onSelect(p){
|
||||||
|
console.log(p);
|
||||||
|
this.store.dispatch(new SelectItemAction(p.id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ export interface State {
|
|||||||
loading: boolean;
|
loading: boolean;
|
||||||
entities: { [id: string]: any };
|
entities: { [id: string]: any };
|
||||||
result: string[];
|
result: string[];
|
||||||
|
selected: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const initialState: State = {
|
export const initialState: State = {
|
||||||
loading: false,
|
loading: false,
|
||||||
entities: {},
|
entities: {},
|
||||||
result: []
|
result: [],
|
||||||
|
selected: null
|
||||||
}
|
}
|
||||||
|
|
||||||
export function reducer(state = initialState, action: podcasts.Actions): State {
|
export function reducer(state = initialState, action: podcasts.Actions): State {
|
||||||
@@ -18,6 +20,7 @@ export function reducer(state = initialState, action: podcasts.Actions): State {
|
|||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
result: [],
|
result: [],
|
||||||
|
selected: null,
|
||||||
loading: true
|
loading: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,6 +42,10 @@ export function reducer(state = initialState, action: podcasts.Actions): State {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case podcasts.SELECT_ITEM:
|
||||||
|
console.log('SELECTED ITEM');
|
||||||
|
return state;
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user