import { makeStyles } from '@material-ui/core' import classnames from 'classnames' import * as R from 'ramda' import React, { useReducer, useEffect } from 'react' import ErrorMessage from 'src/components/ErrorMessage' import Stepper from 'src/components/Stepper' import { Button } from 'src/components/buttons' import { RadioGroup, Autocomplete } from 'src/components/inputs' import { Info2, H4 } from 'src/components/typography' import FormRenderer from 'src/pages/Services/FormRenderer' import schema from 'src/pages/Services/schemas' import { startCase } from 'src/utils/string' import styles from './WizardStep.styles' const useStyles = makeStyles(styles) const initialState = { form: null, selected: null, isNew: false, iError: false } const reducer = (state, action) => { switch (action.type) { case 'select': return { form: null, selected: action.selected, isNew: null, iError: false } case 'new': return { form: state.form, selected: null, isNew: true, iError: false } case 'form': return { form: action.form, selected: action.form.code, isNew: true, iError: false } case 'error': return R.merge(state, { innerError: true }) case 'reset': return initialState default: throw new Error() } } const WizardStep = ({ type, name, step, error, lastStep, onContinue, filled, unfilled, getValue }) => { const classes = useStyles() const [{ innerError, selected, form, isNew }, dispatch] = useReducer( reducer, initialState ) useEffect(() => { dispatch({ type: 'reset' }) }, [step]) const innerContinue = (config, account) => { if (!config || !config[type]) { return dispatch({ type: 'error' }) } onContinue(config, account) } const label = lastStep ? 'Finish' : 'Next' const displayName = name ?? type const subtitleClass = { [classes.subtitle]: true, [classes.error]: innerError } return ( <> {startCase(type)}

Select a {displayName} or set up a new one

{ dispatch({ type: 'select', selected: it }) }} labelClassName={classes.radioLabel} radioClassName={classes.radio} />
{!R.isEmpty(unfilled) && !R.isNil(unfilled) && ( { dispatch({ type: 'new' }) }} labelClassName={classes.radioLabel} radioClassName={classes.radio} options={[{ display: 'Set up new', code: true }]} /> )} {isNew && ( { dispatch({ type: 'form', form: it }) }} /> )}
{form && ( innerContinue({ [type]: form.code }, { [form.code]: it })} elements={schema[form.code].elements} validationSchema={schema[form.code].validationSchema} value={getValue(form.code)} buttonLabel={label} /> )} {!form && (
{error && Failed to save}
)} ) } export default WizardStep