fix(date): auto detect start_date & end_date

This commit is contained in:
2026-06-25 13:39:48 +07:00
parent c22000437b
commit 51e2fa46bf

View File

@@ -183,20 +183,50 @@ const DetailDevice = (props) => {
});
useEffect(() => {
if (period.period !== 'custom') {
setFormData((prev) => ({
...prev,
start_date: null,
end_date: null,
}));
if (formData.start_date && formData.end_date) {
const start = dayjs(formData.start_date);
const end = dayjs(formData.end_date);
let periodValue = 'custom';
if (end.isSame(start.add(1, 'month').subtract(1, 'day'), 'day')) {
periodValue = '1';
} else if (end.isSame(start.add(3, 'month').subtract(1, 'day'), 'day')) {
periodValue = '3';
} else if (end.isSame(start.add(6, 'month').subtract(1, 'day'), 'day')) {
periodValue = '6';
}
setPeriod({
period: periodValue,
});
}
}, [period.period]);
}, [formData.start_date, formData.end_date]);
const handleMonthly = (field, value) => {
setPeriod((prev) => ({
...prev,
[field]: value,
}));
if (value !== 'custom') {
const months = Number(value);
const startDate = dayjs().startOf('day');
const endDate = startDate.add(months, 'month').subtract(1, 'day').endOf('day');
setFormData((prev) => ({
...prev,
start_date: startDate.toISOString(),
end_date: endDate.toISOString(),
}));
} else {
setFormData((prev) => ({
...prev,
start_date: null,
end_date: null,
}));
}
};
useEffect(() => {
@@ -464,6 +494,11 @@ const DetailDevice = (props) => {
onChange={(date) =>
handleDateChange('start_date', date ? date.toISOString() : null)
}
disabledDate={(current) =>
formData.end_date &&
current &&
current.isAfter(dayjs(formData.end_date), 'day')
}
style={{ flex: 1 }}
/>