File size: 5,991 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
import React, { useState, useEffect, useCallback } from 'react'
import { useParams } from 'react-router'
import { Loader } from '@adminjs/design-system'
import { ErrorTypeEnum } from '../../../utils/error-type.enum.js'
import BaseActionComponent from '../app/base-action-component.js'
import ApiClient from '../../utils/api-client.js'
import { RecordActionParams } from '../../../backend/utils/view-helpers/view-helpers.js'
import { ActionJSON, RecordJSON, actionHasDisabledComponent } from '../../interfaces/index.js'
import { NoResourceError, NoActionError, NoRecordError } from '../app/error-message.js'
import Wrapper from './utils/wrapper.js'
import { ActionHeader } from '../app/index.js'
import { useNotice, useResource } from '../../hooks/index.js'
import DrawerPortal from '../app/drawer-portal.js'
import { ActionResponse, RecordActionResponse } from '../../../backend/actions/action.interface.js'
import mergeRecordResponse from '../../hooks/use-record/merge-record-response.js'
import allowOverride from '../../hoc/allow-override.js'
import { getDataCss } from '../../index.js'
const api = new ApiClient()
const RecordAction: React.FC = () => {
const [record, setRecord] = useState<RecordJSON>()
const [loading, setLoading] = useState(true)
const [tag, setTag] = useState('')
const [filterVisible, setFilterVisible] = useState(false)
const params = useParams<RecordActionParams>()
const addNotice = useNotice()
const { actionName, recordId, resourceId } = params
const resource = useResource(resourceId!)
const action = record && record.recordActions.find((r) => r.name === actionName)
const actionFromResource = resource?.actions.find((a) => a.name === actionName)
const listActionName = 'list'
const listAction = resource?.resourceActions.find((r) => r.name === listActionName)
const fetchRecord = (): void => {
// Do not call API on route enter if the action doesn't have a component
if (actionFromResource && actionHasDisabledComponent(actionFromResource)) {
setLoading(false)
return
}
setLoading(true)
api
.recordAction(params as RecordActionParams)
.then((response) => {
if (response.data.notice && response.data.notice.type === 'error') {
addNotice(response.data.notice)
}
if (
!response.data.record?.baseError?.type
|| ![ErrorTypeEnum.App, ErrorTypeEnum.NotFound, ErrorTypeEnum.Forbidden].includes(
response.data.record?.baseError?.type as ErrorTypeEnum,
)
) {
setRecord(response.data.record)
}
})
.catch((error) => {
addNotice({
message: 'errorFetchingRecord',
type: 'error',
resourceId,
})
throw error
})
.finally(() => {
setLoading(false)
})
}
useEffect(() => {
fetchRecord()
}, [actionName, recordId, resourceId])
const handleActionPerformed = useCallback(
(oldRecord: RecordJSON, response: ActionResponse) => {
if (response.record) {
setRecord(mergeRecordResponse(oldRecord, response as RecordActionResponse))
} else {
fetchRecord()
}
},
[fetchRecord],
)
if (!resource) {
return <NoResourceError resourceId={resourceId!} />
}
// When the user visits this route (record action) from a different, than the current one, record.
// It renders everything with a new resource. The old record remains until useEffect fetches data
// from the API. that is why we have to check if the current record has correct record.id.
// Alternative approach would be to setRecord(undefined) before the fetch, but it is async and
// we cannot be sure that the component wont be rendered (it will be at least once) with the
// wrong data.
const hasDifferentRecord = record && record.id && record.id.toString() !== recordId
if (loading || hasDifferentRecord) {
return actionFromResource?.showInDrawer ? (
<DrawerPortal>
<Loader />
</DrawerPortal>
) : (
<Loader />
)
}
if (!action || (actionFromResource && actionHasDisabledComponent(actionFromResource))) {
return <NoActionError resourceId={resourceId!} actionName={actionName!} />
}
if (!record) {
return <NoRecordError resourceId={resourceId!} recordId={recordId!} />
}
const routeWrapperCss = getDataCss(resource.id, action.actionType, action.name, 'route-wrapper')
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 as ActionJSON} resource={resource} record={record} />
</DrawerPortal>
)
}
const toggleFilter = listAction.showFilter
? (): void => setFilterVisible(!filterVisible)
: undefined
return (
<>
<DrawerPortal width={action.containerWidth} data-css={routeActionCss}>
<BaseActionComponent action={action as ActionJSON} resource={resource} record={record} />
</DrawerPortal>
<Wrapper width={listAction.containerWidth} data-css={routeWrapperCss}>
<ActionHeader
resource={resource}
action={listAction}
tag={tag}
toggleFilter={toggleFilter}
/>
<BaseActionComponent action={listAction} resource={resource} setTag={setTag} />
</Wrapper>
</>
)
}
return (
<Wrapper width={action.containerWidth} data-css={routeWrapperCss}>
<ActionHeader
resource={resource}
action={action}
record={record}
actionPerformed={(response: ActionResponse):void => handleActionPerformed(record, response)}
/>
<BaseActionComponent action={action} resource={resource} record={record} />
</Wrapper>
)
}
export default allowOverride(RecordAction, 'RecordActionRoute')
|