import { Injectable } from "@angular/core"; import { Utility } from "./utility"; import { SocialService } from "../services/socialService"; import { SocialInfo } from "../../public-api"; /** * Exposes the User endpoint * */ @Injectable({ providedIn: 'root' }) export class SocialState { socials!: Array; constructor(private social: SocialService, private utility:Utility) { this.socials = new Array(); } getSocials(type: string) { return this.socials.filter(f => f.type === type); } get(error:any) { const self = this; this.social.get().subscribe({ next(rslt) { if (rslt) { self.utility.update(self.socials, rslt.results) } }, error(e) { error(e); }, complete() { } }) } post(social: SocialInfo,done:any, error:any) { const self = this; this.social.post(social).subscribe({ next(rslt) { if (rslt) { self.utility.updateRslt(self.socials, rslt) } }, error(e) { error(e); }, complete() { } }) } put(social: SocialInfo, done:any, error:any) { const self = this; this.social.put(social).subscribe({ next(rslt) { if (rslt) { self.utility.updateRslt(self.socials, rslt) } }, error(e) { error(e); }, complete() { } }) } delete(social: SocialInfo, done:any, error:any) { const self = this; this.social.delete(social).subscribe({ next(rslt) { if (rslt) { rslt.results.forEach((f:SocialInfo) => { const index = self.socials.findIndex(fi => f._id === fi._id); if(index >=0){ self.socials.splice(index,1) } }) } }, error(e) { error(e); }, complete() { } }) } }