import React, { useState, useEffect } from 'react' import { useTranslation } from 'next-i18next' /* * 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 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) { setValid(true) updateMeasurements(evt.target.value*10, m) } else setValid(false) } const [val, setVal] = useState(gist?.measurements?.[m] || '') useEffect(() => { if (gist?.measurements?.[m]) setVal(gist.measurements[m]/10) }, [gist]) if (!m) return null const valid = isValid() return (