|
| 1 | +import * as React from 'react'; |
| 2 | +import { useEffect, useState } from 'react'; |
| 3 | +import { |
| 4 | + Avatar, |
| 5 | + Card, |
| 6 | + CardActions, |
| 7 | + CardContent, |
| 8 | + CardHeader, |
| 9 | + Divider, |
| 10 | + IconButton, |
| 11 | + Tabs, |
| 12 | +} from '@material-ui/core'; |
| 13 | +import { makeStyles } from '@material-ui/core/styles'; |
| 14 | +import MoreVertIcon from '@material-ui/icons/MoreVert'; |
| 15 | +import { |
| 16 | + FormWithRedirect, |
| 17 | + InferredElementDescription, |
| 18 | + RecordContextProvider, |
| 19 | + SaveContextProvider, |
| 20 | +} from 'ra-core'; |
| 21 | +import { SaveButton, TextInput } from 'ra-ui-materialui'; |
| 22 | +import { ResourceConfiguration } from './ResourceConfigurationContext'; |
| 23 | +import { useResourceConfiguration } from './useResourceConfiguration'; |
| 24 | +import { FieldConfiguration } from './FieldConfiguration'; |
| 25 | +import { FieldConfigurationTab } from './FieldConfigurationTab'; |
| 26 | + |
| 27 | +export const ResourceConfigurationPage = ({ |
| 28 | + resource, |
| 29 | +}: { |
| 30 | + resource: string; |
| 31 | +}) => { |
| 32 | + const [definition, actions] = useResourceConfiguration(resource); |
| 33 | + const [activeField, setActiveField] = useState< |
| 34 | + InferredElementDescription |
| 35 | + >(); |
| 36 | + const classes = useStyles(); |
| 37 | + |
| 38 | + const save = (values: ResourceConfiguration) => { |
| 39 | + actions.update(values); |
| 40 | + }; |
| 41 | + const saveContext = { |
| 42 | + save, |
| 43 | + setOnFailure: () => {}, |
| 44 | + setOnSuccess: () => {}, |
| 45 | + }; |
| 46 | + |
| 47 | + const handleTabChange = (event, newValue) => { |
| 48 | + const newField = definition.fields.find( |
| 49 | + f => f.props.source === newValue |
| 50 | + ); |
| 51 | + setActiveField(newField); |
| 52 | + }; |
| 53 | + |
| 54 | + useEffect(() => { |
| 55 | + if (definition && definition.fields) { |
| 56 | + setActiveField(definition.fields[0]); |
| 57 | + } |
| 58 | + }, [definition]); |
| 59 | + |
| 60 | + if (!definition || !activeField) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + return ( |
| 65 | + <RecordContextProvider value={definition}> |
| 66 | + <SaveContextProvider value={saveContext}> |
| 67 | + <FormWithRedirect |
| 68 | + save={save} |
| 69 | + initialValues={definition} |
| 70 | + render={({ handleSubmitWithRedirect }) => ( |
| 71 | + <Card> |
| 72 | + <CardHeader |
| 73 | + avatar={ |
| 74 | + <Avatar> |
| 75 | + { |
| 76 | + // Here will go an icon selector |
| 77 | + ( |
| 78 | + definition.label || |
| 79 | + definition.name |
| 80 | + ).substr(0, 1) |
| 81 | + } |
| 82 | + </Avatar> |
| 83 | + } |
| 84 | + action={ |
| 85 | + // Will display a menu to delete the resource maybe ? |
| 86 | + <IconButton aria-label="settings"> |
| 87 | + <MoreVertIcon /> |
| 88 | + </IconButton> |
| 89 | + } |
| 90 | + title={`Configuration of ${ |
| 91 | + definition.label || definition.name |
| 92 | + }`} |
| 93 | + /> |
| 94 | + |
| 95 | + <CardContent> |
| 96 | + <TextInput |
| 97 | + source="label" |
| 98 | + initialValue={ |
| 99 | + definition.label || definition.name |
| 100 | + } |
| 101 | + /> |
| 102 | + </CardContent> |
| 103 | + <Divider /> |
| 104 | + <div className={classes.fields}> |
| 105 | + <Tabs |
| 106 | + orientation="vertical" |
| 107 | + value={activeField.props.source} |
| 108 | + onChange={handleTabChange} |
| 109 | + className={classes.fieldList} |
| 110 | + > |
| 111 | + {definition.fields.map(field => ( |
| 112 | + <FieldConfigurationTab |
| 113 | + key={field.props.source} |
| 114 | + field={field} |
| 115 | + value={field.props.source} |
| 116 | + /> |
| 117 | + ))} |
| 118 | + </Tabs> |
| 119 | + {definition.fields.map((field, index) => ( |
| 120 | + <div |
| 121 | + role="tabpanel" |
| 122 | + hidden={ |
| 123 | + activeField.props.source !== |
| 124 | + field.props.source |
| 125 | + } |
| 126 | + id={`nav-tabpanel-${field.props.source}`} |
| 127 | + aria-labelledby={`nav-tab-${field.props.source}`} |
| 128 | + > |
| 129 | + {activeField.props.source === |
| 130 | + field.props.source ? ( |
| 131 | + <FieldConfiguration |
| 132 | + key={field.props.source} |
| 133 | + field={field} |
| 134 | + index={index} |
| 135 | + className={classes.fieldPanel} |
| 136 | + /> |
| 137 | + ) : null} |
| 138 | + </div> |
| 139 | + ))} |
| 140 | + </div> |
| 141 | + <CardActions className={classes.actions}> |
| 142 | + <SaveButton |
| 143 | + handleSubmitWithRedirect={ |
| 144 | + handleSubmitWithRedirect |
| 145 | + } |
| 146 | + /> |
| 147 | + </CardActions> |
| 148 | + </Card> |
| 149 | + )} |
| 150 | + /> |
| 151 | + </SaveContextProvider> |
| 152 | + </RecordContextProvider> |
| 153 | + ); |
| 154 | +}; |
| 155 | + |
| 156 | +const useStyles = makeStyles(theme => ({ |
| 157 | + fields: { |
| 158 | + display: 'flex', |
| 159 | + }, |
| 160 | + fieldList: { |
| 161 | + backgroundColor: theme.palette.background.default, |
| 162 | + }, |
| 163 | + fieldTitle: { |
| 164 | + paddingTop: theme.spacing(1), |
| 165 | + paddingBottom: theme.spacing(1), |
| 166 | + paddingLeft: theme.spacing(2), |
| 167 | + paddingRight: theme.spacing(2), |
| 168 | + textTransform: 'none', |
| 169 | + minHeight: 0, |
| 170 | + }, |
| 171 | + fieldPanel: { |
| 172 | + flexGrow: 1, |
| 173 | + }, |
| 174 | + actions: { |
| 175 | + backgroundColor: theme.palette.background.default, |
| 176 | + }, |
| 177 | +})); |
0 commit comments