import { Injectable } from "@angular/core"; import { FeaturedService } from "../services/featuredService"; import { Utility } from "./utility"; import { Feature, Unit } from "../../public-api"; /** * Exposes the User endpoint * */ @Injectable({ providedIn: 'root' }) export class FeaturedState { features!: Array; units!:Array constructor(private feature: FeaturedService, private utility:Utility) { // this.features = new Array(); } getFeatures(id: string) { return this.features.filter(f => f.productID === id); } get(error:any) { const self = this; this.feature.get().subscribe({ next(rslt) { if (rslt) { // if(rslt.results.length === 1){ // done(rslt.results[0]); // } else { // done(); // } self.utility.update(self.features, rslt.results) // self.api.update(self.authUsers, rslt.results.users) } }, error(e) { error(e) }, complete() { } }) } getByID(id: string, done:any, error:any) { // const self = this; this.feature.getByID(id).subscribe({ next(rslt) { if (rslt) { done(rslt.results) // const index = self.features.findIndex(fi => rslt.results._id === fi._id); // if(index >=0){ // self.features[index] = rslt.results // } } }, error(e) { error(e) }, complete() { } }) } getByProduct(id: string, done:any, error:any) { // const self = this; // console.log('get by product hit') this.feature.getByProduct(id).subscribe({ next(rslt) { if (rslt) { // console.log('return result:' + JSON.stringify(rslt.results)) done(rslt.results) // const index = self.features.findIndex(fi => rslt.results._id === fi._id); // if(index >=0){ // self.features[index] = rslt.results // } } }, error(e) { error(e) }, complete() { // done() } }) } post(feature: Feature,done:any, error:any) { const self = this; this.feature.post(feature).subscribe({ next(rslt) { if (rslt) { const index = self.features.findIndex(fi => rslt.results._id === fi._id); if(index >=0){ self.features[index] = rslt.results } done(rslt.results) } }, error(e) { error(e) }, complete() { // done() } }) } put(feature: Feature, done:any, error:any) { const self = this; this.feature.put(feature).subscribe({ next(rslt) { if (rslt) { const index = self.features.findIndex(fi => rslt.results._id === fi._id); if(index >=0){ self.features[index] = rslt.results } done(rslt.results) } }, error(e) { error(e) }, complete() { // done(); } }) } delete(feature: Feature, done:any, error:any) { const self = this; this.feature.delete(feature).subscribe({ next(rslt) { if (rslt) { // rslt.results.forEach((f:Feature) => { const index = self.features.findIndex(fi => rslt.results._id === fi._id); if(index >=0){ self.features.splice(index,1) } done(rslt.results) // }) } }, error(e) { error(e) // done(); }, complete() { // done(); } }) } }