add ifPackageListExists to packageservice
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good
FarmMaps/FarmMapsLib/pipeline/head This commit looks good

This commit is contained in:
Willem Dantuma 2022-03-16 15:56:23 +01:00
parent 713af307cd
commit 57e0a37c78

View File

@ -7,7 +7,8 @@ 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 {Observable,iif,of} from 'rxjs';
import {switchMap} from 'rxjs/operators';
@Injectable({
providedIn: 'root',
@ -16,12 +17,13 @@ import {Observable} from 'rxjs';
export class PackageService {
private userPackages: { [key: string]: IPackage } = {};
private packages: { [key: string]: IPackage } = {};
private packagesObservable = this.store$.select(appCommonReducer.SelectGetPackages);
constructor(private store$: Store<appCommonReducer.State>, public httpClient: HttpClient, public appConfig: AppConfig) {
store$.select(appCommonReducer.SelectGetValidUserPackages).subscribe((packages) => {
this.userPackages = packages;
});
store$.select(appCommonReducer.SelectGetPackages).subscribe((packages) => {
this.packagesObservable.subscribe((packages) => {
this.packages = packages;
});
}
@ -41,6 +43,14 @@ export class PackageService {
postItemPackageTask(item: IItem, task: IItemTask): Observable<IItemTask> {
return this.httpClient.post<IItemTask>(`${this.ApiEndpoint()}/api/v1/items/${item.code}/packagetasks`, task);
}
ifPackageListExists<Type>(packageList: Array<string>, ifTrue:Observable<Type>,ifFalse:Observable<Type>):Observable<Type> {
return this.packagesObservable.pipe(switchMap(packages =>
iif(( )=> Object.keys(packages).some(id => packageList.includes(id)),
ifTrue,
ifFalse)
));
}
}
export function getValidPackages(packageMap: IPackages): {[key: string]: IPackage} {