import { isDegreeMeasurement } from 'config/measurements.mjs' import { measurementAsMm, formatMm } from 'shared/utils.mjs' import { Collapse } from 'shared/components/collapse.mjs' import { PlusIcon, EditIcon } from 'shared/components/icons.mjs' import { useState } from 'react' export const ns = ['account'] const Mval = ({ m, val = false, imperial = false, className = '' }) => val ? ( isDegreeMeasurement(m) ? ( {val}° ) : ( ) ) : null const heightClasses = { 2: 'h-12', 4: 'h-10', 8: 'h-8', 16: 'h-6', 32: 'h-4', } const fractionClasses = 'w-full border-2 border-solid border-base-100 hover:border-secondary bg-secondary rounded bg-opacity-50 hover:bg-opacity-100' const FractionButtons = ({ t, fraction }) => (
{t('fractions')}
{Array.from({ length: 31 }, (_null, i) => { let denom = 32 let num = i + 1 for (let n = 4; n > 0; n--) { const fac = Math.pow(2, n) if (num % fac === 0) { denom = 32 / fac num = num / fac break } } return (
) export const MeasieRow = (props) => { const { t, m, mset } = props const isSet = typeof mset.measies?.[m] !== 'undefined' return (
{t(m)}
{isSet ? ( ) : (
)} } toggle={isSet ? : } toggleClasses={`btn ${isSet ? 'btn-secondary' : 'btn-neutral bg-opacity-50'}`} > ) } export const MeasieInput = ({ t, m, mset, backend, refresh, toast, children, onUpdate, startLoading = () => null, stopLoading = () => null, }) => { const isDegree = isDegreeMeasurement(m) const factor = isDegree ? 1 : mset.imperial ? 25.4 : 10 const isValValid = (val) => typeof val === 'undefined' || val === '' ? null : val != false && !isNaN(val) const isValid = (newVal) => (typeof newVal === 'undefined' ? isValValid(val) : isValValid(newVal)) const [val, setVal] = useState(mset.measies?.[m] / factor || '') const [valid, setValid] = useState(isValid(mset.measies?.[m] / factor || '')) // Update onChange const update = (evt) => { setVal(evt.target.value) const useVal = isDegree ? evt.target.value : measurementAsMm(evt.target.value, mset.imperial ? 'imperial' : 'metric') const validUpdate = isValid(useVal) setValid(validUpdate) if (validUpdate && typeof onUpdate === 'function') { onUpdate(m, useVal) } } const save = async () => { // FIXME startLoading() const measies = {} measies[m] = val * factor const result = await backend.updateSet(mset.id, { measies }) if (result.success) { refresh() toast.for.settingsSaved() } else toast.for.backendError() stopLoading() } const fraction = (i, base) => update({ target: { value: Math.floor(val) + i / base } }) if (!m) return null return (
{mset.imperial && (
{!isDegree && }
)}
{backend && ( )}
) }