From 713af307cd4e98a340c126732d971bd8d5982a05 Mon Sep 17 00:00:00 2001 From: Willem Dantuma Date: Wed, 16 Mar 2022 14:20:28 +0100 Subject: [PATCH] Add fmPackageExists directive --- .../common/src/fm/actions/app-common.actions.ts | 17 +++++++++++++++++ projects/common/src/fm/common.module.ts | 4 ++++ .../common/src/fm/effects/app-common.effects.ts | 11 ++++++++++- projects/common/src/fm/models/package.ts | 5 +++++ .../src/fm/reducers/app-common.reducer.ts | 15 ++++++++++++++- .../common/src/fm/services/package.service.ts | 8 ++++++++ src/app/test/test.component.html | 2 +- 7 files changed, 59 insertions(+), 3 deletions(-) diff --git a/projects/common/src/fm/actions/app-common.actions.ts b/projects/common/src/fm/actions/app-common.actions.ts index 7cde408..82006f9 100644 --- a/projects/common/src/fm/actions/app-common.actions.ts +++ b/projects/common/src/fm/actions/app-common.actions.ts @@ -12,6 +12,9 @@ export const INITUSERSUCCESS = '[AppCommon] InitUserSuccess'; export const INITUSERPACKAGES = '[AppCommon] InitUserPackages'; export const INITUSERPACKAGESSUCCESS = '[AppCommon] InitUserPackagesSuccess'; +export const INITPACKAGES = '[AppCommon] InitPackages'; +export const INITPACKAGESSUCCESS = '[AppCommon] InitPackagesSuccess'; + export const INITUSERSETTINGSROOT = '[AppCommon] InitUserSettingsRoot'; export const INITUSERSETTINGSROOTSUCCESS = '[AppCommon] InitUserSettingsRootSuccess'; @@ -102,6 +105,18 @@ export class InitUserPackagesSuccess implements Action { constructor(public items:IItem[] ) { } } +export class InitPackages implements Action { + readonly type = INITPACKAGES; + + constructor() {} +} + +export class InitPackagesSuccess implements Action { + readonly type = INITPACKAGESSUCCESS; + + constructor(public items:IItem[] ) { } +} + export class InitUserSettingsRoot implements Action { readonly type = INITUSERSETTINGSROOT; @@ -381,6 +396,8 @@ export type Actions = OpenModal | SetMenuVisible | InitUserPackages | InitUserPackagesSuccess + | InitPackages + | InitPackagesSuccess | InitUserSettingsRoot | InitUserSettingsRootSuccess | ToggleAccountMenu diff --git a/projects/common/src/fm/common.module.ts b/projects/common/src/fm/common.module.ts index 8c3c2f6..587ca1e 100644 --- a/projects/common/src/fm/common.module.ts +++ b/projects/common/src/fm/common.module.ts @@ -32,6 +32,7 @@ import { TimespanComponent } from './components/timespan/timespan.component'; import { TagInputComponent } from './components/tag-input/tag-input.component'; import { MenuBackgroundComponent } from './components/menu-background/menu-background.component'; import { HasPackageDirective} from './components/has-package/has-package.directive'; +import { PackageExistsDirective} from './components/package-exists/package-exists.directive'; import { HasClaimDirective} from './components/has-claim/has-claim.directive'; import { UserMenuComponent} from './components/user-menu/user-menu.component'; import { ThumbnailComponent } from './components/thumbnail/thumbnail.component'; @@ -82,6 +83,7 @@ export { UserMenuComponent, ThumbnailComponent, HasPackageDirective, + PackageExistsDirective, HasClaimDirective, Alert, IEventMessage, @@ -144,6 +146,7 @@ export { SessionClearedComponent, MenuBackgroundComponent, HasPackageDirective, + PackageExistsDirective, HasClaimDirective, UserMenuComponent, GradientComponent, @@ -174,6 +177,7 @@ export { SessionClearedComponent, MenuBackgroundComponent, HasPackageDirective, + PackageExistsDirective, HasClaimDirective, UserMenuComponent, GradientComponent, diff --git a/projects/common/src/fm/effects/app-common.effects.ts b/projects/common/src/fm/effects/app-common.effects.ts index a3ae339..a64333a 100644 --- a/projects/common/src/fm/effects/app-common.effects.ts +++ b/projects/common/src/fm/effects/app-common.effects.ts @@ -62,6 +62,15 @@ export class AppCommonEffects { ) }) )); + 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))) + ) + }) + )); userPackagesChanged$ = createEffect(() => this.actions$.pipe( ofType(appCommonActions.ITEMCHANGEDEVENT), @@ -99,7 +108,7 @@ export class AppCommonEffects { initUserSuccess$ = createEffect(() => this.actions$.pipe( ofType(appCommonActions.INITUSERSUCCESS), switchMap(() => { - return [new appCommonActions.InitRoot(),new appCommonActions.InitUserPackages(),new appCommonActions.InitUserSettingsRoot()]; + return [new appCommonActions.InitRoot(),new appCommonActions.InitUserPackages(),new appCommonActions.InitPackages(),new appCommonActions.InitUserSettingsRoot()]; } ))); diff --git a/projects/common/src/fm/models/package.ts b/projects/common/src/fm/models/package.ts index 43bda7a..451a3ee 100644 --- a/projects/common/src/fm/models/package.ts +++ b/projects/common/src/fm/models/package.ts @@ -9,3 +9,8 @@ export interface IPackage { export interface IPackages { [id: string]: IPackage[]; } + +export interface IPackageMap { + [id: string]: IPackage; +} + diff --git a/projects/common/src/fm/reducers/app-common.reducer.ts b/projects/common/src/fm/reducers/app-common.reducer.ts index 600b900..c370e90 100644 --- a/projects/common/src/fm/reducers/app-common.reducer.ts +++ b/projects/common/src/fm/reducers/app-common.reducer.ts @@ -2,7 +2,7 @@ import { tassign } from 'tassign'; import { IItemTypes} from '../models/item.types'; import { IListItem } from '../models/list.item'; import { IUser } from '../models/user'; -import { IPackage,IPackages} from '../models/package'; +import { IPackage,IPackageMap,IPackages} from '../models/package'; import * as appCommonActions from '../actions/app-common.actions'; import { createSelector, createFeatureSelector, ActionReducerMap } from '@ngrx/store'; @@ -20,6 +20,7 @@ export interface State { routeLoading:boolean, menuVisible: boolean, userPackages: IPackages, + packages: IPackageMap, userSettingsRoot: IItem, accountMenuVisible: boolean, appMenuVisible: boolean, @@ -40,6 +41,7 @@ export const initialState: State = { routeLoading: false, menuVisible: false, userPackages: {}, + packages: {}, userSettingsRoot: null, accountMenuVisible: false, appMenuVisible: false, @@ -140,6 +142,15 @@ export function reducer(state = initialState, action: appCommonActions.Actions ) return tassign(state,{userPackages:packages}); } + case appCommonActions.INITPACKAGESSUCCESS:{ + let a = action as appCommonActions.InitPackagesSuccess; + let packages = {} + a.items.forEach((item) => { + packages[item.data.id] = item.data; + }); + + return tassign(state,{packages:packages}); + } case appCommonActions.INITUSERSETTINGSROOTSUCCESS:{ let a = action as appCommonActions.InitUserSettingsRootSuccess; return tassign(state, { userSettingsRoot : a.item }); @@ -187,6 +198,7 @@ export const getRouteLoading = (state: State) => state.routeLoading; export const getMenuVisible = (state: State) => state.menuVisible; export const getUser = (state: State) => state.user; export const getUserPackages = (state: State) => state.userPackages; +export const getPackages = (state: State) => state.packages; export const getUserSettingsRoot = (state: State) => state.userSettingsRoot; export const getAccountMenuVisible = (state: State) => state.accountMenuVisible; export const getAppMenuVisible = (state: State) => state.appMenuVisible; @@ -207,6 +219,7 @@ export const selectGetRouteLoading = createSelector(selectAppCommonState, getRou export const SelectGetMenuVisible = createSelector(selectAppCommonState,getMenuVisible); export const SelectGetUser = createSelector(selectAppCommonState,getUser); export const SelectGetUserPackages = createSelector(selectAppCommonState,getUserPackages); +export const SelectGetPackages = createSelector(selectAppCommonState,getPackages); export const SelectGetValidUserPackages = createSelector(SelectGetUserPackages, (packageMap) => { return getValidPackages(packageMap); }); diff --git a/projects/common/src/fm/services/package.service.ts b/projects/common/src/fm/services/package.service.ts index ef8552e..9967f3c 100644 --- a/projects/common/src/fm/services/package.service.ts +++ b/projects/common/src/fm/services/package.service.ts @@ -14,10 +14,14 @@ import {Observable} from 'rxjs'; }) export class PackageService { + private userPackages: { [key: string]: IPackage } = {}; private packages: { [key: string]: IPackage } = {}; constructor(private store$: Store, public httpClient: HttpClient, public appConfig: AppConfig) { store$.select(appCommonReducer.SelectGetValidUserPackages).subscribe((packages) => { + this.userPackages = packages; + }); + store$.select(appCommonReducer.SelectGetPackages).subscribe((packages) => { this.packages = packages; }); } @@ -27,6 +31,10 @@ export class PackageService { } hasPackage(id: string): boolean { + return id in this.userPackages; + } + + packageExists(id: string): boolean { return id in this.packages; } diff --git a/src/app/test/test.component.html b/src/app/test/test.component.html index 61eab6c..6cd78ed 100644 --- a/src/app/test/test.component.html +++ b/src/app/test/test.component.html @@ -2,7 +2,7 @@
-
+