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
/
AssocListPopup.tsx
321 lines (295 loc) · 11.3 KB
/
AssocListPopup.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import React, { FunctionComponent } from 'react'
import { connect, useSelector } from 'react-redux'
import { $do } from '../../../actions/actions'
import { DataItem, DataValue, PendingDataItem } from '../../../interfaces/data'
import { Store } from '../../../interfaces/store'
import { WidgetTableMeta } from '../../../interfaces/widget'
import Popup, { PopupProps } from '../../ui/Popup/Popup'
import { createMapDispatchToProps } from '../../../utils/redux'
import HierarchyTable from '../../../components/HierarchyTable/HierarchyTable'
import AssocTable from './AssocTable'
import { Skeleton, Tag } from 'antd'
import FullHierarchyTable from '../../FullHierarchyTable/FullHierarchyTable'
import { AssociatedItem } from '../../../interfaces/operation'
import { BcFilter, FilterType } from '../../../interfaces/filters'
import * as styles from '../../ui/Popup/Popup.less'
import { useAssocRecords } from '../../../hooks/useAssocRecords'
export interface IAssocListRecord {
id: string
vstamp: number
originalSelected: DataValue
selected: boolean
value?: DataValue
}
export interface IAssocListActions {
onSave: (bcNames: string[]) => void
onFilter: (bcName: string, filter: BcFilter) => void
onDeleteTag: (bcName: string, depth: number, widgetName: string, dataItem: AssociatedItem, assocValueKey: string) => void
onDeleteAssociations: (bcName: string, parentId: string, depth: number, assocValueKey: string, selected: boolean) => void
onRemoveFilter?: (bcName: string, filter: BcFilter) => void
onCancel: () => void
onClose: () => void
}
export interface IAssocListOwnProps extends Omit<PopupProps, 'bcName' | 'children' | 'showed'> {
widget: WidgetTableMeta
components?: {
title?: React.ReactNode
table?: React.ReactNode
footer?: React.ReactNode
}
}
export interface IAssocListProps extends IAssocListOwnProps {
/**
* @deprecated TODO: Remove in 2.0.0, now handled by Widget.tsx
*/
showed?: boolean
assocValueKey?: string
associateFieldKey?: string
bcLoading: boolean
pendingDataChanges?: {
[cursor: string]: PendingDataItem
}
data?: AssociatedItem[]
bcFilters?: BcFilter[]
isFilter?: boolean
calleeBCName?: string
calleeWidgetName?: string
}
const emptyData: AssociatedItem[] = []
type AssociatedItemTag = Omit<AssociatedItem, 'vstamp'> & {
_closable?: boolean
_value?: string
}
/**
*
* @param props
* @category Widgets
*/
export const AssocListPopup: FunctionComponent<IAssocListProps & IAssocListActions> = ({
onCancel,
onClose,
onSave,
bcFilters,
onFilter,
onRemoveFilter,
onDeleteTag,
onDeleteAssociations,
data,
width,
components,
widget,
assocValueKey,
associateFieldKey,
bcLoading,
pendingDataChanges,
isFilter,
calleeBCName,
calleeWidgetName,
...rest
}) => {
const pendingBcNames = widget.options?.hierarchy
? [widget.bcName, ...widget.options?.hierarchy.map(item => item.bcName)]
: [widget.bcName]
const selectedRecords = useAssocRecords(data, pendingDataChanges)
const saveData = React.useCallback(() => {
onSave(pendingBcNames)
onClose()
}, [pendingBcNames, onSave, onClose])
const viewName = useSelector((store: Store) => {
return store.view.name
})
const filterData = React.useCallback(() => {
const filterValue = selectedRecords.map(item => item.id)
if (filterValue.length > 0) {
onFilter(calleeBCName, {
type: FilterType.equalsOneOf,
fieldName: associateFieldKey,
value: filterValue,
viewName,
widgetName: calleeWidgetName
})
} else {
const currentFilters = bcFilters?.find(filterItem => filterItem.fieldName === associateFieldKey)?.value
currentFilters
? onRemoveFilter?.(calleeBCName, {
type: FilterType.equalsOneOf,
fieldName: associateFieldKey,
value: currentFilters
})
: $do.emptyAction(null)
}
onClose()
}, [onFilter, onRemoveFilter, bcFilters, onClose, calleeBCName, associateFieldKey, selectedRecords, calleeWidgetName, viewName])
const cancelData = React.useCallback(() => {
onCancel()
onClose()
}, [onCancel, onClose])
const handleDeleteTag = React.useCallback(
(val: DataItem) => {
if (widget.options?.hierarchyGroupDeselection) {
onDeleteAssociations(widget.bcName, val.id, (val.level as number) + 1, assocValueKey, false)
}
onDeleteTag(widget.bcName, val.level as number, widget.name, { ...val, _associate: false } as AssociatedItem, assocValueKey)
},
[onDeleteTag, widget, assocValueKey, onDeleteAssociations]
)
// Tag values limit
const tagLimit = 5
const visibleTags = selectedRecords
.map(item => ({
...item,
_value: String(item[assocValueKey] || ''),
_closable: true
}))
.slice(0, tagLimit)
const hiddenTagsCount = selectedRecords.length - tagLimit
const tags: AssociatedItemTag[] =
selectedRecords.length > tagLimit
? [...visibleTags, { id: 'control', _associate: false, _value: `... ${hiddenTagsCount}` }]
: selectedRecords.map(item => ({ ...item, _value: String(item[assocValueKey] || ''), _closable: true }))
const defaultTitle = tags.length ? (
<div>
<div>
<h1 className={styles.title}>{widget.title}</h1>
</div>
<div className={styles.tagArea}>
{assocValueKey &&
tags?.map(val => {
return (
<Tag
title={val._value?.toString()}
closable={val._closable}
id={val.id?.toString()}
key={val.id?.toString()}
onClose={() => {
handleDeleteTag(val as AssociatedItem)
}}
>
{val._value}
</Tag>
)
})}
</div>
</div>
) : (
widget.title
)
const title = components?.title === undefined ? defaultTitle : components.title
const defaultTable =
widget.options?.hierarchy || widget.options?.hierarchyFull ? (
widget.options.hierarchyFull ? (
<FullHierarchyTable meta={widget} assocValueKey={assocValueKey} selectable />
) : (
<HierarchyTable meta={widget} assocValueKey={assocValueKey} selectable />
)
) : (
<AssocTable meta={widget} disablePagination={true} />
)
const table = components?.table === undefined ? defaultTable : components.table
return (
<Popup
title={title}
showed
width={width}
size="large"
onOkHandler={isFilter ? filterData : saveData}
onCancelHandler={cancelData}
bcName={widget.bcName}
widgetName={widget.name}
disablePagination={widget.options?.hierarchyFull}
footer={components?.footer}
{...rest}
>
{bcLoading ? <Skeleton loading paragraph={{ rows: 5 }} /> : { ...table }}
</Popup>
)
}
function mapStateToProps(store: Store, ownProps: IAssocListOwnProps) {
const bcName = ownProps.widget?.bcName
const bc = store.screen.bo.bc[bcName]
const isFilter = store.view.popupData.isFilter
const calleeBCName = store.view.popupData.calleeBCName
const calleeWidgetName = store.view.popupData.calleeWidgetName
const associateFieldKey = store.view.popupData.associateFieldKey
const data = store.data[bcName] || emptyData
const bcFilters = store.screen.filters?.[calleeBCName]
const filterDataItems = bcFilters?.find(filterItem => filterItem.fieldName === associateFieldKey)?.value as DataItem[]
if (isFilter && filterDataItems?.length > 0) {
data?.forEach(dataItem => {
if (filterDataItems.includes((dataItem.id as unknown) as DataItem)) {
dataItem._associate = true
}
})
}
return {
assocValueKey: store.view.popupData.assocValueKey,
associateFieldKey: associateFieldKey,
bcLoading: bc?.loading,
pendingDataChanges: store.view.pendingDataChanges[bcName],
data: data,
bcFilters,
isFilter,
calleeBCName,
calleeWidgetName
}
}
const mapDispatchToProps = createMapDispatchToProps(
(props: IAssocListOwnProps) => {
return {
bcName: props.widget.bcName, // TODO: use widgetName instead
// widgetName: props.widget.name,
isFullHierarchy: !!props.widget.options?.hierarchyFull
}
},
(ctx): IAssocListActions => {
return {
onCancel: () => {
ctx.dispatch($do.closeViewPopup({ bcName: ctx.props.bcName }))
ctx.dispatch($do.bcRemoveAllFilters({ bcName: ctx.props.bcName }))
if (ctx.props.isFullHierarchy) {
ctx.dispatch($do.bcCancelPendingChanges({ bcNames: [ctx.props.bcName] }))
}
},
onClose: () => {
ctx.dispatch($do.closeViewPopup({ bcName: ctx.props.bcName }))
ctx.dispatch($do.bcRemoveAllFilters({ bcName: ctx.props.bcName }))
if (ctx.props.isFullHierarchy) {
ctx.dispatch($do.bcCancelPendingChanges({ bcNames: [ctx.props.bcName] }))
}
},
onSave: (bcNames: string[]) => {
ctx.dispatch($do.saveAssociations({ bcNames }))
if (ctx.props.isFullHierarchy) {
ctx.dispatch($do.bcCancelPendingChanges({ bcNames: [ctx.props.bcName] }))
}
},
onDeleteAssociations: (bcName: string, parentId: string, depth: number, assocValueKey: string, selected: boolean) => {
ctx.dispatch($do.changeDescendantsAssociationsFull({ bcName, parentId, depth, assocValueKey, selected }))
},
onDeleteTag: (bcName: string, depth: number, widgetName: string, dataItem: AssociatedItem, assocValueKey: string) => {
ctx.dispatch(
$do.changeAssociationFull({
bcName,
depth,
widgetName,
dataItem,
assocValueKey
})
)
},
onFilter: (bcName: string, filter: BcFilter) => {
ctx.dispatch($do.bcAddFilter({ bcName, filter }))
ctx.dispatch($do.bcForceUpdate({ bcName }))
},
onRemoveFilter: (bcName: string, filter: BcFilter) => {
ctx.dispatch($do.bcRemoveFilter({ bcName, filter }))
ctx.dispatch($do.bcForceUpdate({ bcName }))
}
}
}
)
/**
* @category Widgets
*/
const AssocListPopupConnected = connect(mapStateToProps, mapDispatchToProps)(AssocListPopup)
export default AssocListPopupConnected