import React, { useState, useEffect } from 'react' import { useTranslation } from 'next-i18next' import { isDegreeMeasurement } from '../../../config/measurements' /* * This is a single input for a measurements * Note that it keeps local state with whatever the user types * but will only trigger a gist update if the input is valid. * * m holds the measurement name. It's just so long to type * measurement and I always have some typo in it because dyslexia. */ const MeasurementInput = ({ m, gist, app, updateMeasurements }) => { const { t } = useTranslation(['app', 'measurements']) const prefix = (app.site === 'org') ? '' : 'https://freesewing.org' const title = t(`measurements:${m}`) const factor = isDegreeMeasurement(m) ? 1 : 10 const isValValid = val => (typeof val === 'undefined' || val === '') ? null : !isNaN(val) const isValid = (newVal) => (typeof newVal === 'undefined') ? isValValid(gist?.measurements?.[m]) : isValValid(newVal) const update = evt => { setVal(evt.target.value) const ok = isValid(evt.target.value) if (ok) updateMeasurements(evt.target.value*factor, m) } const [val, setVal] = useState(gist?.measurements?.[m] || '') useEffect(() => { if (gist?.measurements?.[m]) setVal(gist.measurements[m]/factor) }, [gist]) if (!m) return null const valid = isValid() return (