import { Injectable } from "@angular/core"; import { AccountService } from "../services/accountService"; import { Utility } from "./utility"; import { Account, AccountUser, LinkedAccount } from "../../public-api"; /** * Exposes the User endpoint * */ @Injectable({ providedIn: 'root' }) export class AccountState { selectedIndex:number = 0; // account!:Account //all accounts owned by the user accounts: Array = new Array(); //all of your user accounts for the user // authUsers: Array = new Array(); // get route() { // return this.account..api.base + 'account' // } // get currAccount(){ // return this.accounts[this.selectedIndex] // } constructor(private state: AccountService,private utility:Utility) { } // hasAccount(id: string) { // return this.accounts.some(s => s.users.some(so => so.userID === id)) // } // update(account:Account){ // if(this.account._id === account._id){ // this.account = account; // } else { // const index = this.accounts.findIndex(f => f._id === account._id); // if(index >= 0){ // this.accounts[index] = account; // } // } // } get(done:any,error:any) { const self = this; this.state.get().subscribe({ next(rslt) { if (rslt) { done(rslt.results) // self.account = rslt.results // self.utility.update(self.accounts, rslt.results.accounts) // self.utility.update(self.authUsers, rslt.results.users) } }, error(e) { error(e) }, complete() { } }) } getByID(id:string, done:any,error:any) { const self = this; this.state.getByID(id).subscribe({ next(rslt) { if (rslt) { done(rslt.results) // self.utility.updateRslt(self.accounts, rslt) } }, error(e) { error(e) }, complete() { } }) } getUnverified(done:any, error:any) { const self = this; this.state.getUnverified().subscribe({ next(rslt) { if (rslt) { done(rslt.results) } }, error(e) { error(e) }, complete() { } }) } // getByID(id: string, done:any) { // const self = this; // this.state.getByID(id).subscribe({ // next(rslt) { // if (rslt) { // self.utility.updateRslt(self.accounts, rslt) // } // }, error(e) { // }, complete() { // done() // } // }) // } post(account: Account,done:any, error:any) { const self = this; this.state.post(account).subscribe({ next(rslt) { if (rslt) { // if(self.account._id === rslt.results._id){ // self.account = rslt.results // } else { // const index = self.accounts.findIndex(f => f._id === rslt.results._id); // if(index >= 0){ // self.accounts[index] = rslt.results; // } // } done(rslt.results) } }, error(e) { error(e) }, complete() { } }) } postLinkedAccount(linked: LinkedAccount,done:any, error:any) { const self = this; this.state.postLinkedAccount(linked).subscribe({ next(rslt) { if (rslt) { // if(self.account._id === rslt.results._id){ // self.account = rslt.results // } else { // const index = self.accounts.findIndex(f => f._id === rslt.results._id); // if(index >= 0){ // self.accounts[index] = rslt.results; // } // } done(linked) } }, error(e) { error(e) }, complete() { } }) } put(account: Account, done:any, error:any) { const self = this; this.state.put(account).subscribe({ next(rslt) { if (rslt) { // if(self.account._id === rslt.results._id){ // self.account = rslt.results // } else { // const index = self.accounts.findIndex(f => f._id === rslt.results._id); // if(index >= 0){ // self.accounts[index] = rslt.results; // } // } console.log(JSON.stringify(rslt.results)) done(rslt.results) } }, error(e) { error(e) }, complete() { } }) } postUser(users: Array,done:any, error:any) { const self = this; this.state.postUser(users).subscribe({ next(rslt) { // if (rslt) { // self.utility.updateRslt(self.authUsers, rslt) // } done(rslt.results) }, error(e) { error(e) }, complete() { } }) } // delete(account: Account, done:any) { // const self = this; // this.account.delete(account).subscribe({ // next(rslt) { // if (rslt) { // rslt.results.forEach((f:Account) => { // const index = self.accounts.findIndex(fi => f._id === fi._id); // if(index >=0){ // self.accounts.splice(index,1) // } // }) // } // }, error(e) { // done(); // }, complete() { // done(); // } // }) // } }