import React, { useState, useEffect } from 'react' import { Loader } from '@adminjs/design-system' import { useLocation, useParams } from 'react-router' import { BulkActionParams } from '../../../backend/utils/view-helpers/view-helpers.js' import ApiClient from '../../utils/api-client.js' import getBulkActionsFromRecords from '../app/records-table/utils/get-bulk-actions-from-records.js' import { ActionJSON, RecordJSON } from '../../interfaces/index.js' import Wrapper from './utils/wrapper.js' import { ActionHeader, DrawerPortal, BaseActionComponent, ErrorMessageBox, NoResourceError, NoActionError, } from '../app/index.js' import { useTranslation, useNotice, useResource } from '../../hooks/index.js' import allowOverride from '../../hoc/allow-override.js' import { getDataCss } from '../../index.js' type MatchParams = Pick const api = new ApiClient() const BulkAction: React.FC = () => { const params = useParams() const [records, setRecords] = useState>([]) const [loading, setLoading] = useState(false) const [tag, setTag] = useState('') const [filterVisible, setFilterVisible] = useState(false) const { translateMessage } = useTranslation() const addNotice = useNotice() const location = useLocation() const { resourceId, actionName } = params const resource = useResource(resourceId!) const listActionName = 'list' const listAction = resource?.resourceActions.find((r) => r.name === listActionName) const fetchRecords = (): Promise => { const recordIdsString = new URLSearchParams(location.search).get('recordIds') const recordIds = recordIdsString ? recordIdsString.split(',') : [] setLoading(true) return api .bulkAction({ resourceId: resourceId!, recordIds, actionName: actionName!, }) .then((response) => { setLoading(false) setRecords(response.data.records) }) .catch((error) => { setLoading(false) addNotice({ message: 'errorFetchingRecords', type: 'error', resourceId, }) throw error }) } useEffect(() => { fetchRecords() }, [params.resourceId, params.actionName, location.search]) if (!resource) { return } if (!records && !loading) { return (

{translateMessage('noRecordsSelected', resourceId)}

) } const action = getBulkActionsFromRecords(records || []).find((r) => r.name === actionName) if (loading) { const actionFromResource = resource.actions.find((r) => r.name === actionName) return actionFromResource?.showInDrawer ? ( ) : ( ) } if (!action) { return } 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 ( ) } const toggleFilter = listAction.showFilter ? (): void => setFilterVisible(!filterVisible) : undefined return ( <> ) } return ( {!action?.showInDrawer ? : ''} ) } export default allowOverride(BulkAction, 'BulkActionRoute')