|
| 1 | +import React from 'react' |
| 2 | +import Container from '@mui/material/Container' |
| 3 | +import Link from '@mui/material/Link' |
| 4 | +import Box from '@mui/material/Box' |
| 5 | +import Grid from '@mui/material/Grid' |
| 6 | +import Typography from '@mui/material/Typography' |
| 7 | +import CircularProgress from '@mui/material/CircularProgress' |
| 8 | +import { Nav } from '../components/Layout' |
| 9 | +import { |
| 10 | + createOrderedMap, createStore, JsonSchema, |
| 11 | + onChangeHandler, StoreSchemaType, storeUpdater, |
| 12 | + UIStoreProvider, StoreKeys, ObjectGroup, |
| 13 | + extractValue, applyPluginStack, WidgetProps, WithValue, |
| 14 | + PROGRESS_START, PROGRESS_DONE, PROGRESS_ERROR, PROGRESS_NONE, |
| 15 | + PROGRESS, |
| 16 | +} from '@ui-schema/ui-schema' |
| 17 | +import { List, OrderedMap } from 'immutable' |
| 18 | +import { DataDebug } from '../components/DataDebug' |
| 19 | + |
| 20 | +const schema = createOrderedMap({ |
| 21 | + type: 'object', |
| 22 | + properties: { |
| 23 | + file: { |
| 24 | + type: 'object', |
| 25 | + properties: { |
| 26 | + link: { |
| 27 | + type: 'string', |
| 28 | + }, |
| 29 | + expiry: { |
| 30 | + type: 'string', |
| 31 | + }, |
| 32 | + name: { |
| 33 | + type: 'string', |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | +} as JsonSchema) |
| 39 | + |
| 40 | +const FileUpload: React.ComponentType<WidgetProps & WithValue> = ({storeKeys, onChange, schema, required, value}) => { |
| 41 | + const [loading, setLoading] = React.useState<PROGRESS>(PROGRESS_NONE) |
| 42 | + return <Box style={{display: 'flex', flexDirection: 'column'}}> |
| 43 | + <input |
| 44 | + type={'file'} |
| 45 | + onChange={e => { |
| 46 | + setLoading(PROGRESS_START) |
| 47 | + const formData = new FormData() |
| 48 | + // @ts-ignore |
| 49 | + formData.append('file', e.target.files[0]) |
| 50 | + // todo: handle `is uploading` |
| 51 | + fetch('https://file.io', {method: 'POST', body: formData}) |
| 52 | + .then(r => r.json()) |
| 53 | + .then(data => { |
| 54 | + if(data.status === 200) { |
| 55 | + setLoading(PROGRESS_DONE) |
| 56 | + console.log('file uploaded!', data) |
| 57 | + onChange({ |
| 58 | + type: 'set', |
| 59 | + scopes: ['value'], |
| 60 | + storeKeys: storeKeys, |
| 61 | + data: { |
| 62 | + value: OrderedMap({ |
| 63 | + link: data.link, |
| 64 | + expires: data.expires, |
| 65 | + name: data.name, |
| 66 | + }), |
| 67 | + }, |
| 68 | + schema, |
| 69 | + required, |
| 70 | + }) |
| 71 | + } else { |
| 72 | + // todo: implement validity handling on error |
| 73 | + console.error('File upload error!', data) |
| 74 | + setLoading(PROGRESS_ERROR) |
| 75 | + } |
| 76 | + }) |
| 77 | + .catch((e) => { |
| 78 | + console.error('File upload fetch error!', e) |
| 79 | + setLoading(PROGRESS_ERROR) |
| 80 | + }) |
| 81 | + }} |
| 82 | + /> |
| 83 | + |
| 84 | + {loading === PROGRESS_START || loading === PROGRESS_ERROR ? |
| 85 | + <Box style={{display: 'flex', flexDirection: 'column', alignItems: 'center'}} my={1}> |
| 86 | + <CircularProgress/> |
| 87 | + <Typography variant={'caption'} color={loading === PROGRESS_ERROR ? 'error' : 'textSecondary'}> |
| 88 | + {loading === PROGRESS_ERROR ? 'upload failed' : 'uploading...'} |
| 89 | + </Typography> |
| 90 | + </Box> : null} |
| 91 | + {value?.get('link') ? |
| 92 | + <Box p={2}> |
| 93 | + <Typography variant={'subtitle2'} gutterBottom>Uploaded File:</Typography> |
| 94 | + <Box px={2}> |
| 95 | + <pre style={{margin: 0}}><code>{JSON.stringify(value?.toJS(), undefined, 4)}</code></pre> |
| 96 | + <Link href={value?.get('link')} target={'_blank'} rel={'noreferrer noopener'}>open download page</Link> |
| 97 | + </Box> |
| 98 | + </Box> : |
| 99 | + <Typography variant={'caption'} color={'textSecondary'}>no file uploaded</Typography>} |
| 100 | + </Box> |
| 101 | +} |
| 102 | + |
| 103 | +const WidgetFileUpload = applyPluginStack(extractValue(FileUpload)) |
| 104 | + |
| 105 | +const CustomFormContent: React.FC<{ |
| 106 | + storeKeys?: StoreKeys |
| 107 | + schema: StoreSchemaType |
| 108 | + showValidity?: boolean |
| 109 | +}> = ({storeKeys = List(), schema}) => { |
| 110 | + const [objectSchema, setObjectSchema] = React.useState<StoreSchemaType>(() => schema) |
| 111 | + return <ObjectGroup |
| 112 | + storeKeys={storeKeys} |
| 113 | + schema={schema} parentSchema={undefined} |
| 114 | + onSchema={setObjectSchema} |
| 115 | + > |
| 116 | + <Grid container dir={'columns'} spacing={4}> |
| 117 | + <WidgetFileUpload |
| 118 | + level={1} |
| 119 | + storeKeys={storeKeys.push('file') as StoreKeys} |
| 120 | + schema={objectSchema.getIn(['properties', 'file']) as unknown as StoreSchemaType} |
| 121 | + parentSchema={objectSchema} |
| 122 | + /> |
| 123 | + </Grid> |
| 124 | + </ObjectGroup> |
| 125 | +} |
| 126 | + |
| 127 | +const DemoComponent = () => { |
| 128 | + const showValidity = true |
| 129 | + const [store, setStore] = React.useState(() => createStore(OrderedMap({}))) |
| 130 | + |
| 131 | + const onChange: onChangeHandler = React.useCallback( |
| 132 | + (actions) => setStore(storeUpdater(actions)), |
| 133 | + [setStore], |
| 134 | + ) |
| 135 | + |
| 136 | + return <React.Fragment> |
| 137 | + <UIStoreProvider |
| 138 | + store={store} |
| 139 | + onChange={onChange} |
| 140 | + showValidity={showValidity} |
| 141 | + > |
| 142 | + <CustomFormContent |
| 143 | + schema={schema} |
| 144 | + showValidity={showValidity} |
| 145 | + /> |
| 146 | + |
| 147 | + <DataDebug/> |
| 148 | + </UIStoreProvider> |
| 149 | + </React.Fragment> |
| 150 | +} |
| 151 | + |
| 152 | +export const PageCustomUpload: React.ComponentType = () => { |
| 153 | + return <> |
| 154 | + <Container maxWidth={'md'} fixed style={{display: 'flex'}}> |
| 155 | + <Nav/> |
| 156 | + <Box mx={2} py={1} style={{flexGrow: 1}}> |
| 157 | + <Box mb={2}> |
| 158 | + <Typography variant={'h1'} gutterBottom>UI-Schema Custom Upload</Typography> |
| 159 | + <Typography variant={'body2'} gutterBottom>Custom rendering with a native file upload field.</Typography> |
| 160 | + </Box> |
| 161 | + <DemoComponent/> |
| 162 | + </Box> |
| 163 | + </Container> |
| 164 | + </> |
| 165 | +} |
0 commit comments