FarmMapsLib/projects/common/src/fm/effects/app-common.effects.ts

209 lines
8.2 KiB
TypeScript
Raw Normal View History

2020-01-27 16:01:35 +00:00
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { OAuthService,UserInfo } from 'angular-oauth2-oidc';
2021-07-27 14:26:57 +00:00
import { Store } from '@ngrx/store';
import { Actions,ofType,createEffect } from '@ngrx/effects';
import { of,from,zip } from 'rxjs';
2020-07-23 07:42:44 +00:00
import { withLatestFrom,mergeMap,switchMap,map,catchError,first} from 'rxjs/operators';
2020-01-27 16:01:35 +00:00
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';
2020-02-19 11:02:56 +00:00
import {StateSerializerService} from '../services/state-serializer.service';
2020-01-27 16:01:35 +00:00
@Injectable()
export class AppCommonEffects {
2021-07-27 14:26:57 +00:00
login$ = createEffect(() => this.actions$.pipe(
2020-01-27 16:01:35 +00:00
ofType(appCommonActions.LOGIN),
withLatestFrom(this.store$.select(appCommonReducers.selectGetInitialized)),
mergeMap(([action, initialized]) => {
2023-03-06 13:04:14 +00:00
const a = (action as appCommonActions.Login);
2020-06-25 16:52:13 +00:00
this.oauthService$.initCodeFlow(a.url,{"prompt":"login"});
2020-01-27 16:01:35 +00:00
return [];
2021-07-27 14:26:57 +00:00
})),{dispatch:false});
2020-01-27 16:01:35 +00:00
2021-07-27 14:26:57 +00:00
logout$ = createEffect(() => this.actions$.pipe(
2020-06-25 16:52:13 +00:00
ofType(appCommonActions.LOGOUT),
mergeMap((action) => {
2020-10-09 14:35:14 +00:00
this.oauthService$.revokeTokenAndLogout();
2020-06-25 16:52:13 +00:00
return [];
2021-07-27 14:26:57 +00:00
})),{dispatch:false});
2020-06-25 16:52:13 +00:00
2021-07-27 14:26:57 +00:00
loadItemTypes$ = createEffect(() => this.actions$.pipe(
2020-01-27 16:01:35 +00:00
ofType(appCommonActions.LOADITEMTYPES),
switchMap((action) => {
return this.itemService$.getItemTypes().pipe(
map((itemTypes: IItemTypes) => new appCommonActions.LoadItemTypesSuccess(itemTypes)),
catchError(error => of(new appCommonActions.Fail(error))))
}
2021-07-27 14:26:57 +00:00
)));
2020-01-27 16:01:35 +00:00
2021-07-27 14:26:57 +00:00
initUser$ = createEffect(() => this.actions$.pipe(
2020-01-27 16:01:35 +00:00
ofType(appCommonActions.INITUSER),
2020-07-23 07:42:44 +00:00
first(),
switchMap((action) => {
return zip(this.userService$.getCurrentUser(),from(this.oauthService$.loadUserProfile())).pipe(
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))))
}
2021-07-27 14:26:57 +00:00
)));
2020-08-04 14:09:22 +00:00
2021-07-27 14:26:57 +00:00
initUserPackages$ = createEffect(() => this.actions$.pipe(
2020-08-04 14:09:22 +00:00
ofType(appCommonActions.INITUSERPACKAGES),
switchMap(() => {
return this.itemService$.getItemList('vnd.farmmaps.itemtype.package').pipe(
2020-05-13 10:30:09 +00:00
switchMap((items) => of(new appCommonActions.InitUserPackagesSuccess(items))),
catchError(error => of(new appCommonActions.Fail(error)))
)
})
2021-07-27 14:26:57 +00:00
));
2022-03-16 13:20:28 +00:00
initPackages$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.INITPACKAGES),
switchMap(() => {
return this.itemService$.getItemList('vnd.farmmaps.itemtype.package.template').pipe(
switchMap((items) => of(new appCommonActions.InitPackagesSuccess(items))),
catchError(error => of(new appCommonActions.Fail(error)))
)
})
));
2020-01-27 16:01:35 +00:00
2021-07-27 14:26:57 +00:00
userPackagesChanged$ = createEffect(() => this.actions$.pipe(
2020-08-04 14:09:22 +00:00
ofType(appCommonActions.ITEMCHANGEDEVENT),
switchMap((action) => {
2023-03-06 13:04:14 +00:00
const a = action as appCommonActions.ItemChangedEvent;
2020-08-04 14:09:22 +00:00
if(a.itemCode.endsWith(":USER_PACKAGES"))
return of(new appCommonActions.InitUserPackages());
else
2020-08-05 08:33:55 +00:00
return [];
2020-08-04 14:09:22 +00:00
})
2021-07-27 14:26:57 +00:00
));
2020-08-13 11:06:28 +00:00
2021-07-27 14:26:57 +00:00
initUserSettingsRoot$ = createEffect(() => this.actions$.pipe(
2020-08-13 11:06:28 +00:00
ofType(appCommonActions.INITUSERSETTINGSROOT),
withLatestFrom(this.store$.select(appCommonReducers.SelectGetUser)),
switchMap(([, user]) => {
return this.itemService$.getItem(user.code + ':USER_SETTINGS').pipe(
switchMap((item) => of(new appCommonActions.InitUserSettingsRootSuccess(item))),
catchError(error => of(new appCommonActions.Fail(error)))
)
})
2021-07-27 14:26:57 +00:00
));
2020-08-13 11:06:28 +00:00
2021-07-27 14:26:57 +00:00
initUserSettingsRootChanged$ = createEffect(() => this.actions$.pipe(
2020-08-13 11:06:28 +00:00
ofType(appCommonActions.ITEMCHANGEDEVENT),
switchMap((action) => {
2023-03-06 13:04:14 +00:00
const a = action as appCommonActions.ItemChangedEvent;
2020-08-13 11:06:28 +00:00
if(a.itemCode.endsWith(":USER_SETTINGS"))
return of(new appCommonActions.InitUserSettingsRoot());
else
return [];
})
2021-07-27 14:26:57 +00:00
));
2020-08-04 14:09:22 +00:00
2021-07-27 14:26:57 +00:00
initUserSuccess$ = createEffect(() => this.actions$.pipe(
2020-01-27 16:01:35 +00:00
ofType(appCommonActions.INITUSERSUCCESS),
switchMap(() => {
2022-03-16 13:20:28 +00:00
return [new appCommonActions.InitRoot(),new appCommonActions.InitUserPackages(),new appCommonActions.InitPackages(),new appCommonActions.InitUserSettingsRoot()];
2020-01-27 16:01:35 +00:00
}
2021-07-27 14:26:57 +00:00
)));
2020-01-27 16:01:35 +00:00
2021-07-27 14:26:57 +00:00
initRoot$ = createEffect(() => this.actions$.pipe(
2020-01-27 16:01:35 +00:00
ofType(appCommonActions.INITROOT),
switchMap(() => {
return this.folderService$.getMyRoots().pipe(
map((folders: IListItem[]) => new appCommonActions.InitRootSuccess(folders)),
catchError(error => of(new appCommonActions.Fail(error))))
}
2021-07-27 14:26:57 +00:00
)));
2020-01-27 16:01:35 +00:00
2021-07-27 14:26:57 +00:00
deleteItems$ = createEffect(() => this.actions$.pipe(
2020-01-27 16:01:35 +00:00
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))))
}
2021-07-27 14:26:57 +00:00
)));
2020-01-27 16:01:35 +00:00
2021-07-27 14:26:57 +00:00
editItem$ = createEffect(() => this.actions$.pipe(
2020-01-27 16:01:35 +00:00
ofType(appCommonActions.EDITITEM),
withLatestFrom(this.store$.select(appCommonReducers.selectGetItemTypes)),
switchMap(([action, itemtypes]) => {
2023-03-06 13:04:14 +00:00
const a = action as appCommonActions.EditItem;
2020-07-22 06:22:35 +00:00
var editor = "property";
if(a.item.itemType) {
2023-03-06 13:04:14 +00:00
const itemType = itemtypes[a.item.itemType];
2020-07-22 06:23:53 +00:00
var editor = itemType && itemType.editor ? itemType.editor : editor;
2020-07-22 06:22:35 +00:00
}
2020-01-27 16:01:35 +00:00
this.router$.navigate(['/editor',editor,'item', a.item.code])
return [];
}
2021-07-27 14:26:57 +00:00
)));
2020-01-27 16:01:35 +00:00
2021-07-27 14:26:57 +00:00
viewItem$ = createEffect(() => this.actions$.pipe(
2020-01-27 16:01:35 +00:00
ofType(appCommonActions.VIEWITEM),
withLatestFrom(this.store$.select(appCommonReducers.selectGetItemTypes)),
switchMap(([action, itemtypes]) => {
2023-03-06 13:04:14 +00:00
const a = action as appCommonActions.EditItem;
const itemType = itemtypes[a.item.itemType];
const viewer = itemType.viewer;
const editor = itemType.editor;
2020-02-21 10:28:46 +00:00
if(viewer == 'select_as_mapitem') {
2023-03-06 13:04:14 +00:00
const 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: []
};
2023-03-06 13:04:14 +00:00
const query = this.stateSerializerService$.serialize(queryState);
2020-02-19 11:02:56 +00:00
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 [];
}
2021-07-27 14:26:57 +00:00
)));
2020-01-27 16:01:35 +00:00
2020-01-27 16:28:17 +00:00
2021-07-27 14:26:57 +00:00
fail$ = createEffect(() => this.actions$.pipe(
2020-01-27 16:01:35 +00:00
ofType(appCommonActions.FAIL),
map((action) => {
2023-03-06 13:04:14 +00:00
const failAction = action as appCommonActions.Fail;
2020-01-27 16:01:35 +00:00
console.debug(failAction.payload)
return null;
2021-07-27 14:26:57 +00:00
})),{dispatch:false});
2020-01-27 16:01:35 +00:00
2021-07-27 14:26:57 +00:00
online$ = createEffect(() => this.actions$.pipe(
2020-07-22 07:05:02 +00:00
ofType(appCommonActions.ONLINE),
2020-07-22 19:16:38 +00:00
switchMap((action) => {
2020-07-22 18:31:12 +00:00
console.debug("Online: Check token");
if(!this.oauthService$.hasValidAccessToken()) {
console.debug("No valid token, try to refresh");
if(this.oauthService$.getRefreshToken() != null ) {
console.debug("We have a refresh token");
2020-10-30 07:22:58 +00:00
this.oauthService$.refreshToken();
2020-07-22 18:31:12 +00:00
}
2020-10-30 07:22:58 +00:00
}
return of(undefined);
2021-07-27 14:26:57 +00:00
})),{dispatch:false});
2020-07-22 07:05:02 +00:00
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());
}
}