import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/operator/map'; import { NavController } from 'ionic-angular'; // import { ProductsProvider, StoreProvider } from '../../services'; import { Store, Product } from '../../classes'; import { ProductsProvider } from '../product/products.provider'; import { StoreProvider } from '../store/store.provider'; import { BarcodeScanner } from '@ionic-native/barcode-scanner'; import 'rxjs/add/operator/map'; import * as _ from 'lodash'; // UNTESTED /* Generated class for the ProductsService provider. See https://angular.io/docs/ts/latest/guide/dependency-injection.html for more info on providers and Angular 2 DI. */ export class BarcodeData { constructor ( public text: String, public format: String ) {} } @Injectable() export class BarcodeProvider { data: any; products: any; store: any; isLandScape: boolean; scannedStore: any; scannedData: any; scannedID: any; constructor (public http: Http, public navController: NavController, public productsProvider: ProductsProvider, public storeProvider: StoreProvider, private barcodeScanner: BarcodeScanner) { this.data = null; } // TODO REMOVE EXAMPLE store_3?1?WonderPen %store?%id?%name click(Page: any) { this.barcodeScanner.scan() .then((result) => { if (!result.cancelled) { const barcodeData = new BarcodeData(result.text, result.format); // this.scanDetails(barcodeData); this.load ( barcodeData.text, Page); } }) .catch((err) => { alert(err); }); } // scanDetails(details: any) { // this.load(details.text); // } load (scannedData: String, Page: any) { const values = scannedData.split('?'); // don't have the data yet return new Promise(resolve => { // We're using Angular Http provider to request the data, // then on the response it'll map the JSON data to a parsed JS object. // Next we process the data and resolve the promise with the new data. // var store = JSON.parse(localStorage.getItem("logged")).store; let tenantId = values[0]; let storeId = values[1]; let productId = values[2]; this.storeProvider.retrieveByTenant(tenantId).then( (stores: Store[]) => { let store: Store = _.find(stores, function(o) { return o.id === storeId } ); this.productsProvider.retrieve(tenantId, storeId).then( (products: Product[]) => { let product = this.productsProvider.getProduct(productId); // this.navController.push(Page, { prod: product, store: store }); } ); } ); // load products fo said store/tenant and grab the product by ID // this.http.get ('error' + values[0] + '/products') // .map(res => res.json()) // .subscribe(dataHere => { // // we've got back the raw data, now generate the core schedule data // // and save the data for later reference // this.scannedData = dataHere; // resolve(this.scannedData); // for (let i = 0; i < this.scannedData.length; i++) { // if (this.scannedData[i].id === parseInt(values[1])) { // this.navController.push(ProductDetailsPage, { // prod: this.scannedData[i], store: values[0] // }); // break; // } // } // // // }); }); } }