47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
const { listCategories, listProducts } = require('../../api/productApi')
|
|
|
|
Page({
|
|
data: {
|
|
loading: false,
|
|
categories: [],
|
|
activeCategoryId: null,
|
|
products: []
|
|
},
|
|
|
|
onLoad() {
|
|
this.load()
|
|
},
|
|
|
|
load() {
|
|
this.setData({ loading: true })
|
|
Promise.all([listCategories(), listProducts()])
|
|
.then(([categories, products]) => {
|
|
this.setData({
|
|
categories: categories || [],
|
|
products: products || []
|
|
})
|
|
})
|
|
.catch((error) => wx.showToast({ title: error.message, icon: 'none' }))
|
|
.finally(() => this.setData({ loading: false }))
|
|
},
|
|
|
|
chooseCategory(event) {
|
|
const categoryId = event.currentTarget.dataset.id
|
|
this.setData({ activeCategoryId: categoryId })
|
|
listProducts(categoryId)
|
|
.then((products) => this.setData({ products: products || [] }))
|
|
.catch((error) => wx.showToast({ title: error.message, icon: 'none' }))
|
|
},
|
|
|
|
clearCategory() {
|
|
this.setData({ activeCategoryId: null })
|
|
listProducts()
|
|
.then((products) => this.setData({ products: products || [] }))
|
|
.catch((error) => wx.showToast({ title: error.message, icon: 'none' }))
|
|
},
|
|
|
|
goDetail(event) {
|
|
wx.navigateTo({ url: '/pages/products/detail?id=' + event.currentTarget.dataset.id })
|
|
}
|
|
})
|