|
| 1 | +import { Button, createStyles, DialogActions, DialogContent, makeStyles, TextField } from '@material-ui/core' |
| 2 | +import { GenericContent } from '@sensenet/default-content-types' |
| 3 | +import { useLogger, useRepository } from '@sensenet/hooks-react' |
| 4 | +import React, { useEffect, useRef, useState } from 'react' |
| 5 | +import { useCurrentUser } from '../../context' |
| 6 | +import { useGlobalStyles } from '../../globalStyles' |
| 7 | +import { useLocalization } from '../../hooks' |
| 8 | +import { Icon } from '../Icon' |
| 9 | +import { DialogTitle, useDialog } from '.' |
| 10 | + |
| 11 | +export interface OperationsDialogProps { |
| 12 | + content: GenericContent |
| 13 | + OperationName: string |
| 14 | +} |
| 15 | +/*Ezt itt jól ki kell dolgozni!!! nem végleges csak demora van egyszerűsítve |
| 16 | + Valószínüleg nem is itt lesz a végleges helye hanem ott ahol a GenericContent van |
| 17 | +*/ |
| 18 | +type UIDescription = { |
| 19 | + title?: string |
| 20 | + submitTitle?: string |
| 21 | + elements: Array<{ |
| 22 | + name?: string |
| 23 | + description?: string |
| 24 | + inputProps: React.HTMLProps<HTMLInputElement> |
| 25 | + }> |
| 26 | +} |
| 27 | +const useStyles = makeStyles(() => |
| 28 | + createStyles({ |
| 29 | + form: { |
| 30 | + display: 'flex', |
| 31 | + flexDirection: 'column', |
| 32 | + alignItems: 'center', |
| 33 | + rowGap: '30px', |
| 34 | + '& :is(h3,p)': { |
| 35 | + margin: '0 0', |
| 36 | + fontWeight: 'normal', |
| 37 | + }, |
| 38 | + '& input': { |
| 39 | + padding: '0', |
| 40 | + }, |
| 41 | + '& .input-container': { |
| 42 | + width: '300px', |
| 43 | + }, |
| 44 | + }, |
| 45 | + }), |
| 46 | +) |
| 47 | + |
| 48 | +export function OperationsDialog(props: OperationsDialogProps) { |
| 49 | + const { closeLastDialog } = useDialog() |
| 50 | + const currentUser = useCurrentUser() |
| 51 | + const classes = useStyles() |
| 52 | + const logger = useLogger('Operations') |
| 53 | + const formRef = useRef<HTMLFormElement>(null) |
| 54 | + const repository = useRepository() |
| 55 | + const localization = useLocalization().operations |
| 56 | + const globalClasses = useGlobalStyles() |
| 57 | + |
| 58 | + const [UIDescription, setUIDescription] = useState<UIDescription>() |
| 59 | + |
| 60 | + useEffect(() => { |
| 61 | + console.log(props.OperationName) |
| 62 | + const loadOperation = async () => { |
| 63 | + try { |
| 64 | + const result = await repository.executeAction<any, UIDescription>({ |
| 65 | + method: 'GET', |
| 66 | + idOrPath: props.content.Path, |
| 67 | + name: props.OperationName, |
| 68 | + }) |
| 69 | + setUIDescription(result) |
| 70 | + } catch (error) { |
| 71 | + logger.error({ message: error.message }) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + loadOperation() |
| 76 | + }, [logger, props.OperationName, props.content.Path, repository]) |
| 77 | + |
| 78 | + const submitAction = async (e: React.FormEvent<HTMLFormElement>) => { |
| 79 | + e.preventDefault() |
| 80 | + if (!formRef.current) return |
| 81 | + |
| 82 | + const formData = new FormData(formRef.current) |
| 83 | + const formJson: Record<string, any> = {} |
| 84 | + |
| 85 | + formData.forEach((value, key) => { |
| 86 | + formJson[key] = value |
| 87 | + }) |
| 88 | + |
| 89 | + try { |
| 90 | + await repository.executeAction<any, UIDescription>({ |
| 91 | + method: 'POST', |
| 92 | + idOrPath: props.content.Path, |
| 93 | + name: props.OperationName, |
| 94 | + body: formJson, |
| 95 | + }) |
| 96 | + |
| 97 | + closeLastDialog() |
| 98 | + } catch (error) { |
| 99 | + logger.error({ message: error.message }) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return ( |
| 104 | + <> |
| 105 | + <DialogTitle> |
| 106 | + <div className={globalClasses.centered}> |
| 107 | + <Icon |
| 108 | + style={{ |
| 109 | + margin: '0 1em 0 0', |
| 110 | + transition: 'filter linear 1s, opacity linear 1.5s', |
| 111 | + }} |
| 112 | + item={currentUser} |
| 113 | + /> |
| 114 | + {UIDescription?.title || localization.title} |
| 115 | + </div> |
| 116 | + </DialogTitle> |
| 117 | + <> |
| 118 | + <DialogContent> |
| 119 | + <form |
| 120 | + ref={formRef} |
| 121 | + className={classes.form} |
| 122 | + id="operation-form" |
| 123 | + onSubmit={(e) => { |
| 124 | + submitAction(e) |
| 125 | + }}> |
| 126 | + {UIDescription?.elements.map((field, index) => { |
| 127 | + const { inputProps, description, name } = field |
| 128 | + |
| 129 | + return ( |
| 130 | + <div className="input-container" key={index}> |
| 131 | + <h3>{name}</h3> |
| 132 | + <p>{description}</p> |
| 133 | + |
| 134 | + <TextField |
| 135 | + name={inputProps.name} |
| 136 | + required={Boolean(inputProps.required)} |
| 137 | + fullWidth |
| 138 | + inputProps={inputProps} |
| 139 | + /> |
| 140 | + </div> |
| 141 | + ) |
| 142 | + })} |
| 143 | + </form> |
| 144 | + </DialogContent> |
| 145 | + <DialogActions> |
| 146 | + <Button aria-label={localization.cancel} className={globalClasses.cancelButton} onClick={closeLastDialog}> |
| 147 | + {localization.cancel} |
| 148 | + </Button> |
| 149 | + <Button |
| 150 | + aria-label={localization.submit} |
| 151 | + form="operation-form" |
| 152 | + color="primary" |
| 153 | + variant="contained" |
| 154 | + type="submit" |
| 155 | + autoFocus={true}> |
| 156 | + {UIDescription?.submitTitle || localization.submit} |
| 157 | + </Button> |
| 158 | + </DialogActions> |
| 159 | + </> |
| 160 | + </> |
| 161 | + ) |
| 162 | +} |
| 163 | + |
| 164 | +export default OperationsDialog |
0 commit comments