|
| 1 | +import React, { useState } from 'react' |
| 2 | +import { object, string } from 'yup' |
| 3 | +import { Form, Formik } from 'formik' |
| 4 | +import OFFormControlInputFormiked from '../../baseComponents/form/formControl/OFFormControlInputFormiked.jsx' |
| 5 | +import OFButton from '../../baseComponents/button/OFButton.jsx' |
| 6 | +import Collapse from '@mui/material/Collapse' |
| 7 | +import { makeStyles } from '@mui/styles' |
| 8 | +import ArrowDownIcon from '@mui/icons-material/KeyboardArrowDown' |
| 9 | +import Box from '@mui/material/Box' |
| 10 | +import { FormikObserver } from '../../baseComponents/form/formik/FormikObserver.js' |
| 11 | +import jsonModel from './jsonmodel' |
| 12 | +import clipboardCopy from 'clipboard-copy' |
| 13 | +import Button from '@mui/material/Button' |
| 14 | +import { useTranslation } from 'react-i18next' |
| 15 | + |
| 16 | +const useStyles = makeStyles(() => ({ |
| 17 | + jsonShowButton: { |
| 18 | + width: '100%', |
| 19 | + }, |
| 20 | + jsonExample: { |
| 21 | + background: '#EEE', |
| 22 | + padding: 12, |
| 23 | + marginTop: 32, |
| 24 | + }, |
| 25 | + jsonExamplePre: { |
| 26 | + overflow: 'auto', |
| 27 | + }, |
| 28 | + jsonExampleInnerContainer: { |
| 29 | + position: 'relative', |
| 30 | + }, |
| 31 | + jsonCopyButton: { |
| 32 | + position: 'absolute', |
| 33 | + right: 0, |
| 34 | + top: 10, |
| 35 | + }, |
| 36 | +})) |
| 37 | + |
| 38 | +const SetupJSONUrlForm = ({ |
| 39 | + onBack, |
| 40 | + onSubmit, |
| 41 | + submitText, |
| 42 | + backText, |
| 43 | + initialValues, |
| 44 | + onFormChange, |
| 45 | +}) => { |
| 46 | + const classes = useStyles() |
| 47 | + const [isExampleOpen, setExampleOpen] = useState(false) |
| 48 | + const { t } = useTranslation() |
| 49 | + |
| 50 | + return ( |
| 51 | + <Formik |
| 52 | + validationSchema={object().shape({ |
| 53 | + jsonUrl: string() |
| 54 | + .url(t('settingsSetup.jsonUrl.jsonUrlValid')) |
| 55 | + .required(t('settingsSetup.jsonUrl.jsonUrlRequired')), |
| 56 | + })} |
| 57 | + initialValues={initialValues} |
| 58 | + onSubmit={(values) => |
| 59 | + onSubmit({ |
| 60 | + jsonUrl: values.jsonUrl, |
| 61 | + }) |
| 62 | + }> |
| 63 | + {({ isSubmitting, values }) => ( |
| 64 | + <Form method="POST"> |
| 65 | + {onFormChange && ( |
| 66 | + <FormikObserver |
| 67 | + value={values} |
| 68 | + onChange={(values) => onFormChange(values)} |
| 69 | + /> |
| 70 | + )} |
| 71 | + <OFFormControlInputFormiked |
| 72 | + name={t('settingsSetup.jsonUrl.fieldJsonUrl')} |
| 73 | + fieldName="jsonUrl" |
| 74 | + type="text" |
| 75 | + isSubmitting={isSubmitting} |
| 76 | + /> |
| 77 | + |
| 78 | + <div className={classes.jsonExample}> |
| 79 | + <Button |
| 80 | + className={classes.jsonShowButton} |
| 81 | + onClick={() => setExampleOpen(!isExampleOpen)}> |
| 82 | + {t('settingsSetup.jsonUrl.showJsonModel')}{' '} |
| 83 | + <ArrowDownIcon /> |
| 84 | + </Button> |
| 85 | + <Collapse |
| 86 | + in={isExampleOpen} |
| 87 | + className={classes.jsonExampleInnerContainer}> |
| 88 | + <OFButton |
| 89 | + style={{ design: 'text' }} |
| 90 | + variant="outlined" |
| 91 | + className={classes.jsonCopyButton} |
| 92 | + onClick={() => |
| 93 | + clipboardCopy( |
| 94 | + JSON.stringify(jsonModel, undefined, 4) |
| 95 | + ) |
| 96 | + }> |
| 97 | + {t('common.copy')} |
| 98 | + </OFButton> |
| 99 | + <pre className={classes.jsonExamplePre}> |
| 100 | + {JSON.stringify(jsonModel, undefined, 4)} |
| 101 | + <br /> |
| 102 | + Required fields: |
| 103 | + <br /> |
| 104 | + - session.title |
| 105 | + <br /> |
| 106 | + - session.id |
| 107 | + <br />- speakers: {'{}'} |
| 108 | + <br /> |
| 109 | + Optional fields: all others |
| 110 | + </pre> |
| 111 | + </Collapse> |
| 112 | + </div> |
| 113 | + |
| 114 | + <Box justifyContent="space-between" display="flex"> |
| 115 | + {backText && ( |
| 116 | + <OFButton |
| 117 | + disabled={isSubmitting} |
| 118 | + onClick={() => onBack(values)} |
| 119 | + style={{ |
| 120 | + type: 'big', |
| 121 | + design: 'text', |
| 122 | + marginTop: 64, |
| 123 | + }}> |
| 124 | + {backText} |
| 125 | + </OFButton> |
| 126 | + )} |
| 127 | + |
| 128 | + {submitText && ( |
| 129 | + <OFButton |
| 130 | + disabled={isSubmitting} |
| 131 | + type="submit" |
| 132 | + style={{ type: 'big', marginTop: 64 }}> |
| 133 | + {submitText} |
| 134 | + </OFButton> |
| 135 | + )} |
| 136 | + </Box> |
| 137 | + </Form> |
| 138 | + )} |
| 139 | + </Formik> |
| 140 | + ) |
| 141 | +} |
| 142 | + |
| 143 | +export default SetupJSONUrlForm |
0 commit comments