From b7c80dfdd8784a63c4fcc368450cd9cc8182ce3b Mon Sep 17 00:00:00 2001 From: Mark van der Wal Date: Thu, 27 Jan 2022 14:06:23 +0100 Subject: [PATCH] AW-3441 add valid packages selector --- package.json | 2 +- projects/common/src/fm/models/package.ts | 4 +-- .../src/fm/reducers/app-common.reducer.ts | 28 ++++++++++++--- .../common/src/fm/services/package.service.ts | 36 +++++++++---------- 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index 5b1e488..1565053 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "farmmaps-lib-app", - "version": "1.1.6", + "version": "1.1.7", "scripts": { "ng": "ng", "start": "ng serve", diff --git a/projects/common/src/fm/models/package.ts b/projects/common/src/fm/models/package.ts index 850e5fb..43bda7a 100644 --- a/projects/common/src/fm/models/package.ts +++ b/projects/common/src/fm/models/package.ts @@ -7,5 +7,5 @@ export interface IPackage { } export interface IPackages { - [id: string]: IPackage; -} \ No newline at end of file + [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 bc5dcbb..2b8f19d 100644 --- a/projects/common/src/fm/reducers/app-common.reducer.ts +++ b/projects/common/src/fm/reducers/app-common.reducer.ts @@ -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); diff --git a/projects/common/src/fm/services/package.service.ts b/projects/common/src/fm/services/package.service.ts index bd0c4fd..b5b9bad 100644 --- a/projects/common/src/fm/services/package.service.ts +++ b/projects/common/src/fm/services/package.service.ts @@ -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, 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 { return this.httpClient.post(`${this.ApiEndpoint()}/api/v1/items/${item.code}/packagetasks`, task); - } -} \ No newline at end of file + } +}