Files
CRM_Montzar/apps/web/src/lib/useRoutineTaskActions.ts
T

132 lines
4.2 KiB
TypeScript
Raw Normal View History

import { reactive, ref } from 'vue';
import type { RoutineTaskDto } from '@mento/shared';
import { api } from '@/lib/api';
import { apiError, isoToLocalInput, localInputToIso, todayKey } from '@/lib/routines';
export type RoutineTaskModalKind = 'complete' | 'skip' | 'reschedule' | 'cancel';
/**
* اکشن‌های مشترک روی یک Task (تیک، رد، جابه‌جایی، لغو) + وضعیت مودال‌ها.
* `onChanged` بعد از هر تغییر موفق با Task به‌روزشده صدا زده می‌شود.
*/
export function useRoutineTaskActions(onChanged: (task: RoutineTaskDto) => void) {
const busyId = ref<string | null>(null);
const error = ref('');
const modal = reactive({
kind: null as RoutineTaskModalKind | null,
task: null as RoutineTaskDto | null,
note: '',
date: '',
time: '',
saving: false,
error: '',
});
async function run(task: RoutineTaskDto, path: string, body: object = {}) {
busyId.value = task.id;
error.value = '';
try {
const { data } = await api.post<RoutineTaskDto>(`/routine-tasks/${task.id}/${path}`, body);
onChanged(data);
return data;
} catch (e) {
error.value = apiError(e, 'انجام عملیات ممکن نشد.');
return null;
} finally {
busyId.value = null;
}
}
function open(kind: RoutineTaskModalKind, task: RoutineTaskDto) {
const due = isoToLocalInput(task.dueAt);
Object.assign(modal, {
kind,
task,
note: kind === 'complete' ? (task.resultNote ?? '') : '',
date: kind === 'reschedule' ? due.date || todayKey() : '',
time: kind === 'reschedule' ? (due.time === '23:59' ? '' : due.time) : '',
saving: false,
error: '',
});
}
function close() {
modal.kind = null;
modal.task = null;
}
/** تیک یک‌کلیکی؛ اگر گزارش اجباری باشد مودال نتیجه باز می‌شود */
async function toggle(task: RoutineTaskDto) {
if (busyId.value) return;
if (task.status === 'completed') return run(task, 'uncomplete');
if (task.status === 'cancelled' || task.status === 'skipped') return null;
if (task.rules.reportRequired) {
open('complete', task);
return null;
}
return run(task, 'complete');
}
async function submit() {
const task = modal.task;
if (!task || !modal.kind) return;
modal.error = '';
let path = '';
let body: object = {};
if (modal.kind === 'complete') {
if (task.rules.reportRequired && !modal.note.trim()) {
modal.error = 'ثبت گزارش برای این کار الزامی است.';
return;
}
path = 'complete';
body = { note: modal.note.trim() || null };
} else if (modal.kind === 'skip') {
if (task.rules.skipReasonRequired && !modal.note.trim()) {
modal.error = 'ثبت دلیل رد کردن الزامی است.';
return;
}
path = 'skip';
body = { reason: modal.note.trim() || null };
} else if (modal.kind === 'cancel') {
path = 'cancel';
body = { reason: modal.note.trim() || null };
} else {
const dueAt = localInputToIso(modal.date, modal.time);
if (!dueAt) {
modal.error = 'تاریخ جدید را انتخاب کنید.';
return;
}
path = 'reschedule';
body = { dueAt };
}
modal.saving = true;
try {
const { data } = await api.post<RoutineTaskDto>(`/routine-tasks/${task.id}/${path}`, body);
onChanged(data);
close();
} catch (e) {
modal.error = apiError(e, 'ذخیره ممکن نشد.');
} finally {
modal.saving = false;
}
}
return {
busyId,
error,
modal,
toggle,
start: (task: RoutineTaskDto) => run(task, 'start'),
moveToday: (task: RoutineTaskDto) => run(task, 'move-today'),
openComplete: (task: RoutineTaskDto) => open('complete', task),
openSkip: (task: RoutineTaskDto) => open('skip', task),
openReschedule: (task: RoutineTaskDto) => open('reschedule', task),
openCancel: (task: RoutineTaskDto) => open('cancel', task),
close,
submit,
};
}
export type RoutineTaskActions = ReturnType<typeof useRoutineTaskActions>;