File size: 1,650 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
import { useNavigate, useLocation } from 'react-router'

import { ActionResponse } from '../../../backend/actions/action.interface.js'
import { ActionJSON, buildActionCallApiTrigger, buildActionClickHandler } from '../../interfaces/index.js'
import { DifferentActionParams, ActionCallCallback, UseActionResult } from './use-action.types.js'
import { actionHref } from '../../interfaces/action/action-href.js'
import { useActionResponseHandler } from './use-action-response-handler.js'
import { useTranslation } from '../use-translation.js'
import { useModal } from '../use-modal.js'

/**
 * @load ./use-action.doc.md
 * @subcategory Hooks
 *
 * @param {ActionJSON}   action      action object
 * @param {ActionParams} params
 * @param {ActionCallCallback} onActionCall - callback triggered when action is performed
 * @return {UseActionResult}
 * @class
 * @hideconstructor
 */
export function useAction<K extends ActionResponse>(
  action: ActionJSON,
  params: DifferentActionParams,
  onActionCall?: ActionCallCallback,
): UseActionResult<K> {
  const navigate = useNavigate()
  const location = useLocation()
  const translateFunctions = useTranslation()
  const modalFunctions = useModal()
  const actionResponseHandler = useActionResponseHandler(onActionCall)

  const href = actionHref(action, params)

  const callApi = buildActionCallApiTrigger<K>({
    action,
    params,
    actionResponseHandler,
  })

  const handleClick = buildActionClickHandler({
    action,
    params,
    actionResponseHandler,
    navigate,
    translateFunctions,
    modalFunctions,
    location,
  })

  return {
    href,
    callApi,
    handleClick,
  }
}