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

@ -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,12 +1,12 @@
import {Injectable} from '@angular/core';
import {Store} from '@ngrx/store';
import * as appCommonReducer from '../reducers/app-common.reducer'
import { IPackages } from '../models/package';
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 {HttpClient} from '@angular/common/http';
import {AppConfig} from '../shared/app.config';
import {Observable} from 'rxjs';
@Injectable({
@ -14,12 +14,12 @@ import { Observable } from 'rxjs';
})
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() {
@ -27,8 +27,8 @@ export class PackageService {
}
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> {