feat(FE-114): prevent auto-calculation override during manual average weight editing in RecordingForm

This commit is contained in:
rstubryan
2025-10-23 22:02:12 +07:00
parent ef249fee12
commit 6060ec0f7e
@@ -40,6 +40,9 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const [selectedStocks, setSelectedStocks] = useState<number[]>([]); const [selectedStocks, setSelectedStocks] = useState<number[]>([]);
const [selectedDepletions, setSelectedDepletions] = useState<number[]>([]); const [selectedDepletions, setSelectedDepletions] = useState<number[]>([]);
// Track which average weight field is being edited to prevent auto-calculation override
const [editingAverageIndex, setEditingAverageIndex] = useState<number | null>(null);
// State for Location search and selection // State for Location search and selection
const [locationSearchValue, setLocationSearchValue] = useState(''); const [locationSearchValue, setLocationSearchValue] = useState('');
const [selectedLocation, setSelectedLocation] = useState<OptionType | null>(null); const [selectedLocation, setSelectedLocation] = useState<OptionType | null>(null);
@@ -231,20 +234,28 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
} }
}, [formik.values.record_datetime]); }, [formik.values.record_datetime]);
// Auto-calculate average weight when weight or qty changes // Auto-calculate average weight when weight or qty changes (but not when editing average weight manually)
useEffect(() => { useEffect(() => {
if (formik.values.body_weights) { if (formik.values.body_weights && editingAverageIndex === null) {
const updatedBodyWeights = formik.values.body_weights.map((weight) => ({ const updatedBodyWeights = formik.values.body_weights.map((weight, idx) => {
...weight, // Skip auto-calculation for the field being manually edited
average_weight: if (idx === editingAverageIndex) {
weight.qty > 0 && weight.weight > 0 return weight;
? Math.round(weight.weight / weight.qty) }
: 0,
})); return {
...weight,
average_weight:
weight.qty > 0 && weight.weight > 0
? Math.round(weight.weight / weight.qty)
: 0,
};
});
// Only update if values are different to avoid infinite loops // Only update if values are different to avoid infinite loops
const hasChanges = updatedBodyWeights.some( const hasChanges = updatedBodyWeights.some(
(updated, idx) => (updated, idx) =>
idx !== editingAverageIndex && // Skip the field being edited
updated.average_weight !== updated.average_weight !==
(formik.values.body_weights[idx]?.average_weight || 0) (formik.values.body_weights[idx]?.average_weight || 0)
); );
@@ -256,6 +267,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
}, [ }, [
formik.values.body_weights?.map((w) => w.weight), formik.values.body_weights?.map((w) => w.weight),
formik.values.body_weights?.map((w) => w.qty), formik.values.body_weights?.map((w) => w.qty),
editingAverageIndex, // Include editing index in dependencies
]); ]);
// EVENT HANDLERS - Body Weights // EVENT HANDLERS - Body Weights
@@ -332,10 +344,18 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
}; };
const handleAverageWeightChangeWrapper = (idx: number) => (e: React.ChangeEvent<HTMLInputElement>) => { const handleAverageWeightChangeWrapper = (idx: number) => (e: React.ChangeEvent<HTMLInputElement>) => {
// Set focus state to prevent auto-calculation override
setEditingAverageIndex(idx);
const value = parseFloat(e.target.value.replace(/[^\d,.-]/g, '').replace(/,/g, '')) || 0; const value = parseFloat(e.target.value.replace(/[^\d,.-]/g, '').replace(/,/g, '')) || 0;
handleAverageWeightChange(idx, value); handleAverageWeightChange(idx, value);
}; };
const handleAverageWeightBlur = () => {
// Clear focus state when user leaves the field to re-enable auto-calculation
setEditingAverageIndex(null);
};
const removeBodyWeight = (idx: number) => { const removeBodyWeight = (idx: number) => {
const updatedBodyWeights = formik.values.body_weights?.filter( const updatedBodyWeights = formik.values.body_weights?.filter(
(_, i) => i !== idx (_, i) => i !== idx
@@ -707,7 +727,10 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
name={`body_weights.${idx}.average_weight`} name={`body_weights.${idx}.average_weight`}
value={bw.average_weight || 0} value={bw.average_weight || 0}
onChange={handleAverageWeightChangeWrapper(idx)} onChange={handleAverageWeightChangeWrapper(idx)}
onBlur={formik.handleBlur} onBlur={(e) => {
handleAverageWeightBlur();
formik.handleBlur(e);
}}
maskType='weight' maskType='weight'
weightUnit='gram' weightUnit='gram'
decimals={2} decimals={2}