/
edit.tsx
87 lines (80 loc) · 2.49 KB
/
edit.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React, { FC, useEffect } from 'react'
import { useHistory } from 'react-router'
import { DrawerContent, Box, DrawerFooter, Button, Icon } from '@admin-bro/design-system'
import PropertyType from '../property-type'
import { ActionProps } from './action.props'
import ActionHeader from '../app/action-header/action-header'
import useRecord from '../../hooks/use-record/use-record'
import { RecordJSON } from '../../interfaces'
import { appendForceRefresh } from './utils/append-force-refresh'
import { useTranslation } from '../../hooks/use-translation'
import LayoutElementRenderer from './utils/layout-element-renderer'
const Edit: FC<ActionProps> = (props) => {
const { record: initialRecord, resource, action } = props
const {
record,
handleChange,
submit: handleSubmit,
loading,
setRecord,
} = useRecord(initialRecord, resource.id)
const { translateButton } = useTranslation()
const history = useHistory()
useEffect(() => {
if (initialRecord) {
setRecord(initialRecord)
}
}, [initialRecord])
const submit = (event: React.FormEvent<HTMLFormElement>): boolean => {
event.preventDefault()
handleSubmit().then((response) => {
if (response.data.redirectUrl) {
history.push(appendForceRefresh(response.data.redirectUrl))
}
})
return false
}
return (
<Box
as="form"
onSubmit={submit}
flex
flexGrow={1}
flexDirection="column"
>
<DrawerContent>
{action?.showInDrawer ? <ActionHeader {...props} /> : null}
{action.layout ? action.layout.map((layoutElement, i) => (
<LayoutElementRenderer
// eslint-disable-next-line react/no-array-index-key
key={i}
layoutElement={layoutElement}
{...props}
where="edit"
onChange={handleChange}
record={record as RecordJSON}
/>
)) : resource.editProperties.map(property => (
<PropertyType
key={property.path}
where="edit"
onChange={handleChange}
property={property}
resource={resource}
record={record as RecordJSON}
/>
))}
</DrawerContent>
<DrawerFooter>
<Button variant="primary" size="lg" type="submit" data-testid="button-save">
{loading ? (<Icon icon="Fade" spin />) : null}
{translateButton('save', resource.id)}
</Button>
</DrawerFooter>
</Box>
)
}
export {
Edit as default,
Edit,
}