mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
feat(FE-114): prevent auto-calculation override during manual average weight editing in RecordingForm
This commit is contained in:
@@ -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}
|
||||||
|
|||||||
Reference in New Issue
Block a user