add notification store series

This commit is contained in:
snoop 2018-03-12 18:45:52 +09:00
parent 5a3099f3ca
commit 696b26a663
15 changed files with 279 additions and 2 deletions

View File

@ -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;

View File

@ -0,0 +1 @@
export * from './Notification';

View File

@ -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 { }

View File

@ -0,0 +1,3 @@
export const MODULE = {
name: 'Notification'
};

View File

@ -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 { }

View File

@ -0,0 +1,5 @@
import { NotificationService } from './Notification.service';
export const SERVICES = [
NotificationService,
];

View File

@ -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();
}));
});

View File

@ -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);
}
}

View File

@ -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
));

View File

@ -0,0 +1,4 @@
export * from './readallbymember.action';
export * from './readallbymember.effect';
export * from './readallbymember.reducer';
export * from './readallbymember.state';

View File

@ -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
;

View File

@ -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();
}));
});

View File

@ -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)))
);
}

View File

@ -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;
}
}
}

View File

@ -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,
};