From 696b26a66395cb75d6dbe95cb40520bdb2a05a62 Mon Sep 17 00:00:00 2001 From: snoop Date: Mon, 12 Mar 2018 18:45:52 +0900 Subject: [PATCH] add notification store series --- .../notification/model/Notification.ts | 4 +- src/packages/notification/model/index.ts | 1 + .../notification/notification-store.module.ts | 24 ++++++++++ .../notification/notification.constant.ts | 3 ++ .../notification/notification.module.ts | 7 +++ src/packages/notification/service/index.ts | 5 ++ .../service/notification.service.spec.ts | 15 ++++++ .../service/notification.service.ts | 29 +++++++++++ src/packages/notification/store/index.ts | 30 ++++++++++++ .../store/readallbymember/index.ts | 4 ++ .../readallbymember/readallbymember.action.ts | 36 ++++++++++++++ .../readallbymember.effect.spec.ts | 15 ++++++ .../readallbymember/readallbymember.effect.ts | 48 +++++++++++++++++++ .../readallbymember.reducer.ts | 45 +++++++++++++++++ .../readallbymember/readallbymember.state.ts | 15 ++++++ 15 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 src/packages/notification/model/index.ts diff --git a/src/packages/notification/model/Notification.ts b/src/packages/notification/model/Notification.ts index 148bb2c..f02bdd2 100644 --- a/src/packages/notification/model/Notification.ts +++ b/src/packages/notification/model/Notification.ts @@ -1,7 +1,7 @@ import { Member } from 'packages/member/model'; -interface Notification { +export interface Notification { id?: number; createDate?: Date; title?: string; @@ -11,4 +11,4 @@ interface Notification { url?: string; } -export default Notification; + diff --git a/src/packages/notification/model/index.ts b/src/packages/notification/model/index.ts new file mode 100644 index 0000000..ed80171 --- /dev/null +++ b/src/packages/notification/model/index.ts @@ -0,0 +1 @@ +export * from './Notification'; diff --git a/src/packages/notification/notification-store.module.ts b/src/packages/notification/notification-store.module.ts index e69de29..93cc2d4 100644 --- a/src/packages/notification/notification-store.module.ts +++ b/src/packages/notification/notification-store.module.ts @@ -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 { } diff --git a/src/packages/notification/notification.constant.ts b/src/packages/notification/notification.constant.ts index e69de29..f524425 100644 --- a/src/packages/notification/notification.constant.ts +++ b/src/packages/notification/notification.constant.ts @@ -0,0 +1,3 @@ +export const MODULE = { + name: 'Notification' + }; diff --git a/src/packages/notification/notification.module.ts b/src/packages/notification/notification.module.ts index 6185f3a..870417a 100644 --- a/src/packages/notification/notification.module.ts +++ b/src/packages/notification/notification.module.ts @@ -7,12 +7,16 @@ import { import { MaterialModule } from 'app/commons/ui/material/material.module'; import { COMPONENTS } from './component'; +import { SERVICES } from './service'; + +import { NotificationStoreModule } from './notification-store.module'; @NgModule({ imports: [ CommonModule, PerfectScrollbarModule, MaterialModule, + NotificationStoreModule ], declarations: [ COMPONENTS, @@ -20,5 +24,8 @@ import { COMPONENTS } from './component'; exports: [ COMPONENTS, ], + providers: [ + SERVICES, + ], }) export class NotificationModule { } diff --git a/src/packages/notification/service/index.ts b/src/packages/notification/service/index.ts index e69de29..9a5e124 100644 --- a/src/packages/notification/service/index.ts +++ b/src/packages/notification/service/index.ts @@ -0,0 +1,5 @@ +import { NotificationService } from './Notification.service'; + +export const SERVICES = [ + NotificationService, +]; diff --git a/src/packages/notification/service/notification.service.spec.ts b/src/packages/notification/service/notification.service.spec.ts index e69de29..872e93f 100644 --- a/src/packages/notification/service/notification.service.spec.ts +++ b/src/packages/notification/service/notification.service.spec.ts @@ -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(); + })); +}); diff --git a/src/packages/notification/service/notification.service.ts b/src/packages/notification/service/notification.service.ts index e69de29..f8adf75 100644 --- a/src/packages/notification/service/notification.service.ts +++ b/src/packages/notification/service/notification.service.ts @@ -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 { + const body = { + member: member, + }; + + return this.rpcClient.call('NotificationService.readAllByMember', member); + } + + +} diff --git a/src/packages/notification/store/index.ts b/src/packages/notification/store/index.ts index e69de29..d0971c6 100644 --- a/src/packages/notification/store/index.ts +++ b/src/packages/notification/store/index.ts @@ -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(MODULE.name); + + export const ReadAllByMemberSelector = new StateSelector(createSelector( + selectNotificationState, + (state: State) => state.readallbymember + )); diff --git a/src/packages/notification/store/readallbymember/index.ts b/src/packages/notification/store/readallbymember/index.ts index e69de29..ca4ece9 100644 --- a/src/packages/notification/store/readallbymember/index.ts +++ b/src/packages/notification/store/readallbymember/index.ts @@ -0,0 +1,4 @@ +export * from './readallbymember.action'; +export * from './readallbymember.effect'; +export * from './readallbymember.reducer'; +export * from './readallbymember.state'; diff --git a/src/packages/notification/store/readallbymember/readallbymember.action.ts b/src/packages/notification/store/readallbymember/readallbymember.action.ts index e69de29..40e312b 100644 --- a/src/packages/notification/store/readallbymember/readallbymember.action.ts +++ b/src/packages/notification/store/readallbymember/readallbymember.action.ts @@ -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 +; diff --git a/src/packages/notification/store/readallbymember/readallbymember.effect.spec.ts b/src/packages/notification/store/readallbymember/readallbymember.effect.spec.ts index e69de29..2ace0e6 100644 --- a/src/packages/notification/store/readallbymember/readallbymember.effect.spec.ts +++ b/src/packages/notification/store/readallbymember/readallbymember.effect.spec.ts @@ -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(); + })); +}); diff --git a/src/packages/notification/store/readallbymember/readallbymember.effect.ts b/src/packages/notification/store/readallbymember/readallbymember.effect.ts index e69de29..6a7208d 100644 --- a/src/packages/notification/store/readallbymember/readallbymember.effect.ts +++ b/src/packages/notification/store/readallbymember/readallbymember.effect.ts @@ -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 = 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))) + ); + +} diff --git a/src/packages/notification/store/readallbymember/readallbymember.reducer.ts b/src/packages/notification/store/readallbymember/readallbymember.reducer.ts index e69de29..81a76ab 100644 --- a/src/packages/notification/store/readallbymember/readallbymember.reducer.ts +++ b/src/packages/notification/store/readallbymember/readallbymember.reducer.ts @@ -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; + } + } + } diff --git a/src/packages/notification/store/readallbymember/readallbymember.state.ts b/src/packages/notification/store/readallbymember/readallbymember.state.ts index e69de29..0e9d097 100644 --- a/src/packages/notification/store/readallbymember/readallbymember.state.ts +++ b/src/packages/notification/store/readallbymember/readallbymember.state.ts @@ -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, +};