This commit is contained in:
insanity 2018-03-13 19:41:08 +09:00
parent 311dfee153
commit 61feaf3d6a
12 changed files with 115 additions and 37 deletions

View File

@ -35,8 +35,8 @@ export class RPCClientJSONCodec extends RPCClientCodec {
const _res: ClientResponse = { const _res: ClientResponse = {
id: res.id, id: res.id,
jsonrpc: res.jsonrpc, jsonrpc: res.jsonrpc,
result: undefined !== res.result ? JSON.parse(res.result) : undefined, result: res.result,
error: undefined !== res.error ? JSON.parse(res.error) : undefined, error: res.error,
}; };
return new RPCClientJSONResponseCodec(_res); return new RPCClientJSONResponseCodec(_res);
} }

View File

@ -1 +1,29 @@
<div>list</div> <div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef mat-sort-header> Name </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.id}} </mat-cell>
</ng-container>
<ng-container matColumnDef="title">
<mat-header-cell *matHeaderCellDef mat-sort-header> Title </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.title}} </mat-cell>
</ng-container>
<ng-container matColumnDef="message">
<mat-header-cell *matHeaderCellDef mat-sort-header> Message </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.message}} </mat-cell>
</ng-container>
<ng-container matColumnDef="member">
<mat-header-cell *matHeaderCellDef mat-sort-header> Member </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.member.name}} </mat-cell>
</ng-container>
<mat-row *matRowDef="let row; columns: displayedColumns;" (click)="handleRowClick(row)"></mat-row>
</mat-table>
<mat-paginator [length]="length" [pageIndex]="0" [pageSize]="pageSize" [pageSizeOptions]="[5, 10, 25, 100]">
</mat-paginator>
</div>

View File

@ -1,18 +1,62 @@
import { Component, OnInit, Input } from '@angular/core'; import { Component, OnInit, Input, ViewChild, AfterContentInit } from '@angular/core';
import { MatTableDataSource, MatSort } from '@angular/material';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { RPCError } from 'packages/core/rpc/error';
import { Notification } from '../../model';
import * as NotificationStore from '../../store/notification';
import { ReadAllByMemberSelector } from '../../store';
import { AuthSelector } from 'packages/member/store';
import { Member } from '../../../member/model';
@Component({ @Component({
selector: 'of-notification', selector: 'of-notification',
templateUrl: './notification.component.html', templateUrl: './notification.component.html',
styleUrls: ['./notification.component.scss'] styleUrls: ['./notification.component.scss']
}) })
export class NotificationComponent implements OnInit { export class NotificationComponent implements OnInit, AfterContentInit {
notification$ = this.store.pipe(select(ReadAllByMemberSelector.select('notifications')));
displayedColumns = ['id', 'title', 'message', 'member' ];
dataSource: MatTableDataSource<Notification>;
@ViewChild(MatSort) sort: MatSort;
constructor( constructor(
private router: Router private router: Router,
private store: Store<NotificationStore.State>
) { } ) { }
ngOnInit() { ngOnInit() {
this.notification$.subscribe(
(notifications: Notification[]) => {
console.log('#########');
console.log(notifications);
console.log('#########');
this.dataSource = new MatTableDataSource(notifications);
this.dataSource.sort = this.sort;
},
(error: RPCError) => {
console.log(error.message);
}
);
}
ngAfterContentInit() {
this.store.select(AuthSelector.select('member')).subscribe(
(member: Member) => {
console.log(member);
this.store.dispatch(new NotificationStore.ReadAllByMember(member));
},
(error) => {
console.log(error);
}
);
}
handleRowClick(n: Notification) {
this.router.navigate([n.url]);
} }
} }

View File

@ -18,9 +18,6 @@ export class NotificationService {
} }
public readAllByMember(member: Member): Observable<Notification[]> { public readAllByMember(member: Member): Observable<Notification[]> {
const body = {
member: member,
};
return this.rpcClient.call('NotificationService.readAllByMember', member); return this.rpcClient.call('NotificationService.readAllByMember', member);
} }

View File

@ -8,23 +8,23 @@ import {
import { MODULE } from '../notification.constant'; import { MODULE } from '../notification.constant';
import * as ReadAllByMemberStore from './readallbymember'; import * as NotificationStore from './notification';
export interface State { export interface State {
readallbymember: ReadAllByMemberStore.State; notifications: NotificationStore.State;
} }
export const REDUCERS = { export const REDUCERS = {
readallbymember: ReadAllByMemberStore.reducer, notifications: NotificationStore.reducer,
}; };
export const EFFECTS = [ export const EFFECTS = [
ReadAllByMemberStore.Effects, NotificationStore.Effects,
]; ];
export const selectNotificationState = createFeatureSelector<State>(MODULE.name); export const selectNotificationState = createFeatureSelector<State>(MODULE.name);
export const ReadAllByMemberSelector = new StateSelector<ReadAllByMemberStore.State>(createSelector( export const ReadAllByMemberSelector = new StateSelector<NotificationStore.State>(createSelector(
selectNotificationState, selectNotificationState,
(state: State) => state.readallbymember (state: State) => state.notifications
)); ));

View File

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

View File

@ -6,9 +6,9 @@ import { Notification } from '../../model';
import { Member } from '../../../member/model'; import { Member } from '../../../member/model';
export enum ActionType { export enum ActionType {
ReadAllByMember = '[Notification.ReadAllByMember] ReadAllByMember', ReadAllByMember = '[Notification.notification] ReadAllByMember',
ReadAllByMemberSuccess = '[Notification.ReadAllByMemberSuccess] ReadAllByMemberSuccess', ReadAllByMemberSuccess = '[Notification.notification] ReadAllByMemberSuccess',
ReadAllByMemberFailure = '[Notification.ReadAllByMemberFailure] ReadAllByMemberFailure', ReadAllByMemberFailure = '[Notification.notification] ReadAllByMemberFailure',
} }
export class ReadAllByMember implements Action { export class ReadAllByMember implements Action {

View File

@ -1,8 +1,8 @@
import { TestBed, inject } from '@angular/core/testing'; import { TestBed, inject } from '@angular/core/testing';
import { Effects } from './readallbymember.effect'; import { Effects } from './notification.effect';
describe('ReadAllByMember.Effects', () => { describe('Notification.Effects', () => {
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
providers: [Effects] providers: [Effects]

View File

@ -23,7 +23,7 @@ import {
ReadAllByMemberSuccess, ReadAllByMemberSuccess,
ReadAllByMemberFailure, ReadAllByMemberFailure,
ActionType, ActionType,
} from './readallbymember.action'; } from './notification.action';
@Injectable() @Injectable()
export class Effects { export class Effects {
@ -36,13 +36,22 @@ export class Effects {
@Effect() @Effect()
readAllByMember$: Observable<Action> = this.actions$ 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)))
// );
.ofType(ActionType.ReadAllByMember) .ofType(ActionType.ReadAllByMember)
.map((action: ReadAllByMember) => action.payload) .map((action: ReadAllByMember) => action.payload)
.exhaustMap(member => .switchMap(payload => this.notificationService.readAllByMember(payload))
this.notificationService .map(notifications => {
.readAllByMember(member) return new ReadAllByMemberSuccess(notifications);
.map(notificationList => new ReadAllByMemberSuccess(notificationList)) })
.catch(error => of(new ReadAllByMemberFailure(error))) .catch((error: RPCError) => {
); return of(new ReadAllByMemberFailure(error));
});
} }

View File

@ -1,12 +1,12 @@
import { import {
Actions, Actions,
ActionType, ActionType,
} from './readallbymember.action'; } from './notification.action';
import { import {
State, State,
initialState, initialState,
} from './readallbymember.state'; } from './notification.state';
import { Notification } from '../../model'; import { Notification } from '../../model';
@ -25,7 +25,7 @@ import {
...state, ...state,
error: null, error: null,
pending: false, pending: false,
notificationList: action.payload, notifications: action.payload,
}; };
} }
@ -34,7 +34,7 @@ import {
...state, ...state,
error: action.payload, error: action.payload,
pending: false, pending: false,
notificationList: null, notifications: null,
}; };
} }

View File

@ -5,11 +5,11 @@ import { Notification } from '../../model';
export interface State { export interface State {
error: RPCError | null; error: RPCError | null;
pending: boolean; pending: boolean;
notificationList: Notification[] | null; notifications: Notification[] | null;
} }
export const initialState: State = { export const initialState: State = {
error: null, error: null,
pending: false, pending: false,
notificationList: null, notifications: null,
}; };

View File

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