fix: local data before sending to server
This commit is contained in:
@@ -13,7 +13,7 @@ import { updateTask } from "@/api/tasks";
|
||||
const CronInput = defineAsyncComponent(
|
||||
() => import("@/components/CronInput.vue"),
|
||||
);
|
||||
import { taskCompleted, taskNextReset } from "@/lib/tasks";
|
||||
import { isTemporary, taskCompleted, taskNextReset } from "@/lib/tasks";
|
||||
import { relativeTime } from "@/lib/dates";
|
||||
import { useAlertStore } from "@/stores/alerts";
|
||||
import { useI18n } from "vue-i18n";
|
||||
@@ -38,10 +38,12 @@ const i18n = useI18n();
|
||||
const { alertSuccess, alertError } = useAlertStore();
|
||||
|
||||
async function onOpen() {
|
||||
if (!isTemporary(task.value)) {
|
||||
editMode.value = true;
|
||||
await nextTick();
|
||||
editNameInput.value?.focus();
|
||||
}
|
||||
}
|
||||
|
||||
function onClose() {
|
||||
editName.value = task.value.name;
|
||||
@@ -51,10 +53,14 @@ function onClose() {
|
||||
|
||||
function onSave() {
|
||||
if (editName.value.trim()) {
|
||||
updateTask(task.value.id, {
|
||||
const data = {
|
||||
name: editName.value.trim(),
|
||||
reset_cron: editCron.value,
|
||||
})
|
||||
};
|
||||
task.value.name = editName.value.trim();
|
||||
task.value.reset_cron = editCron.value;
|
||||
editMode.value = false;
|
||||
updateTask(task.value.id, data)
|
||||
.then((newTask) => {
|
||||
task.value = newTask;
|
||||
emit("updated", newTask);
|
||||
@@ -63,47 +69,56 @@ function onSave() {
|
||||
.catch(() => {
|
||||
alertError(i18n.t("alerts.task_update_error"));
|
||||
});
|
||||
task.value.name = editName.value;
|
||||
task.value.reset_cron = editCron.value;
|
||||
editMode.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function onCheck() {
|
||||
if (!isTemporary(task.value)) {
|
||||
if (!checked.value) {
|
||||
updateTask(task.value.id, {
|
||||
const data = {
|
||||
check_date: new Date().toISOString(),
|
||||
previous_check_date: task.value.check_date?.toISOString(),
|
||||
})
|
||||
.then((newTask) => {
|
||||
task.value = newTask;
|
||||
emit("updated", newTask);
|
||||
};
|
||||
task.value.previous_check_date = task.value.check_date;
|
||||
task.value.check_date = new Date();
|
||||
updateTask(task.value.id, data)
|
||||
.then((data) => {
|
||||
task.value = data;
|
||||
emit("updated", data);
|
||||
})
|
||||
.catch(() => {
|
||||
alertError(i18n.t("alerts.task_update_error"));
|
||||
});
|
||||
} else {
|
||||
updateTask(task.value.id, {
|
||||
const data = {
|
||||
check_date: null,
|
||||
previous_check_date: task.value.check_date?.toISOString(),
|
||||
})
|
||||
.then((newTask) => {
|
||||
task.value = newTask;
|
||||
emit("updated", newTask);
|
||||
};
|
||||
task.value.previous_check_date = task.value.check_date;
|
||||
task.value.check_date = null;
|
||||
updateTask(task.value.id, data)
|
||||
.then((data) => {
|
||||
task.value = data;
|
||||
emit("updated", data);
|
||||
})
|
||||
.catch(() => {
|
||||
alertError(i18n.t("alerts.task_update_error"));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function onClone() {
|
||||
if (!isTemporary(task.value)) {
|
||||
emit("clone", task.value);
|
||||
}
|
||||
}
|
||||
|
||||
function onDelete() {
|
||||
if (!isTemporary(task.value)) {
|
||||
emit("delete", task.value);
|
||||
}
|
||||
}
|
||||
|
||||
onBeforeMount(() => {
|
||||
editName.value = task.value.name;
|
||||
@@ -112,6 +127,7 @@ onBeforeMount(() => {
|
||||
refreshKey.value++;
|
||||
}, 60000);
|
||||
});
|
||||
|
||||
watch(task, () => {
|
||||
if (!editMode.value && task.value.name !== editName.value) {
|
||||
editName.value = task.value.name;
|
||||
@@ -131,7 +147,7 @@ watch(task, () => {
|
||||
type="checkbox"
|
||||
class="checkbox"
|
||||
:checked="checked"
|
||||
@click.prevent="onCheck"
|
||||
@click="onCheck"
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { SortType } from "@/enums";
|
||||
import type { RawTask, Task } from "@/types";
|
||||
import type { RawTask, Task, TaskCreateData } from "@/types";
|
||||
import { CronExpressionParser } from "cron-parser";
|
||||
|
||||
const TEMPORARY_ID = "tmp";
|
||||
|
||||
export function parseTask(task: RawTask): Task {
|
||||
return {
|
||||
id: task.id,
|
||||
@@ -16,6 +18,22 @@ export function parseTask(task: RawTask): Task {
|
||||
};
|
||||
}
|
||||
|
||||
export function newTemporaryTask(data: TaskCreateData): Task {
|
||||
return {
|
||||
id: TEMPORARY_ID,
|
||||
name: data.name,
|
||||
reset_cron: data.reset_cron,
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
check_date: null,
|
||||
previous_check_date: null,
|
||||
}
|
||||
}
|
||||
|
||||
export function isTemporary(task: Task): boolean {
|
||||
return task.id === TEMPORARY_ID;
|
||||
}
|
||||
|
||||
export function taskNextReset(task: Task): Date | null {
|
||||
if (task.check_date === null || task.reset_cron === null) {
|
||||
return null;
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
const TaskItem = defineAsyncComponent(
|
||||
() => import("@/components/TaskItem.vue"),
|
||||
);
|
||||
import { taskCompleted, sortTasks } from "@/lib/tasks";
|
||||
import { taskCompleted, sortTasks, newTemporaryTask } from "@/lib/tasks";
|
||||
import { SortType } from "@/enums";
|
||||
import { booleanCookieRef, enumCookieRef } from "@/lib/cookies";
|
||||
import { useAlertStore } from "@/stores/alerts";
|
||||
@@ -88,13 +88,16 @@ function reSortTasks(update = false) {
|
||||
|
||||
function onNewTask() {
|
||||
if (taskInput.value.trim() && list.value && !loading.value) {
|
||||
createTask(list.value, {
|
||||
const data = {
|
||||
name: taskInput.value.trim(),
|
||||
reset_cron: null,
|
||||
})
|
||||
.then((data: Task) => {
|
||||
tasks.value.push(data);
|
||||
};
|
||||
const task = newTemporaryTask(data);
|
||||
tasks.value.push(task);
|
||||
reSortTasks(true);
|
||||
createTask(list.value, data)
|
||||
.then((data: Task) => {
|
||||
task.id = data.id;
|
||||
alertSuccess(i18n.t("alerts.task_created"));
|
||||
})
|
||||
.catch(() => {
|
||||
@@ -112,12 +115,16 @@ function onUpdateTask() {
|
||||
|
||||
function onCloneTask(task: Task) {
|
||||
if (list.value) {
|
||||
createTask(list.value, {
|
||||
const data = {
|
||||
name: task.name,
|
||||
reset_cron: task.reset_cron,
|
||||
})
|
||||
};
|
||||
const newTask = newTemporaryTask(data);
|
||||
tasks.value.push(newTask);
|
||||
reSortTasks(true);
|
||||
createTask(list.value, data)
|
||||
.then((data: Task) => {
|
||||
tasks.value.push(data);
|
||||
task.id = data.id;
|
||||
reSortTasks(true);
|
||||
alertSuccess(i18n.t("alerts.task_cloned"));
|
||||
})
|
||||
@@ -128,16 +135,15 @@ function onCloneTask(task: Task) {
|
||||
}
|
||||
|
||||
function onDeleteTask(task: Task) {
|
||||
tasks.value.splice(tasks.value.indexOf(task), 1);
|
||||
reSortTasks(true);
|
||||
deleteTask(task.id)
|
||||
.then(() => {
|
||||
reSortTasks(true);
|
||||
alertSuccess(i18n.t("alerts.task_deleted"));
|
||||
})
|
||||
.catch(() => {
|
||||
alertError(i18n.t("alerts.task_delete_error"));
|
||||
});
|
||||
|
||||
tasks.value.splice(tasks.value.indexOf(task), 1);
|
||||
}
|
||||
|
||||
function onChangeSortType() {
|
||||
|
||||
Reference in New Issue
Block a user