SoftwareBrothers / adminjs

AdminJS is an admin panel for apps written in node.js

Home Page:https://adminjs.co

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature]: function for guard instead of only string

milovanderpas opened this issue · comments

Description

When defining a guard at the moment it is only possible to pass a string. I would like to pass a function so that I can show a specific guard message for each record.

Suggested Solution

In the build-action-click-handler.ts:

if (actionHasDisabledComponent(action)) {
      if (action.guard) {
        const modalData: ModalData = {
          modalProps: {
            variant: 'danger',
            label: 'confirm',
            title: typeof action.guard === 'string' ? action.guard : await action.guard(params),
          },
          type: 'confirm',
          resourceId: params.resourceId,
          confirmAction: callApi,
        }

        // If confirmation is required, action trigger should be handled in modal
        openModal(modalData)
        return
      }

      // If no confirmation is required, call API
      callApi()
      return
}

In the action.interface.ts:
export type GuardFunction = (params: DifferentActionParams) => string | Promise<string>
In the action.interface.d.ts:
guard?: string | GuardFunction

Alternatives

I do not have alternatives

Additional Context

The Function type can maybe be changed to a own interface