update manual input form, block date before start date and make age on manual form fixed not editable
This commit is contained in:
1 parent
a9a2acb705
commit
0d34aa8557
3 files changed
+93
-20
No files matched your search
@@ -530,6 +530,7 @@ const Dashboard: React.FC<Props> = ({ setView }) => {
|
||||
<DashboardManualInputForm
|
||||
cycleId={selectedCycle.id}
|
||||
currentDay={selectedCycle.current_day}
|
||||
startDate={selectedCycle.start_date}
|
||||
readOnly={cycleStatus !== 'active'}
|
||||
onSaved={handleManualInputSaved}
|
||||
/>
|
||||
|
||||
@@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
|
||||
import { api, errorMessage } from '../../services/apiClient.ts';
|
||||
import type { ManualInput, ManualInputWrite } from '../../types/api.ts';
|
||||
import { Banner } from '../ui/Feedback.tsx';
|
||||
import { formatNumber, todayIso } from '../../utils/format.ts';
|
||||
import { cycleDayForDate, formatNumber, todayIso } from '../../utils/format.ts';
|
||||
|
||||
const inputClass =
|
||||
'border border-gray-300 rounded-md px-3 py-2 text-sm w-full focus:ring-red-500 focus:border-red-500';
|
||||
@@ -10,6 +10,7 @@ const inputClass =
|
||||
type Props = {
|
||||
cycleId: number;
|
||||
currentDay: number;
|
||||
startDate?: string;
|
||||
readOnly?: boolean;
|
||||
onSaved?: (savedRow: ManualInput) => void | Promise<void>;
|
||||
};
|
||||
@@ -26,9 +27,15 @@ type FormState = {
|
||||
manual_weight: number;
|
||||
};
|
||||
|
||||
const emptyForm = (currentDay: number): FormState => ({
|
||||
date: todayIso(),
|
||||
age_manual: currentDay,
|
||||
export function calculateAgeForDate(date: string, startDate?: string, fallback = 0): number {
|
||||
if (!startDate || !date) return fallback;
|
||||
const age = cycleDayForDate(startDate, date);
|
||||
return Number.isFinite(age) ? age : fallback;
|
||||
}
|
||||
|
||||
const emptyForm = (date: string = todayIso(), startDate?: string, currentDay = 0): FormState => ({
|
||||
date,
|
||||
age_manual: calculateAgeForDate(date, startDate, currentDay),
|
||||
mortality_manual: 0,
|
||||
feed_in_manual: 0,
|
||||
feed_use_manual: 0,
|
||||
@@ -56,10 +63,12 @@ function toForm(row: ManualInput): FormState {
|
||||
const DashboardManualInputForm: React.FC<Props> = ({
|
||||
cycleId,
|
||||
currentDay,
|
||||
startDate,
|
||||
readOnly = false,
|
||||
onSaved,
|
||||
}) => {
|
||||
const [form, setForm] = useState<FormState>(() => emptyForm(currentDay));
|
||||
const initialDate = startDate && todayIso() < startDate ? startDate : todayIso();
|
||||
const [form, setForm] = useState<FormState>(() => emptyForm(initialDate, startDate, currentDay));
|
||||
const [saved, setSaved] = useState<ManualInput | null>(null);
|
||||
const [rows, setRows] = useState<ManualInput[]>([]);
|
||||
const [saving, setSaving] = useState(false);
|
||||
@@ -67,17 +76,22 @@ const DashboardManualInputForm: React.FC<Props> = ({
|
||||
const [notice, setNotice] = useState<string | null>(null);
|
||||
|
||||
const load = async (date: string) => {
|
||||
if (!date) {
|
||||
setForm((prev) => ({ ...prev, date: '' }));
|
||||
return;
|
||||
}
|
||||
if (startDate && date < startDate) {
|
||||
setError(`Tanggal tidak boleh sebelum tanggal mulai siklus (${startDate}).`);
|
||||
return;
|
||||
}
|
||||
const list = await api.manualInputs.list({ cycle_id: cycleId });
|
||||
setRows(list);
|
||||
const match = list.find((row) => row.date === date) ?? null;
|
||||
setSaved(match);
|
||||
if (match) setForm(toForm(match));
|
||||
else {
|
||||
setForm((prev) => ({
|
||||
...emptyForm(currentDay),
|
||||
date,
|
||||
age_manual: prev.date === date ? prev.age_manual : currentDay,
|
||||
}));
|
||||
if (match) {
|
||||
setForm(toForm(match));
|
||||
} else {
|
||||
setForm(emptyForm(date, startDate, currentDay));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -89,10 +103,11 @@ const DashboardManualInputForm: React.FC<Props> = ({
|
||||
if (cancelled) return;
|
||||
setRows(list);
|
||||
const today = todayIso();
|
||||
const targetDate = startDate && today < startDate ? startDate : today;
|
||||
const match =
|
||||
list.find((row) => row.date === today) ?? (readOnly ? (list[0] ?? null) : null);
|
||||
list.find((row) => row.date === targetDate) ?? (readOnly ? (list[0] ?? null) : null);
|
||||
setSaved(match);
|
||||
setForm(match ? toForm(match) : emptyForm(currentDay));
|
||||
setForm(match ? toForm(match) : emptyForm(targetDate, startDate, currentDay));
|
||||
} catch (err) {
|
||||
if (!cancelled) setError(errorMessage(err));
|
||||
}
|
||||
@@ -100,11 +115,16 @@ const DashboardManualInputForm: React.FC<Props> = ({
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [cycleId, currentDay, readOnly]);
|
||||
}, [cycleId, currentDay, startDate, readOnly]);
|
||||
|
||||
const onDateChange = async (date: string) => {
|
||||
setError(null);
|
||||
setNotice(null);
|
||||
if (startDate && date < startDate) {
|
||||
setError(`Tanggal tidak boleh sebelum tanggal mulai siklus (${startDate}).`);
|
||||
setForm((prev) => ({ ...prev, date }));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await load(date);
|
||||
} catch (err) {
|
||||
@@ -118,6 +138,10 @@ const DashboardManualInputForm: React.FC<Props> = ({
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
if (readOnly) return;
|
||||
if (startDate && form.date < startDate) {
|
||||
setError(`Tanggal tidak boleh sebelum tanggal mulai siklus (${startDate}).`);
|
||||
return;
|
||||
}
|
||||
void (async () => {
|
||||
setSaving(true);
|
||||
setError(null);
|
||||
@@ -126,7 +150,7 @@ const DashboardManualInputForm: React.FC<Props> = ({
|
||||
const body: ManualInputWrite = {
|
||||
cycle: cycleId,
|
||||
date: form.date,
|
||||
age_manual: form.age_manual,
|
||||
age_manual: calculateAgeForDate(form.date, startDate, form.age_manual),
|
||||
mortality_manual: form.mortality_manual,
|
||||
feed_in_manual: form.feed_in_manual,
|
||||
feed_use_manual: form.feed_use_manual,
|
||||
@@ -173,6 +197,7 @@ const DashboardManualInputForm: React.FC<Props> = ({
|
||||
Tanggal
|
||||
<input
|
||||
type="date"
|
||||
min={startDate || undefined}
|
||||
className={`${inputClass} mt-1`}
|
||||
value={form.date}
|
||||
onChange={(e) => void onDateChange(e.target.value)}
|
||||
@@ -182,10 +207,11 @@ const DashboardManualInputForm: React.FC<Props> = ({
|
||||
Umur (hari)
|
||||
<input
|
||||
type="number"
|
||||
className={`${inputClass} mt-1`}
|
||||
className={`${inputClass} mt-1 bg-gray-100 text-gray-700 cursor-not-allowed select-none focus:ring-0 focus:border-gray-300`}
|
||||
value={form.age_manual}
|
||||
disabled={readOnly}
|
||||
onChange={(e) => setForm((f) => ({ ...f, age_manual: Number(e.target.value) }))}
|
||||
readOnly
|
||||
tabIndex={-1}
|
||||
title="Umur dihitung otomatis berdasarkan tanggal dan tidak dapat diubah manual"
|
||||
/>
|
||||
</label>
|
||||
<label className="block text-sm">
|
||||
|
||||
@@ -58,7 +58,10 @@ describe('DashboardManualInputForm', () => {
|
||||
expect(screen.queryByLabelText(/EEF/i)).not.toBeInTheDocument();
|
||||
expect(screen.queryByLabelText(/rata-rata panen/i)).not.toBeInTheDocument();
|
||||
|
||||
fireEvent.change(screen.getByLabelText(/Umur/i), { target: { value: '10' } });
|
||||
const ageInput = screen.getByLabelText(/Umur/i);
|
||||
expect(ageInput).toHaveAttribute('readOnly');
|
||||
expect(ageInput).toHaveValue(10);
|
||||
|
||||
fireEvent.change(screen.getByLabelText(/Mortalitas/i), { target: { value: '5' } });
|
||||
fireEvent.change(screen.getByLabelText(/Karung masuk/i), { target: { value: '8' } });
|
||||
fireEvent.change(screen.getByLabelText(/Karung tuang/i), { target: { value: '12' } });
|
||||
@@ -101,4 +104,47 @@ describe('DashboardManualInputForm', () => {
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('automatically calculates age_manual from startDate when user selects different dates', async () => {
|
||||
listMock.mockResolvedValue([]);
|
||||
render(<DashboardManualInputForm cycleId={4} currentDay={0} startDate="2026-10-01" />);
|
||||
|
||||
expect(await screen.findByRole('heading', { name: /Input manual/i })).toBeInTheDocument();
|
||||
|
||||
const dateInput = screen.getByLabelText(/Tanggal/i);
|
||||
const ageInput = screen.getByLabelText(/Umur/i);
|
||||
expect(ageInput).toHaveAttribute('readOnly');
|
||||
|
||||
// 1 Oktober -> Umur 0
|
||||
fireEvent.change(dateInput, { target: { value: '2026-10-01' } });
|
||||
await waitFor(() => expect(ageInput).toHaveValue(0));
|
||||
|
||||
// 2 Oktober -> Umur 1
|
||||
fireEvent.change(dateInput, { target: { value: '2026-10-02' } });
|
||||
await waitFor(() => expect(ageInput).toHaveValue(1));
|
||||
|
||||
// 3 Oktober -> Umur 2
|
||||
fireEvent.change(dateInput, { target: { value: '2026-10-03' } });
|
||||
await waitFor(() => expect(ageInput).toHaveValue(2));
|
||||
});
|
||||
|
||||
it('blocks dates before startDate via min attribute and shows error on invalid date', async () => {
|
||||
listMock.mockResolvedValue([]);
|
||||
render(<DashboardManualInputForm cycleId={4} currentDay={0} startDate="2026-10-01" />);
|
||||
|
||||
expect(await screen.findByRole('heading', { name: /Input manual/i })).toBeInTheDocument();
|
||||
|
||||
const dateInput = screen.getByLabelText(/Tanggal/i);
|
||||
expect(dateInput).toHaveAttribute('min', '2026-10-01');
|
||||
|
||||
// Attempt to select date before startDate
|
||||
fireEvent.change(dateInput, { target: { value: '2026-09-30' } });
|
||||
expect(
|
||||
await screen.findByText(/Tanggal tidak boleh sebelum tanggal mulai siklus/i)
|
||||
).toBeInTheDocument();
|
||||
|
||||
// Attempt to submit
|
||||
fireEvent.click(screen.getByRole('button', { name: /Simpan/i }));
|
||||
expect(createMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user