113 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-09-25 17:26:19 +09:00
import { Injectable } from '@angular/core';
import { Actions, ofType, createEffect } from '@ngrx/effects';
import { of } from 'rxjs';
2019-09-25 18:08:50 +09:00
import { catchError, exhaustMap, map, withLatestFrom } from 'rxjs/operators';
2019-09-25 17:26:19 +09:00
2019-09-25 18:08:50 +09:00
import { Store, select } from '@ngrx/store';
2019-09-26 11:11:22 +09:00
import { NGXLogger } from 'ngx-logger';
2019-09-25 18:08:50 +09:00
import {
buddy2,
buddy2Success,
buddy2Failure,
buddy2Data,
group2,
group2Success,
group2Failure,
group2Data
} from './actions';
2019-09-25 17:26:19 +09:00
import { SessionStorageService } from '@ucap-webmessenger/web-storage';
import {
SyncProtocolService,
SSVC_TYPE_SYNC_BUDDY2_DATA,
BuddyResponse,
2019-09-25 18:08:50 +09:00
BuddyDetailData,
SSVC_TYPE_SYNC_GROUP_DATA2,
GroupDetailData,
GroupResponse
2019-09-25 17:26:19 +09:00
} from '@ucap-webmessenger/protocol-sync';
import { regViewSuccess } from '@app/store/setting/option';
@Injectable()
export class Effects {
regViewSuccess$ = createEffect(() =>
this.actions$.pipe(
ofType(regViewSuccess),
2019-09-25 18:08:50 +09:00
withLatestFrom(
this.store.pipe(
select(state => {
2019-09-26 11:11:22 +09:00
this.logger.debug('state', state);
2019-09-25 18:08:50 +09:00
return state.messenger.sync.buddy2SyncDate as string;
})
)
),
map(([action, buddy2SyncDate]) => buddy2({ syncDate: buddy2SyncDate }))
2019-09-25 17:26:19 +09:00
)
);
buddy2$ = createEffect(() =>
this.actions$.pipe(
ofType(buddy2),
exhaustMap(req =>
this.syncProtocolService.buddy2(req).pipe(
map(res => {
switch (res.Type) {
case SSVC_TYPE_SYNC_BUDDY2_DATA:
return buddy2Data({ data: res as BuddyDetailData });
}
return buddy2Success({ res: res as BuddyResponse });
}),
catchError(error => of(buddy2Failure({ error })))
)
)
)
);
2019-09-25 18:08:50 +09:00
buddy2SuccessToGroup2$ = createEffect(() =>
this.actions$.pipe(
ofType(buddy2Success),
withLatestFrom(
this.store.pipe(
select(state => {
2019-09-26 11:11:22 +09:00
this.logger.debug('state', state);
2019-09-25 18:08:50 +09:00
return state.messenger.sync.group2SyncDate as string;
})
)
),
map(([action, group2SyncDate]) => group2({ syncDate: group2SyncDate }))
)
);
group2$ = createEffect(() =>
this.actions$.pipe(
ofType(group2),
exhaustMap(req =>
this.syncProtocolService.group2(req).pipe(
map(res => {
switch (res.Type) {
case SSVC_TYPE_SYNC_GROUP_DATA2:
return group2Data({ data: res as GroupDetailData });
}
return group2Success({ res: res as GroupResponse });
}),
catchError(error => of(group2Failure({ error })))
)
)
)
);
2019-09-25 17:26:19 +09:00
constructor(
private actions$: Actions,
2019-09-25 18:08:50 +09:00
private store: Store<any>,
2019-09-25 17:26:19 +09:00
private syncProtocolService: SyncProtocolService,
2019-09-26 11:11:22 +09:00
private sessionStorageService: SessionStorageService,
private logger: NGXLogger
2019-09-25 17:26:19 +09:00
) {}
}