add notification store series
This commit is contained in:
		
							parent
							
								
									5a3099f3ca
								
							
						
					
					
						commit
						696b26a663
					
				| @ -1,7 +1,7 @@ | |||||||
| import { Member } from 'packages/member/model'; | import { Member } from 'packages/member/model'; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| interface Notification { | export interface Notification { | ||||||
|     id?: number; |     id?: number; | ||||||
|     createDate?: Date; |     createDate?: Date; | ||||||
|     title?: string; |     title?: string; | ||||||
| @ -11,4 +11,4 @@ interface Notification { | |||||||
|     url?: string; |     url?: string; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export default Notification; | 
 | ||||||
|  | |||||||
							
								
								
									
										1
									
								
								src/packages/notification/model/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/packages/notification/model/index.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | |||||||
|  | export * from './Notification'; | ||||||
| @ -0,0 +1,24 @@ | |||||||
|  | import { NgModule } from '@angular/core'; | ||||||
|  | import { StoreModule } from '@ngrx/store'; | ||||||
|  | import { StoreDevtoolsModule } from '@ngrx/store-devtools'; | ||||||
|  | import { | ||||||
|  |   StoreRouterConnectingModule, | ||||||
|  |   RouterStateSerializer, | ||||||
|  | } from '@ngrx/router-store'; | ||||||
|  | import { EffectsModule } from '@ngrx/effects'; | ||||||
|  | import { combineReducers, ActionReducer, ActionReducerMap, MetaReducer } from '@ngrx/store'; | ||||||
|  | 
 | ||||||
|  | import { | ||||||
|  |   REDUCERS, | ||||||
|  |   EFFECTS, | ||||||
|  | } from './store'; | ||||||
|  | 
 | ||||||
|  | import { MODULE } from './notification.constant'; | ||||||
|  | 
 | ||||||
|  | @NgModule({ | ||||||
|  |   imports: [ | ||||||
|  |     StoreModule.forFeature(MODULE.name, REDUCERS), | ||||||
|  |     EffectsModule.forFeature(EFFECTS), | ||||||
|  |   ], | ||||||
|  | }) | ||||||
|  | export class NotificationStoreModule { } | ||||||
| @ -0,0 +1,3 @@ | |||||||
|  | export const MODULE = { | ||||||
|  |     name: 'Notification' | ||||||
|  |   }; | ||||||
| @ -7,12 +7,16 @@ import { | |||||||
| import { MaterialModule } from 'app/commons/ui/material/material.module'; | import { MaterialModule } from 'app/commons/ui/material/material.module'; | ||||||
| 
 | 
 | ||||||
| import { COMPONENTS } from './component'; | import { COMPONENTS } from './component'; | ||||||
|  | import { SERVICES } from './service'; | ||||||
|  | 
 | ||||||
|  | import { NotificationStoreModule } from './notification-store.module'; | ||||||
| 
 | 
 | ||||||
| @NgModule({ | @NgModule({ | ||||||
|   imports: [ |   imports: [ | ||||||
|     CommonModule, |     CommonModule, | ||||||
|     PerfectScrollbarModule, |     PerfectScrollbarModule, | ||||||
|     MaterialModule, |     MaterialModule, | ||||||
|  |     NotificationStoreModule | ||||||
|   ], |   ], | ||||||
|   declarations: [ |   declarations: [ | ||||||
|     COMPONENTS, |     COMPONENTS, | ||||||
| @ -20,5 +24,8 @@ import { COMPONENTS } from './component'; | |||||||
|   exports: [ |   exports: [ | ||||||
|     COMPONENTS, |     COMPONENTS, | ||||||
|   ], |   ], | ||||||
|  |   providers: [ | ||||||
|  |     SERVICES, | ||||||
|  |   ], | ||||||
| }) | }) | ||||||
| export class NotificationModule { } | export class NotificationModule { } | ||||||
|  | |||||||
| @ -0,0 +1,5 @@ | |||||||
|  | import { NotificationService } from './Notification.service'; | ||||||
|  | 
 | ||||||
|  | export const SERVICES = [ | ||||||
|  |     NotificationService, | ||||||
|  | ]; | ||||||
| @ -0,0 +1,15 @@ | |||||||
|  | import { TestBed, inject } from '@angular/core/testing'; | ||||||
|  | 
 | ||||||
|  | import { NotificationService } from './notification.service'; | ||||||
|  | 
 | ||||||
|  | describe('MemberService', () => { | ||||||
|  |   beforeEach(() => { | ||||||
|  |     TestBed.configureTestingModule({ | ||||||
|  |       providers: [NotificationService] | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   it('should be created', inject([NotificationService], (service: NotificationService) => { | ||||||
|  |     expect(service).toBeTruthy(); | ||||||
|  |   })); | ||||||
|  | }); | ||||||
| @ -0,0 +1,29 @@ | |||||||
|  | import { Injectable } from '@angular/core'; | ||||||
|  | import { Observable } from 'rxjs/Observable'; | ||||||
|  | 
 | ||||||
|  | import 'rxjs/add/operator/map'; | ||||||
|  | 
 | ||||||
|  | import { RPCClient } from 'packages/core/rpc/client/RPCClient'; | ||||||
|  | 
 | ||||||
|  | import { Notification } from '../model'; | ||||||
|  | import { Member } from '../../member/model'; | ||||||
|  | 
 | ||||||
|  | @Injectable() | ||||||
|  | export class NotificationService { | ||||||
|  | 
 | ||||||
|  |   public constructor( | ||||||
|  |     private rpcClient: RPCClient, | ||||||
|  |   ) { | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public readAllByMember(member: Member): Observable<Notification[]> { | ||||||
|  |     const body = { | ||||||
|  |         member: member, | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     return this.rpcClient.call('NotificationService.readAllByMember', member); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,30 @@ | |||||||
|  | import { | ||||||
|  |     createSelector, | ||||||
|  |     createFeatureSelector, | ||||||
|  |     ActionReducerMap, | ||||||
|  |   } from '@ngrx/store'; | ||||||
|  | 
 | ||||||
|  |   import { StateSelector } from 'packages/commons/util/ngrx/store'; | ||||||
|  | 
 | ||||||
|  |   import { MODULE } from '../notification.constant'; | ||||||
|  | 
 | ||||||
|  |   import * as ReadAllByMemberStore from './readallbymember'; | ||||||
|  | 
 | ||||||
|  |   export interface State { | ||||||
|  |     readallbymember: ReadAllByMemberStore.State; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   export const REDUCERS = { | ||||||
|  |     readallbymember: ReadAllByMemberStore.reducer, | ||||||
|  |   }; | ||||||
|  | 
 | ||||||
|  |   export const EFFECTS = [ | ||||||
|  |     ReadAllByMemberStore.Effects, | ||||||
|  |   ]; | ||||||
|  | 
 | ||||||
|  |   export const selectNotificationState = createFeatureSelector<State>(MODULE.name); | ||||||
|  | 
 | ||||||
|  |   export const ReadAllByMemberSelector = new StateSelector<ReadAllByMemberStore.State>(createSelector( | ||||||
|  |     selectNotificationState, | ||||||
|  |     (state: State) => state.readallbymember | ||||||
|  |   )); | ||||||
| @ -0,0 +1,4 @@ | |||||||
|  | export * from './readallbymember.action'; | ||||||
|  | export * from './readallbymember.effect'; | ||||||
|  | export * from './readallbymember.reducer'; | ||||||
|  | export * from './readallbymember.state'; | ||||||
| @ -0,0 +1,36 @@ | |||||||
|  | import { Action } from '@ngrx/store'; | ||||||
|  | 
 | ||||||
|  | import { RPCError } from 'packages/core/rpc/error'; | ||||||
|  | 
 | ||||||
|  | import { Notification } from '../../model'; | ||||||
|  | import { Member } from '../../../member/model'; | ||||||
|  | 
 | ||||||
|  | export enum ActionType { | ||||||
|  |   ReadAllByMember = '[Notification.ReadAllByMember] ReadAllByMember', | ||||||
|  |   ReadAllByMemberSuccess = '[Notification.ReadAllByMemberSuccess] ReadAllByMemberSuccess', | ||||||
|  |   ReadAllByMemberFailure = '[Notification.ReadAllByMemberFailure] ReadAllByMemberFailure', | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export class ReadAllByMember implements Action { | ||||||
|  |   readonly type = ActionType.ReadAllByMember; | ||||||
|  | 
 | ||||||
|  |   constructor(public payload: Member) {} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export class ReadAllByMemberSuccess implements Action { | ||||||
|  |   readonly type = ActionType.ReadAllByMemberSuccess; | ||||||
|  | 
 | ||||||
|  |   constructor(public payload: Notification[]) {} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export class ReadAllByMemberFailure implements Action { | ||||||
|  |   readonly type = ActionType.ReadAllByMemberFailure; | ||||||
|  | 
 | ||||||
|  |   constructor(public payload: RPCError) {} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export type Actions = | ||||||
|  |   | ReadAllByMember | ||||||
|  |   | ReadAllByMemberSuccess | ||||||
|  |   | ReadAllByMemberFailure | ||||||
|  | ; | ||||||
| @ -0,0 +1,15 @@ | |||||||
|  | import { TestBed, inject } from '@angular/core/testing'; | ||||||
|  | 
 | ||||||
|  | import { Effects } from './readallbymember.effect'; | ||||||
|  | 
 | ||||||
|  | describe('ReadAllByMember.Effects', () => { | ||||||
|  |   beforeEach(() => { | ||||||
|  |     TestBed.configureTestingModule({ | ||||||
|  |       providers: [Effects] | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   it('should be created', inject([Effects], (effects: Effects) => { | ||||||
|  |     expect(effects).toBeTruthy(); | ||||||
|  |   })); | ||||||
|  | }); | ||||||
| @ -0,0 +1,48 @@ | |||||||
|  | import { Injectable } from '@angular/core'; | ||||||
|  | import { Router } from '@angular/router'; | ||||||
|  | 
 | ||||||
|  | import { Effect, Actions, ofType } from '@ngrx/effects'; | ||||||
|  | import { Action } from '@ngrx/store'; | ||||||
|  | 
 | ||||||
|  | import { Observable } from 'rxjs/Observable'; | ||||||
|  | import { of } from 'rxjs/observable/of'; | ||||||
|  | 
 | ||||||
|  | import 'rxjs/add/operator/catch'; | ||||||
|  | import 'rxjs/add/operator/do'; | ||||||
|  | import 'rxjs/add/operator/exhaustMap'; | ||||||
|  | import 'rxjs/add/operator/map'; | ||||||
|  | import 'rxjs/add/operator/take'; | ||||||
|  | 
 | ||||||
|  | import { RPCError } from 'packages/core/rpc/error'; | ||||||
|  | 
 | ||||||
|  | import { Notification } from '../../model'; | ||||||
|  | import { NotificationService } from '../../service/notification.service'; | ||||||
|  | 
 | ||||||
|  | import { | ||||||
|  |   ReadAllByMember, | ||||||
|  |   ReadAllByMemberSuccess, | ||||||
|  |   ReadAllByMemberFailure, | ||||||
|  |   ActionType, | ||||||
|  | } from './readallbymember.action'; | ||||||
|  | 
 | ||||||
|  | @Injectable() | ||||||
|  | export class Effects { | ||||||
|  | 
 | ||||||
|  |   constructor( | ||||||
|  |     private actions$: Actions, | ||||||
|  |     private notificationService: NotificationService, | ||||||
|  |     private router: Router | ||||||
|  |   ) { } | ||||||
|  | 
 | ||||||
|  |   @Effect() | ||||||
|  |   readAllByMember$: Observable<Action> = this.actions$ | ||||||
|  |     .ofType(ActionType.ReadAllByMember) | ||||||
|  |     .map((action: ReadAllByMember) => action.payload) | ||||||
|  |     .exhaustMap(member => | ||||||
|  |       this.notificationService | ||||||
|  |       .readAllByMember(member) | ||||||
|  |         .map(notificationList => new ReadAllByMemberSuccess(notificationList)) | ||||||
|  |         .catch(error => of(new ReadAllByMemberFailure(error))) | ||||||
|  |     ); | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,45 @@ | |||||||
|  | import { | ||||||
|  |     Actions, | ||||||
|  |     ActionType, | ||||||
|  |   } from './readallbymember.action'; | ||||||
|  | 
 | ||||||
|  |   import { | ||||||
|  |     State, | ||||||
|  |     initialState, | ||||||
|  |   } from './readallbymember.state'; | ||||||
|  | 
 | ||||||
|  |   import { Notification } from '../../model'; | ||||||
|  | 
 | ||||||
|  |   export function reducer(state = initialState, action: Actions): State { | ||||||
|  |     switch (action.type) { | ||||||
|  |       case ActionType.ReadAllByMember: { | ||||||
|  |         return { | ||||||
|  |           ...state, | ||||||
|  |           error: null, | ||||||
|  |           pending: true, | ||||||
|  |         }; | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       case ActionType.ReadAllByMemberSuccess: { | ||||||
|  |         return { | ||||||
|  |           ...state, | ||||||
|  |           error: null, | ||||||
|  |           pending: false, | ||||||
|  |           notificationList: action.payload, | ||||||
|  |         }; | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       case ActionType.ReadAllByMemberFailure: { | ||||||
|  |         return { | ||||||
|  |           ...state, | ||||||
|  |           error: action.payload, | ||||||
|  |           pending: false, | ||||||
|  |           notificationList: null, | ||||||
|  |         }; | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       default: { | ||||||
|  |         return state; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
| @ -0,0 +1,15 @@ | |||||||
|  | import { RPCError } from 'packages/core/rpc/error'; | ||||||
|  | 
 | ||||||
|  | import { Notification } from '../../model'; | ||||||
|  | 
 | ||||||
|  | export interface State { | ||||||
|  |   error: RPCError | null; | ||||||
|  |   pending: boolean; | ||||||
|  |   notificationList: Notification[] | null; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export const initialState: State = { | ||||||
|  |   error: null, | ||||||
|  |   pending: false, | ||||||
|  |   notificationList: null, | ||||||
|  | }; | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user