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 [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
const [locationSearchValue, setLocationSearchValue] = useState('');
const [selectedLocation, setSelectedLocation] = useState<OptionType | null>(null);
@@ -231,20 +234,28 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
}
}, [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(() => {
if (formik.values.body_weights) {
const updatedBodyWeights = formik.values.body_weights.map((weight) => ({
...weight,
average_weight:
weight.qty > 0 && weight.weight > 0
? Math.round(weight.weight / weight.qty)
: 0,
}));
if (formik.values.body_weights && editingAverageIndex === null) {
const updatedBodyWeights = formik.values.body_weights.map((weight, idx) => {
// Skip auto-calculation for the field being manually edited
if (idx === editingAverageIndex) {
return weight;
}
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
const hasChanges = updatedBodyWeights.some(
(updated, idx) =>
idx !== editingAverageIndex && // Skip the field being edited
updated.average_weight !==
(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.qty),
editingAverageIndex, // Include editing index in dependencies
]);
// EVENT HANDLERS - Body Weights
@@ -332,10 +344,18 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
};
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;
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 updatedBodyWeights = formik.values.body_weights?.filter(
(_, i) => i !== idx
@@ -707,7 +727,10 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
name={`body_weights.${idx}.average_weight`}
value={bw.average_weight || 0}
onChange={handleAverageWeightChangeWrapper(idx)}
onBlur={formik.handleBlur}
onBlur={(e) => {
handleAverageWeightBlur();
formik.handleBlur(e);
}}
maskType='weight'
weightUnit='gram'
decimals={2}