File size: 4,649 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Box, Button, DrawerContent, DrawerFooter, Icon } from '@adminjs/design-system'
import pick from 'lodash/pick.js'
import React, { FC, useEffect } from 'react'
import { useNavigate } from 'react-router'

import allowOverride from '../../hoc/allow-override.js'
import { useQueryParams } from '../../hooks/use-query-params.js'
import useRecord from '../../hooks/use-record/use-record.js'
import { useTranslation } from '../../hooks/use-translation.js'
import { RecordJSON } from '../../interfaces/index.js'
import { getActionElementCss } from '../../utils/index.js'
import ActionHeader from '../app/action-header/action-header.js'
import BasePropertyComponent from '../property-type/index.js'
import { ActionProps } from './action.props.js'
import { appendForceRefresh } from './utils/append-force-refresh.js'
import LayoutElementRenderer from './utils/layout-element-renderer.js'

const New: FC<ActionProps> = (props) => {
  const { record: initialRecord, resource, action } = props
  const { record, handleChange, submit, loading, setRecord } = useRecord(initialRecord, resource.id)
  const { translateButton } = useTranslation()
  const navigate = useNavigate()
  const { parsedQuery, redirectUrl } = useQueryParams()

  useEffect(() => {
    if (initialRecord) {
      setRecord(initialRecord)
    }
  }, [initialRecord, parsedQuery])

  useEffect(() => {
    if (parsedQuery) {
      const resourceProperties = pick(parsedQuery, Object.keys(resource.properties))
      if (Object.keys(resourceProperties).length) {
        setRecord({ ...record, params: { ...record.params, ...resourceProperties } })
      }
    }
  }, [parsedQuery])

  const handleSubmit = (event): boolean => {
    event.preventDefault()
    if (!event.currentTarget) return false
    submit().then((response) => {
      if (response.data.redirectUrl) {
        navigate(appendForceRefresh(response.data.redirectUrl))
      }
      // if record has id === has been created
      if (response.data.record.id && !Object.keys(response.data.record.errors).length) {
        handleChange({ params: {}, populated: {}, errors: {} } as RecordJSON)
      }
    })
    return false
  }

  const handleCancel = () => {
    if (redirectUrl) {
      window.location.href = redirectUrl
    }
  }

  const contentTag = getActionElementCss(resource.id, action.name, 'drawer-content')
  const formTag = getActionElementCss(resource.id, action.name, 'form')
  const footerTag = getActionElementCss(resource.id, action.name, 'drawer-footer')
  const buttonTag = getActionElementCss(resource.id, action.name, 'drawer-submit')
  const cancelButtonTag = getActionElementCss(resource.id, action.name, 'drawer-cancel')

  return (
    <Box
      as="form"
      flex
      flexGrow={1}
      onSubmit={handleSubmit}
      flexDirection="column"
      data-css={formTag}
    >
      <DrawerContent data-css={contentTag}>
        {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) => (
            <BasePropertyComponent
              key={property.propertyPath}
              where="edit"
              onChange={handleChange}
              property={property}
              resource={resource}
              record={record as RecordJSON}
            />
          ))}
      </DrawerContent>
      <DrawerFooter data-css={footerTag}>
        <Box flex style={{ gap: 16 }}>
          {redirectUrl && (
            <Button
              variant="light"
              type="button"
              onClick={handleCancel}
              data-css={cancelButtonTag}
              data-testid="button-cancel"
            >
              {translateButton('cancel', resource.id)}
            </Button>
          )}
          <Button
            variant="contained"
            type="submit"
            data-css={buttonTag}
            data-testid="button-save"
            disabled={loading}
          >
            {loading ? <Icon icon="Loader" spin /> : null}
            {translateButton('save', resource.id)}
          </Button>
        </Box>
      </DrawerFooter>
    </Box>
  )
}

const OverridableNew = allowOverride(New, 'DefaultNewAction')

export {
  OverridableNew as default,
  OverridableNew as New,
  New as OriginalNew,
}