const { loadBreeds, groupBreeds } = require("../../utils/breeds"); Page({ data: { groups: [], query: "", loading: true, error: "", total: 0, initial: "", }, onLoad() { this.load(); }, async load() { if (this.pending) return; this.pending = true; this.setData({ loading: true, error: "" }); try { this.rows = await loadBreeds(); this.filter(); } catch (error) { this.setData({ error: error.message }); } finally { this.pending = false; this.setData({ loading: false }); } }, filter() { let groups = groupBreeds(this.rows || [], this.data.query); if (this.data.initial) groups = groups.filter((g) => g.letter === this.data.initial); this.setData({ groups, total: groups.reduce((n, g) => n + g.items.length, 0), }); }, search(e) { this.setData({ query: e.detail.value, initial: "" }); this.filter(); }, letter(e) { this.setData({ initial: e.currentTarget.dataset.letter }); this.filter(); }, open(e) { wx.navigateTo({ url: "/pages/breed-detail/breed-detail?id=" + e.currentTarget.dataset.id, }); }, });