-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathhome.ts
100 lines (76 loc) · 2.35 KB
/
home.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Component, ViewChild } from '@angular/core';
import { IonicPage, NavController, Slides, ToastController } from 'ionic-angular';
// import { ProductDetails } from '../product-details/product-details';
import * as WC from 'woocommerce-api';
import { WooCommerceProvider } from '../../providers/woocommerce/woocommerce';
// import { SearchPage } from "../search/search";
@IonicPage({})
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
WooCommerce: any;
products: any[];
moreProducts: any[];
page: number;
searchQuery: string = "";
@ViewChild('productSlides') productSlides: Slides;
constructor(public navCtrl: NavController, public toastCtrl: ToastController, private woocommerce: WooCommerceProvider) {
this.page = 2;
this.WooCommerce = WC({
url: "http://6204dcc0.ngrok.io",
consumerKey: "ck_e1c614aa631216a299e23fe592cdb72669200566",
consumerSecret: "cs_6f8c8055c349d966ece12115671f24ef9af3da63"
});
this.loadMoreProducts(null);
this.WooCommerce.getAsync("products").then( (data) => {
console.log(JSON.parse(data.body));
this.products = JSON.parse(data.body).products;
}, (err) => {
console.log(err)
})
}
ionViewDidLoad(){
setInterval(()=> {
if(this.productSlides.getActiveIndex() == this.productSlides.length() -1)
this.productSlides.slideTo(0);
this.productSlides.slideNext();
}, 3000)
}
loadMoreProducts(event){
console.log(event);
if(event == null)
{
this.page = 2;
this.moreProducts = [];
}
else
this.page++;
this.WooCommerce.getAsync("products?page=" + this.page).then( (data) => {
console.log(JSON.parse(data.body));
this.moreProducts = this.moreProducts.concat(JSON.parse(data.body).products);
if(event != null)
{
event.complete();
}
if(JSON.parse(data.body).products.length < 10){
event.enable(false);
this.toastCtrl.create({
message: "No more products!",
duration: 5000
}).present();
}
}, (err) => {
console.log(err)
})
}
openProductPage(product){
this.navCtrl.push('ProductDetails', {"product": product} );
}
onSearch(event){
if(this.searchQuery.length > 0){
this.navCtrl.push('SearchPage', {"searchQuery": this.searchQuery});
}
}
}