AW-3441 add valid packages selector
All checks were successful
FarmMaps/FarmMapsLib/pipeline/head This commit looks good

This commit is contained in:
Mark van der Wal 2022-01-27 14:06:23 +01:00
parent d89670f669
commit b7c80dfdd8
4 changed files with 45 additions and 25 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "farmmaps-lib-app", "name": "farmmaps-lib-app",
"version": "1.1.6", "version": "1.1.7",
"scripts": { "scripts": {
"ng": "ng", "ng": "ng",
"start": "ng serve", "start": "ng serve",

View File

@ -7,5 +7,5 @@ export interface IPackage {
} }
export interface IPackages { export interface IPackages {
[id: string]: IPackage; [id: string]: IPackage[];
} }

View File

@ -66,7 +66,7 @@ export function reducer(state = initialState, action: appCommonActions.Actions )
name:a.userinfo["name"], name:a.userinfo["name"],
claims:claims, claims:claims,
searchable: false searchable: false
}; };
return tassign(state, { user: user }); return tassign(state, { user: user });
} }
case appCommonActions.INITROOTSUCCESS: { case appCommonActions.INITROOTSUCCESS: {
@ -75,10 +75,10 @@ export function reducer(state = initialState, action: appCommonActions.Actions )
} }
case appCommonActions.OPENMODAL: { case appCommonActions.OPENMODAL: {
return tassign(state, { openedModalName: action.modalName }); return tassign(state, { openedModalName: action.modalName });
} }
case appCommonActions.CLOSEMODAL: { case appCommonActions.CLOSEMODAL: {
return tassign(state, { openedModalName: null }); return tassign(state, { openedModalName: null });
} }
case appCommonActions.LOADITEMTYPESSUCCESS: { case appCommonActions.LOADITEMTYPESSUCCESS: {
let a = action as appCommonActions.LoadItemTypesSuccess; let a = action as appCommonActions.LoadItemTypesSuccess;
return tassign(state, { itemTypes: a.itemTypes }); return tassign(state, { itemTypes: a.itemTypes });
@ -131,7 +131,10 @@ export function reducer(state = initialState, action: appCommonActions.Actions )
a.items.forEach((item) => { a.items.forEach((item) => {
item.data.dataDate = item.dataDate; item.data.dataDate = item.dataDate;
item.data.dataEndDate = item.dataEndDate; item.data.dataEndDate = item.dataEndDate;
packages[item.data.id]=item.data; if (!packages[item.data.id]) {
packages[item.data.id] = [];
}
packages[item.data.id].push(item.data);
}); });
return tassign(state,{userPackages:packages}); return tassign(state,{userPackages:packages});
@ -203,6 +206,23 @@ export const selectGetRouteLoading = createSelector(selectAppCommonState, getRou
export const SelectGetMenuVisible = createSelector(selectAppCommonState,getMenuVisible); export const SelectGetMenuVisible = createSelector(selectAppCommonState,getMenuVisible);
export const SelectGetUser = createSelector(selectAppCommonState,getUser); export const SelectGetUser = createSelector(selectAppCommonState,getUser);
export const SelectGetUserPackages = createSelector(selectAppCommonState,getUserPackages); export const SelectGetUserPackages = createSelector(selectAppCommonState,getUserPackages);
export const SelectGetValidUserPackages = createSelector(SelectGetUserPackages, (packageMap) => {
const nowYear = new Date(Date.now()).getUTCFullYear();
const keys = Object.keys(packageMap);
return keys.filter(k => {
const packages = packageMap[k]
.filter((p) => p.dataEndDate.getUTCFullYear() >= nowYear);
return packages.length > 0;
}).reduce((map, key) => {
const packages = packageMap[key];
return {
...map,
key: packages[0]
};
}, {});
});
export const SelectGetUserSettingsRoot = createSelector(selectAppCommonState,getUserSettingsRoot); export const SelectGetUserSettingsRoot = createSelector(selectAppCommonState,getUserSettingsRoot);
export const SelectGetAccountMenuVisible = createSelector(selectAppCommonState,getAccountMenuVisible); export const SelectGetAccountMenuVisible = createSelector(selectAppCommonState,getAccountMenuVisible);
export const SelectGetAppMenuVisible = createSelector(selectAppCommonState,getAppMenuVisible); export const SelectGetAppMenuVisible = createSelector(selectAppCommonState,getAppMenuVisible);

View File

@ -1,37 +1,37 @@
import { Injectable } from '@angular/core'; import {Injectable} from '@angular/core';
import { Store} from '@ngrx/store'; import {Store} from '@ngrx/store';
import * as appCommonReducer from '../reducers/app-common.reducer' import * as appCommonReducer from '../reducers/app-common.reducer';
import { IPackages } from '../models/package'; import {IPackage} from '../models/package';
import { IItem } from '../models/item'; import {IItem} from '../models/item';
import { IItemTask } from '../models/itemTask'; import {IItemTask} from '../models/itemTask';
import { HttpClient } from "@angular/common/http"; import {HttpClient} from '@angular/common/http';
import { AppConfig } from "../shared/app.config"; import {AppConfig} from '../shared/app.config';
import { Observable } from 'rxjs'; import {Observable} from 'rxjs';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class PackageService { export class PackageService {
private packages$:IPackages = {}; private packages: {[key: string]: IPackage} = {};
constructor(private store$: Store<appCommonReducer.State>, public httpClient: HttpClient, public appConfig: AppConfig) { constructor(private store$: Store<appCommonReducer.State>, public httpClient: HttpClient, public appConfig: AppConfig) {
store$.select(appCommonReducer.SelectGetUserPackages).subscribe((packages) => { store$.select(appCommonReducer.SelectGetValidUserPackages).subscribe((packages) => {
this.packages$ = packages; this.packages = packages;
}) });
} }
ApiEndpoint() { ApiEndpoint() {
return this.appConfig.getConfig("apiEndPoint"); return this.appConfig.getConfig("apiEndPoint");
} }
hasPackage(id:string):boolean { hasPackage(id:string):boolean {
if(!this.packages$[id]) return false; if(!this.packages[id]) return false;
return this.packages$[id].enabled ? this.packages$[id].enabled == true : true; return this.packages[id].enabled ? this.packages[id].enabled === true : true;
} }
postItemPackageTask(item: IItem, task: IItemTask): Observable<IItemTask> { postItemPackageTask(item: IItem, task: IItemTask): Observable<IItemTask> {
return this.httpClient.post<IItemTask>(`${this.ApiEndpoint()}/api/v1/items/${item.code}/packagetasks`, task); return this.httpClient.post<IItemTask>(`${this.ApiEndpoint()}/api/v1/items/${item.code}/packagetasks`, task);
} }
} }