Files
Blizzard cb4ffa13e9
ci-cd / verify (push) Successful in 1m31s
ci-cd / deploy (push) Skipped
Add one-way task completion and exportable pet identity cards
2026-10-05 11:32:32 +08:00

74 lines
2.2 KiB
JavaScript

const { secure, toast } = require("./api");
const { localDate } = require("./date");
function apply(page, ids) {
const tasks = page.data.tasks.map((task) =>
ids.includes(task.id) ? { ...task, completed: true } : task,
);
page.setData({
tasks,
done: tasks.filter((t) => t.completed).length,
undoneCount: tasks.filter((t) => !t.completed).length,
});
if (wx.vibrateShort) wx.vibrateShort({ type: "light", fail() {} });
}
async function complete(page, task) {
if (
!task ||
task.completed ||
page.data.completingAll ||
page.data.taskBusyIds.length
)
return;
const date = page.data.date || localDate();
page.setData({ taskBusyIds: [task.id] });
try {
await secure("/tasks/" + task.id + "/completion", "PUT", {
date,
completed: true,
});
if (page.data.date && page.data.date !== date) return;
apply(page, [task.id]);
wx.showToast({ title: "照顾好了,记下一份陪伴", icon: "success" });
} catch (error) {
toast(error);
} finally {
page.setData({ taskBusyIds: [] });
}
}
async function completeAll(page) {
if (page.data.completingAll || page.data.taskBusyIds.length) return;
const tasks = page.data.tasks.filter((t) => !t.completed);
if (!tasks.length) return;
const date = page.data.date || localDate();
page.setData({ completingAll: true });
try {
const confirmation = await wx.showModal({
title: "都照顾好了吗?",
content: `将 ${date} 的 ${tasks.length} 项待办记为完成,完成后不能撤销。`,
confirmText: "全部完成",
confirmColor: "#b95738",
});
if (!confirmation.confirm) return;
// The API accepts bounded batches; avoid partial completion of larger lists.
if (tasks.length > 200) throw new Error("任务较多,请分项完成后再试");
await secure("/tasks/completions", "PUT", {
date,
task_ids: tasks.map((t) => t.id),
});
if (page.data.date && page.data.date !== date) return;
apply(
page,
tasks.map((t) => t.id),
);
wx.showToast({ title: "日常照顾全部完成", icon: "success" });
} catch (error) {
toast(error);
} finally {
page.setData({ completingAll: false });
}
}
module.exports = { complete, completeAll };