1
0
Fork 0

fix measurement gist issues with better state management

This commit is contained in:
Enoch Riese 2022-06-22 15:19:07 -05:00
parent 1dae0a77b3
commit 347554f3a1
6 changed files with 78 additions and 64 deletions

View file

@ -17,16 +17,16 @@ const MeasurementInput = ({ m, gist, app, updateMeasurements }) => {
const title = t(`measurements:${m}`)
const isDegree = isDegreeMeasurement(m);
const factor = useMemo(() => (isDegree ? 1 : (gist?.units == 'imperial' ? 25.4 : 10)), [gist?.units])
const factor = useMemo(() => (isDegree ? 1 : (gist.units == 'imperial' ? 25.4 : 10)), [gist.units])
const isValValid = val => (typeof val === 'undefined' || val === '')
? null
: val !== false && !isNaN(val)
: val != false && !isNaN(val)
const isValid = (newVal) => (typeof newVal === 'undefined')
? isValValid(val)
: isValValid(newVal)
const [val, setVal] = useState(gist?.measurements?.[m] / factor || '')
const [val, setVal] = useState(gist.measurements?.[m] / factor || '')
// keep a single reference to a debounce timer
const debounceTimeout = useRef(null);
@ -38,7 +38,7 @@ const MeasurementInput = ({ m, gist, app, updateMeasurements }) => {
// set Val immediately so that the input reflects it
setVal(evtVal)
let useVal = isDegree ? evtVal : measurementAsMm(evtVal, gist?.units);
let useVal = isDegree ? evtVal : measurementAsMm(evtVal, gist.units);
const ok = isValid(useVal)
// only set to the gist if it's valid
if (ok) {
@ -50,14 +50,14 @@ const MeasurementInput = ({ m, gist, app, updateMeasurements }) => {
updateMeasurements(useVal, m)
}, 500);
}
}, [gist?.units])
}, [gist.units])
// use this for better update efficiency
// FIXME: This breaks gist updates.
// See: https://github.com/freesewing/freesewing/issues/2281
const memoVal = gist?.measurements?.[m] //useMemo(() => gist?.measurements?.[m], [gist])
const memoVal = useMemo(() => gist.measurements?.[m], [gist])
// track validity against the value and the units
const valid = useMemo(() => isValid(isDegree ? val : measurementAsMm(val, gist?.units)), [val, gist?.units])
const valid = useMemo(() => isValid(isDegree ? val : measurementAsMm(val, gist.units)), [val, gist.units])
// hook to update the value or format when the gist changes
useEffect(() => {