This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
RowOperationsMenu.tsx
89 lines (82 loc) · 3.12 KB
/
RowOperationsMenu.tsx
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
import React from 'react'
import { Menu, Icon, Skeleton } from 'antd'
import { Operation, isOperationGroup } from '../../interfaces/operation'
import { buildBcUrl } from '../../utils/strings'
import { useSelector, useDispatch } from 'react-redux'
import { Store } from '../../interfaces/store'
import { $do } from '../../actions/actions'
import { useWidgetOperations } from '../../hooks/useWidgetOperations'
import { WidgetMeta } from '../../interfaces/widget'
import { ClickParam } from 'antd/lib/menu'
import { useTranslation } from 'react-i18next'
import * as styles from './RowOperationsMenu.less'
/**
* {@link RowOperationsMenu | RowOperationsMenu} properties
*/
interface RowOperationsMenuProps {
/**
* Widget meta description
*/
meta: WidgetMeta
/**
* Use when business component differs from widget's (e.g. hierarchies nested level)
*/
bcName?: string
/**
* Callback when selecting an operation
*/
onSelect?: (operationKey: string) => void
}
/**
* Menu with available record operations
*
* On operation selection dispatches {@link ActionPayloadTypes.sendOperation | sendOperation}
*
* @param props - Component properties
*/
export const RowOperationsMenu: React.FC<RowOperationsMenuProps> = ({ meta, bcName: hierarchyBc, onSelect, ...rest }) => {
const { t } = useTranslation()
const dispatch = useDispatch()
const bcName = hierarchyBc || meta.bcName
const bcUrl = buildBcUrl(bcName, true)
const loading = useSelector((store: Store) => !!store.view.metaInProgress[meta.bcName])
/**
* Operations from row meta
*/
const operations = useSelector((store: Store) => store.view.rowMeta[bcName]?.[bcUrl]?.actions)
/**
* Filter operations based on widget settings
*/
const operationList = useWidgetOperations(operations, meta, bcName)
const handleClick = React.useCallback(
(param: ClickParam) => {
dispatch($do.sendOperation({ bcName, operationType: param.key, widgetName: meta.name }))
onSelect?.(param.key)
},
[meta.name, bcName, onSelect, dispatch]
)
const menuItem = React.useCallback(
(item: Operation) => (
<Menu.Item key={item.type} onClick={handleClick}>
{item.icon && <Icon type={item.icon} />}
{item.text}
</Menu.Item>
),
[handleClick]
)
const menuItemList = operationList
.map(item => {
if (isOperationGroup(item)) {
return (
<Menu.ItemGroup key={item.type || item.text} title={item.text}>
{item.actions.filter(operation => operation.scope === 'record').map(menuItem)}
</Menu.ItemGroup>
)
}
return item.scope === 'record' ? menuItem(item) : null
})
.filter(item => !!item)
const displayedItems = menuItemList.length ? menuItemList : <Menu.Item disabled>{t('No operations available')}</Menu.Item>
return <Menu {...rest}>{loading ? <Skeleton active className={styles.skeleton} /> : displayedItems}</Menu>
}
export default React.memo(RowOperationsMenu)