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",
"version": "1.1.6",
"version": "1.1.7",
"scripts": {
"ng": "ng",
"start": "ng serve",

View File

@ -7,5 +7,5 @@ export interface IPackage {
}
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"],
claims:claims,
searchable: false
};
};
return tassign(state, { user: user });
}
case appCommonActions.INITROOTSUCCESS: {
@ -75,10 +75,10 @@ export function reducer(state = initialState, action: appCommonActions.Actions )
}
case appCommonActions.OPENMODAL: {
return tassign(state, { openedModalName: action.modalName });
}
}
case appCommonActions.CLOSEMODAL: {
return tassign(state, { openedModalName: null });
}
}
case appCommonActions.LOADITEMTYPESSUCCESS: {
let a = action as appCommonActions.LoadItemTypesSuccess;
return tassign(state, { itemTypes: a.itemTypes });
@ -131,7 +131,10 @@ export function reducer(state = initialState, action: appCommonActions.Actions )
a.items.forEach((item) => {
item.data.dataDate = item.dataDate;
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});
@ -203,6 +206,23 @@ 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 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 SelectGetAccountMenuVisible = createSelector(selectAppCommonState,getAccountMenuVisible);
export const SelectGetAppMenuVisible = createSelector(selectAppCommonState,getAppMenuVisible);

View File

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