2020-01-27 16:01:35 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Router } from '@angular/router';
|
2020-06-10 19:53:54 +00:00
|
|
|
import { OAuthService,UserInfo } from 'angular-oauth2-oidc';
|
2020-01-27 16:01:35 +00:00
|
|
|
import { Store, Action } from '@ngrx/store';
|
|
|
|
import { Effect, Actions,ofType } from '@ngrx/effects';
|
2020-06-10 19:53:54 +00:00
|
|
|
import { Observable , defer , of,from } from 'rxjs';
|
2020-01-27 16:01:35 +00:00
|
|
|
import { withLatestFrom,mergeMap,switchMap,map,catchError} from 'rxjs/operators';
|
|
|
|
import * as appCommonActions from '../actions/app-common.actions';
|
|
|
|
import * as appCommonReducers from '../reducers/app-common.reducer';
|
|
|
|
import { ItemService } from '../services/item.service';
|
|
|
|
import { FolderService } from '../services/folder.service';
|
|
|
|
import { UserService } from '../services/user.service';
|
|
|
|
import { IItemTypes } from '../models/item.types';
|
|
|
|
import { IListItem } from '../models/list.item';
|
|
|
|
import { IUser } from '../models/user';
|
2020-02-19 11:02:56 +00:00
|
|
|
import {IQueryState} from '../models/query.state';
|
|
|
|
import {StateSerializerService} from '../services/state-serializer.service';
|
2020-01-27 16:01:35 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AppCommonEffects {
|
|
|
|
|
|
|
|
@Effect({ dispatch: false })
|
|
|
|
login$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(appCommonActions.LOGIN),
|
|
|
|
withLatestFrom(this.store$.select(appCommonReducers.selectGetInitialized)),
|
|
|
|
mergeMap(([action, initialized]) => {
|
|
|
|
var a = (action as appCommonActions.Login);
|
2020-05-09 07:14:16 +00:00
|
|
|
this.oauthService$.initCodeFlow(a.url);
|
2020-01-27 16:01:35 +00:00
|
|
|
return [];
|
|
|
|
}));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
loadItemTypes$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(appCommonActions.LOADITEMTYPES),
|
|
|
|
switchMap((action) => {
|
|
|
|
return this.itemService$.getItemTypes().pipe(
|
|
|
|
map((itemTypes: IItemTypes) => new appCommonActions.LoadItemTypesSuccess(itemTypes)),
|
|
|
|
catchError(error => of(new appCommonActions.Fail(error))))
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
initUser$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(appCommonActions.INITUSER),
|
2020-02-05 13:57:12 +00:00
|
|
|
withLatestFrom(this.store$.select(appCommonReducers.selectGetInitialized)),
|
|
|
|
switchMap(([action, initialized]) => {
|
2020-06-10 19:53:54 +00:00
|
|
|
if(!initialized) {
|
2020-01-27 16:01:35 +00:00
|
|
|
return this.userService$.getCurrentUser().pipe(
|
2020-06-10 19:53:54 +00:00
|
|
|
withLatestFrom(from(this.oauthService$.loadUserProfile())),
|
|
|
|
switchMap(([user,userInfo]) => {return of(new appCommonActions.InitUserSuccess(user,userInfo as UserInfo))} ),
|
2020-01-27 16:01:35 +00:00
|
|
|
catchError(error => of(new appCommonActions.Fail(error))))
|
2020-02-05 13:57:12 +00:00
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
2020-01-27 16:01:35 +00:00
|
|
|
}
|
|
|
|
));
|
2020-05-13 10:30:09 +00:00
|
|
|
|
|
|
|
@Effect()
|
|
|
|
initUserPackages$:Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(appCommonActions.INITUSERSUCCESS),
|
|
|
|
switchMap((action) => {
|
|
|
|
let a = action as appCommonActions.InitUserSuccess;
|
|
|
|
return this.itemService$.getChildItemList(a.user.code+":USER_PACKAGES","vnd.farmmaps.itemtype.package").pipe(
|
|
|
|
switchMap((items) => of(new appCommonActions.InitUserPackagesSuccess(items))),
|
|
|
|
catchError(error => of(new appCommonActions.Fail(error)))
|
|
|
|
)
|
|
|
|
})
|
|
|
|
);
|
2020-01-27 16:01:35 +00:00
|
|
|
|
|
|
|
@Effect()
|
|
|
|
initUserSuccess$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(appCommonActions.INITUSERSUCCESS),
|
|
|
|
switchMap(() => {
|
|
|
|
return of(new appCommonActions.InitRoot());
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
initRoot$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(appCommonActions.INITROOT),
|
|
|
|
switchMap(() => {
|
|
|
|
return this.folderService$.getMyRoots().pipe(
|
|
|
|
map((folders: IListItem[]) => new appCommonActions.InitRootSuccess(folders)),
|
|
|
|
catchError(error => of(new appCommonActions.Fail(error))))
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
deleteItems$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(appCommonActions.DELETEITEMS),
|
|
|
|
switchMap((action:appCommonActions.DeleteItems) => {
|
|
|
|
return this.itemService$.deleteItems(action.itemCodes).pipe(
|
|
|
|
map((deletedItemCodes: string[]) => new appCommonActions.DeleteItemsSuccess(deletedItemCodes)),
|
|
|
|
catchError(error => of(new appCommonActions.Fail(error))))
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
editItem$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(appCommonActions.EDITITEM),
|
|
|
|
withLatestFrom(this.store$.select(appCommonReducers.selectGetItemTypes)),
|
|
|
|
switchMap(([action, itemtypes]) => {
|
|
|
|
var a = action as appCommonActions.EditItem;
|
|
|
|
var itemType = itemtypes[a.item.itemType];
|
|
|
|
var editor = itemType.editor ? itemType.editor : "property";
|
|
|
|
this.router$.navigate(['/editor',editor,'item', a.item.code])
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
viewItem$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(appCommonActions.VIEWITEM),
|
|
|
|
withLatestFrom(this.store$.select(appCommonReducers.selectGetItemTypes)),
|
|
|
|
switchMap(([action, itemtypes]) => {
|
|
|
|
var a = action as appCommonActions.EditItem;
|
|
|
|
var itemType = itemtypes[a.item.itemType];
|
|
|
|
var viewer = itemType.viewer;
|
2020-02-21 10:16:30 +00:00
|
|
|
var editor = itemType.editor;
|
2020-02-21 10:28:46 +00:00
|
|
|
if(viewer == 'select_as_mapitem') {
|
2020-02-19 11:02:56 +00:00
|
|
|
let queryState = {
|
2020-02-21 10:28:46 +00:00
|
|
|
itemCode: a.item.code,
|
|
|
|
parentCode: null,
|
2020-02-19 11:02:56 +00:00
|
|
|
level: 1,
|
2020-02-21 10:28:46 +00:00
|
|
|
itemType: null,
|
2020-02-19 11:02:56 +00:00
|
|
|
bboxFilter: false,
|
|
|
|
query: null,
|
|
|
|
tags: null,
|
|
|
|
endDate: null,
|
|
|
|
startDate: null,
|
|
|
|
bbox: []
|
|
|
|
};
|
|
|
|
let query = this.stateSerializerService$.serialize(queryState);
|
|
|
|
this.router$.navigate(['/map', query ])
|
2020-02-21 10:16:30 +00:00
|
|
|
}else if(viewer == 'edit_in_editor') {
|
|
|
|
this.router$.navigate(['/editor', editor, 'item', a.item.code])
|
2020-02-19 11:02:56 +00:00
|
|
|
} else {
|
|
|
|
this.router$.navigate(['/viewer', viewer, 'item', a.item.code])
|
|
|
|
}
|
2020-01-27 16:01:35 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2020-01-27 16:28:17 +00:00
|
|
|
|
2020-01-27 16:01:35 +00:00
|
|
|
@Effect({ dispatch: false })
|
|
|
|
fail$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(appCommonActions.FAIL),
|
|
|
|
map((action) => {
|
|
|
|
let failAction = action as appCommonActions.Fail;
|
|
|
|
console.debug(failAction.payload)
|
|
|
|
return null;
|
|
|
|
}));
|
|
|
|
|
2020-02-19 11:02:56 +00:00
|
|
|
constructor(private actions$: Actions, private store$: Store<appCommonReducers.State>, private oauthService$: OAuthService, private itemService$: ItemService, private folderService$:FolderService, private userService$: UserService, private router$: Router, private stateSerializerService$:StateSerializerService) {
|
2020-01-27 16:01:35 +00:00
|
|
|
store$.dispatch(new appCommonActions.LoadItemTypes());
|
|
|
|
}
|
|
|
|
}
|