import { Form, Formik } from 'formik' import * as R from 'ramda' import React, { useContext } from 'react' import PromptWhenDirty from '../../../components/PromptWhenDirty' import { TL2 } from '../../../components/typography' import * as Yup from 'yup' import { transformNumber } from '../../../utils/number' import { Cashbox } from '../../../components/inputs/cashbox/Cashbox' import NotificationsCtx from '../NotificationsContext' import Header from '../components/EditHeader' import EditableNumber from '../components/EditableNumber' const CASH_IN_KEY = 'fiatBalanceAlertsCashIn' const CASH_OUT_KEY = 'fiatBalanceAlertsCashOut' const RECYCLER_STACKER_KEY = 'fiatBalanceAlertsRecyclerStacker' const DEFAULT_NUMBER_OF_CASSETTES = 2 const DEFAULT_NUMBER_OF_RECYCLERS = 0 const notesMin = 0 const notesMax = 9999999 const FiatBalance = ({ section, fieldWidth = 80 }) => { const { isEditing, isDisabled, setEditing, data, save, machines = [], } = useContext(NotificationsCtx) const maxNumberOfCassettes = Math.max( ...R.map(it => it.numberOfCassettes, machines), DEFAULT_NUMBER_OF_CASSETTES, ) const maxNumberOfRecyclers = Math.max( ...R.map(it => it.numberOfRecyclers, machines), DEFAULT_NUMBER_OF_RECYCLERS, ) const percentValidation = Yup.number() .transform(transformNumber) .integer() .min(0) .max(100) .nullable() const schema = Yup.object().shape({ cashInAlertThreshold: Yup.number() .transform(transformNumber) .integer() .min(notesMin) .max(notesMax) .nullable(), fillingPercentageCassette1: percentValidation, fillingPercentageCassette2: percentValidation, fillingPercentageCassette3: percentValidation, fillingPercentageCassette4: percentValidation, fillingPercentageRecycler1: percentValidation, fillingPercentageRecycler2: percentValidation, fillingPercentageRecycler3: percentValidation, fillingPercentageRecycler4: percentValidation, fillingPercentageRecycler5: percentValidation, fillingPercentageRecycler6: percentValidation, }) return ( save(section, schema.cast(it))} onReset={() => { setEditing(CASH_IN_KEY, false) setEditing(CASH_OUT_KEY, false) }}> {({ values }) => (
setEditing(CASH_IN_KEY, it)} /> (x === '' ? '-' : x)} decoration="notes" width={fieldWidth} />
setEditing(CASH_OUT_KEY, it)} />
{R.map( it => ( <>
Cassette {it + 1} (x === '' ? '-' : x)} decoration="%" width={fieldWidth} />
), R.times(R.identity, maxNumberOfCassettes), )}
{maxNumberOfRecyclers > 0 && (
setEditing(RECYCLER_STACKER_KEY, it)} />
{R.chain( it => [ <>
Recycler {(it + 1) * 2 - 1} (x === '' ? '-' : x)} decoration="%" width={fieldWidth} />
, <>
Recycler {(it + 1) * 2} (x === '' ? '-' : x)} decoration="%" width={fieldWidth} />
, ], R.times(R.identity, maxNumberOfRecyclers / 2), )}
)}
)}
) } export default FiatBalance