Skip to content

useItemActions ​

Processes the actions array from a form schema into executable, filtered, and attribute-cast action buttons. Handles four action types: request, modal, download, and blank.

File: vue/src/js/hooks/useItemActions.js
Props factory: makeItemActionsProps


Usage ​

js
import { useItemActions, makeItemActionsProps } from '@/hooks'

const props = defineProps({ ...makeItemActionsProps() })
const { allActions, visibleActions, hasVisibleActions, handleAction } = useItemActions(props, context)
html
<template v-if="hasVisibleActions">
  <v-btn
    v-for="action in visibleActions"
    :key="action.label"
    @click="handleAction(action)"
  >
    {{ action.label }}
  </v-btn>
</template>

Props (via makeItemActionsProps) ​

PropTypeDefaultDescription
isEditingBooleanfalseWhether the form is in edit mode
actionsArray|Object[]Action definitions from the module config

Action definition shape ​

php
// In module config
'actions' => [
    [
        'type'             => 'request',   // 'request' | 'modal' | 'download' | 'blank'
        'label'            => 'Archive $name',
        'endpoint'         => '/items/:id/archive',
        'method'           => 'post',      // for 'request' type
        'params'           => [],          // request body params
        'editable'         => true,        // show in edit mode
        'creatable'        => false,       // show in create mode
        'hasConfirmation'  => true,        // show confirmation modal first
        'reloadOnSuccess'  => true,        // reload page after success
        'reloadDelay'      => 1000,        // ms delay before reload
        'hideOnCondition'  => false,       // hide (not just disable) when conditions fail
        'conditions'       => [],          // item-level conditions
        'userConditions'   => [],          // user profile conditions
        'responseMessage'  => [
            'success' => 'Archived successfully',
            'error'   => 'Archive failed',
        ],
        'confirmationModalAttributes' => [
            'title' => 'Confirm Archive',
        ],
    ],
]

Action types ​

TypeBehaviour
requestSends an Axios request to endpoint using method. Shows result via useAlert.
modalOpens a form modal at endpoint (uses store.commit('SET_MODAL', ...)).
downloadOpens endpoint in a new tab via a temporary <a> element.
blankOpens endpoint in a new tab via window.open.

Returns ​

NameTypeDescription
allActionsComputedRef<Array>All actions after attribute casting and condition evaluation
visibleActionsComputedRef<Array>Actions that are not hidden and not disabled
hasActionsComputedRef<Boolean>True when allActions.length > 0
hasVisibleActionsComputedRef<Boolean>True when visibleActions.length > 0
handleAction(action) => voidExecute an action; opens confirmation modal if hasConfirmation is true
shouldShowAction(action) => BooleanValidate action against isEditing, conditions, and user conditions

Attribute casting ​

Before rendering, each action's string values are processed with useCastAttributes. This lets you reference the editing item in labels and endpoints:

php
'label'    => 'View $name',        // item.name
'endpoint' => '/items/:id/review', // :id is replaced with item.id

See Also ​