File size: 3,549 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
import { Box } from '@adminjs/design-system'
import React, { useState } from 'react'
import { connect } from 'react-redux'
import { useParams } from 'react-router'

import { ResourceActionParams } from '../../../backend/utils/view-helpers/view-helpers.js'
import allowOverride from '../../hoc/allow-override.js'
import { ResourceJSON, actionHasDisabledComponent } from '../../interfaces/index.js'
import { ReduxState } from '../../store/store.js'
import BaseActionComponent from '../app/base-action-component.js'
import DrawerPortal from '../app/drawer-portal.js'
import { NoActionError, NoResourceError } from '../app/error-message.js'
import FilterDrawer from '../app/filter-drawer.js'
import { ActionHeader } from '../app/index.js'
import Wrapper from './utils/wrapper.js'
import { getDataCss, getResourceElementCss } from '../../utils/data-css-name.js'

type PropsFromState = {
  resources: Array<ResourceJSON>
}

type Props = PropsFromState

const ResourceAction: React.FC<Props> = (props) => {
  const params = useParams<ResourceActionParams>()
  const { resources } = props
  const { resourceId, actionName } = params
  const [tag, setTag] = useState('')
  const [filterVisible, setFilterVisible] = useState(false)

  const resource = resources.find((r) => r.id === resourceId)
  if (!resource) {
    return <NoResourceError resourceId={resourceId!} />
  }

  const action = resource.resourceActions.find((r) => r.name === actionName)
  if (!action || actionHasDisabledComponent(action)) {
    return <NoActionError resourceId={resourceId!} actionName={actionName!} />
  }

  const listActionName = 'list'
  const listAction = resource.resourceActions.find((r) => r.name === listActionName)

  const contentTag = getResourceElementCss(resource.id, action.name)
  const routeActionCss = getDataCss(resource.id, action.actionType, action.name, 'route')

  if (action.showInDrawer) {
    if (!listAction) {
      return (
        <DrawerPortal width={action.containerWidth} data-css={routeActionCss}>
          <BaseActionComponent action={action} resource={resource} />
        </DrawerPortal>
      )
    }

    const toggleFilter = listAction.showFilter
      ? (): void => setFilterVisible(!filterVisible)
      : undefined

    return (
      <>
        <DrawerPortal width={action.containerWidth} data-css={routeActionCss}>
          <BaseActionComponent
            action={action}
            resource={resource}
            setTag={setTag}
          />
        </DrawerPortal>
        <Wrapper width={listAction.containerWidth} data-css={contentTag}>
          <ActionHeader
            resource={resource}
            action={listAction}
            tag={tag}
            toggleFilter={toggleFilter}
          />
          <BaseActionComponent action={listAction} resource={resource} setTag={setTag} />
        </Wrapper>
      </>
    )
  }

  return (
    <Wrapper width={action.containerWidth} showFilter={action.showFilter} data-css={contentTag}>
      <Box flex flexDirection="column" flexGrow={1}>
        <ActionHeader
          resource={resource}
          action={action}
          toggleFilter={action.showFilter}
          tag={tag}
        />
        <BaseActionComponent action={action} resource={resource} setTag={setTag} />
      </Box>
      {action.showFilter && <FilterDrawer resource={resource} />}
    </Wrapper>
  )
}

const mapStateToProps = (state: ReduxState): PropsFromState => ({
  resources: state.resources,
})

export default allowOverride(connect(mapStateToProps)(ResourceAction), 'ResourceActionRoute')