Complete core mini program flows
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
const { secure, toast } = require("../../utils/api");
|
||||
const today = () => new Date().toLocaleDateString("sv-SE");
|
||||
const { localDate, shiftDate } = require("../../utils/date");
|
||||
Page({
|
||||
data: {
|
||||
pets: [],
|
||||
tasks: [],
|
||||
date: today(),
|
||||
date: localDate(),
|
||||
showForm: false,
|
||||
editingId: "",
|
||||
saving: false,
|
||||
title: "",
|
||||
petIndex: 0,
|
||||
category: "food",
|
||||
@@ -17,11 +19,13 @@ Page({
|
||||
this.load();
|
||||
},
|
||||
async load() {
|
||||
const date = this.data.date;
|
||||
try {
|
||||
const [pets, result] = await Promise.all([
|
||||
secure("/pets"),
|
||||
secure("/tasks?date=" + this.data.date),
|
||||
secure("/tasks?date=" + date),
|
||||
]);
|
||||
if (date !== this.data.date) return;
|
||||
const tasks = result.items.map((t) => ({
|
||||
...t,
|
||||
petName: (pets.find((p) => p.id === t.pet_id) || {}).name || "宠物",
|
||||
@@ -42,17 +46,50 @@ Page({
|
||||
date: this.data.date,
|
||||
completed: !t.completed,
|
||||
});
|
||||
this.load();
|
||||
await this.load();
|
||||
} catch (err) {
|
||||
toast(err);
|
||||
}
|
||||
},
|
||||
open() {
|
||||
this.setData({ showForm: true });
|
||||
if (!this.data.pets.length) {
|
||||
wx.showToast({ title: "请先添加宠物", icon: "none" });
|
||||
return;
|
||||
}
|
||||
this.setData({
|
||||
showForm: true,
|
||||
editingId: "",
|
||||
title: "",
|
||||
petIndex: 0,
|
||||
category: "food",
|
||||
time_of_day: "08:30",
|
||||
repeat_rule: "daily",
|
||||
});
|
||||
},
|
||||
close() {
|
||||
this.setData({ showForm: false });
|
||||
},
|
||||
edit(e) {
|
||||
const task = this.data.tasks[e.currentTarget.dataset.index];
|
||||
const petIndex = this.data.pets.findIndex((pet) => pet.id === task.pet_id);
|
||||
this.setData({
|
||||
showForm: true,
|
||||
editingId: task.id,
|
||||
title: task.title,
|
||||
petIndex: Math.max(petIndex, 0),
|
||||
category: task.category,
|
||||
time_of_day: task.time_of_day,
|
||||
repeat_rule: task.repeat_rule,
|
||||
});
|
||||
},
|
||||
changeDay(e) {
|
||||
this.setData({ date: shiftDate(this.data.date, Number(e.currentTarget.dataset.days)) });
|
||||
this.load();
|
||||
},
|
||||
showToday() {
|
||||
this.setData({ date: localDate() });
|
||||
this.load();
|
||||
},
|
||||
field(e) {
|
||||
this.setData({ [e.currentTarget.dataset.key]: e.detail.value });
|
||||
},
|
||||
@@ -69,22 +106,26 @@ Page({
|
||||
this.setData({ repeat_rule: e.detail.value });
|
||||
},
|
||||
async save() {
|
||||
if (this.data.saving) return;
|
||||
if (!this.data.pets.length || !this.data.title.trim()) {
|
||||
wx.showToast({ title: "请选择宠物并填写任务名", icon: "none" });
|
||||
return;
|
||||
}
|
||||
this.setData({ saving: true });
|
||||
try {
|
||||
await secure("/tasks", "POST", {
|
||||
await secure(this.data.editingId ? "/tasks/" + this.data.editingId : "/tasks", this.data.editingId ? "PUT" : "POST", {
|
||||
pet_id: this.data.pets[this.data.petIndex].id,
|
||||
title: this.data.title,
|
||||
title: this.data.title.trim(),
|
||||
category: this.data.category,
|
||||
time_of_day: this.data.time_of_day,
|
||||
repeat_rule: this.data.repeat_rule,
|
||||
});
|
||||
this.setData({ showForm: false, title: "" });
|
||||
this.load();
|
||||
this.setData({ showForm: false, editingId: "" });
|
||||
await this.load();
|
||||
} catch (e) {
|
||||
toast(e);
|
||||
} finally {
|
||||
this.setData({ saving: false });
|
||||
}
|
||||
},
|
||||
async remove(e) {
|
||||
|
||||
Reference in New Issue
Block a user