2022-01-25 08:31:06 +01:00
|
|
|
import React, { useState, useEffect } from 'react'
|
2022-02-07 20:02:28 +01:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-01-22 17:55:03 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 }) => {
|
2022-02-07 20:02:28 +01:00
|
|
|
const { t } = useTranslation(['app', 'measurements'])
|
2022-01-22 17:55:03 +01:00
|
|
|
const prefix = (app.site === 'org') ? '' : 'https://freesewing.org'
|
2022-02-10 21:40:19 +01:00
|
|
|
const title = t(`measurements:${m}`)
|
2022-01-22 17:55:03 +01:00
|
|
|
const isValid = input => {
|
2022-01-25 10:03:10 +01:00
|
|
|
if (input === '') return ''
|
2022-01-22 17:55:03 +01:00
|
|
|
return !isNaN(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
const update = evt => {
|
|
|
|
setVal(evt.target.value)
|
|
|
|
const ok = isValid(evt.target.value)
|
|
|
|
console.log({ok})
|
|
|
|
if (ok) {
|
|
|
|
setValid(true)
|
2022-01-25 08:31:06 +01:00
|
|
|
updateMeasurements(evt.target.value*10, m)
|
2022-01-22 17:55:03 +01:00
|
|
|
} else setValid(false)
|
|
|
|
}
|
|
|
|
|
2022-01-25 10:03:10 +01:00
|
|
|
const [val, setVal] = useState(gist?.measurements?.[m] || '')
|
2022-01-22 17:55:03 +01:00
|
|
|
const [valid, setValid] = useState(typeof gist?.measurements?.[m] === 'undefined'
|
2022-01-25 10:03:10 +01:00
|
|
|
? '' :
|
2022-01-22 17:55:03 +01:00
|
|
|
isValid(gist.measurements[m])
|
|
|
|
)
|
|
|
|
|
2022-01-25 08:31:06 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (gist?.measurements?.[m]) setVal(gist.measurements[m]/10)
|
|
|
|
}, [gist])
|
|
|
|
|
2022-01-22 17:55:03 +01:00
|
|
|
if (!m) return null
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="form-control mb-2" key={`wrap-${m}`}>
|
|
|
|
<label className="label">
|
|
|
|
<span className="label-text font-bold text-xl">{title}</span>
|
|
|
|
<a
|
|
|
|
href={`${prefix}/docs/measurements/${m.toLowerCase()}`}
|
|
|
|
className="label-text-alt text-secondary hover:text-secondary-focus hover:underline"
|
2022-02-07 20:02:28 +01:00
|
|
|
title={`${t('docs')}: ${t(m)}`}
|
2022-01-22 17:55:03 +01:00
|
|
|
tabIndex="-1"
|
|
|
|
>
|
2022-02-07 20:02:28 +01:00
|
|
|
{t('docs')}
|
2022-01-22 17:55:03 +01:00
|
|
|
</a>
|
|
|
|
</label>
|
|
|
|
<label className="input-group input-group-lg">
|
|
|
|
<input
|
|
|
|
key={`input-${m}`}
|
|
|
|
type="text"
|
|
|
|
placeholder={title}
|
|
|
|
className={`
|
|
|
|
input input-lg input-bordered grow text-base-content
|
|
|
|
${valid === false && 'input-error'}
|
|
|
|
${valid === true && 'input-success'}
|
2022-02-10 21:40:19 +01:00
|
|
|
border-r-0
|
2022-01-22 17:55:03 +01:00
|
|
|
`}
|
|
|
|
value={val}
|
|
|
|
onChange={update}
|
|
|
|
/>
|
2022-02-10 21:40:19 +01:00
|
|
|
<span role="img" className={`bg-transparent border-y
|
|
|
|
${valid === false && 'border-error text-neutral-content'}
|
|
|
|
${valid === true && 'border-success text-neutral-content'}
|
|
|
|
${valid === '' && 'border-base-200 text-base-content'}
|
|
|
|
`}>
|
|
|
|
{valid === ''
|
|
|
|
? ''
|
|
|
|
: valid
|
|
|
|
? '👍'
|
|
|
|
: '🤔'
|
|
|
|
}
|
|
|
|
</span>
|
2022-01-22 17:55:03 +01:00
|
|
|
<span className={`
|
|
|
|
${valid === false && 'bg-error text-neutral-content'}
|
|
|
|
${valid === true && 'bg-success text-neutral-content'}
|
2022-01-25 10:03:10 +01:00
|
|
|
${valid === '' && 'bg-base-200 text-base-content'}
|
2022-01-22 17:55:03 +01:00
|
|
|
`}>
|
|
|
|
cm
|
|
|
|
</span>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MeasurementInput
|
|
|
|
|