datastake-daf 0.6.229 → 0.6.230

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,639 +13,586 @@ import { getLocales } from "../../../../../../helpers/Table";
13
13
  import ModalHeader from "../../../Header/ModalHeader/index.jsx";
14
14
  import { getToken } from "../../../../../../helpers/Token";
15
15
  import DynamicForm from "../../../DynamicForm/index.jsx";
16
- import { convertUndefinedToNull } from "../../../../../utils/object";
16
+ import { convertUndefinedToNull } from '../../../../../utils/object';
17
17
 
18
- const changeErrors = () => {};
18
+ const changeErrors = () => { };
19
19
 
20
20
  export default function DataLink({
21
- form = {},
22
- values,
23
- formsValue,
24
- maxRepeat,
25
- onUpdate = () => {},
26
- remove,
27
- name,
28
- add,
29
- flat,
30
- k,
31
- isHighlighted,
21
+ form = {},
22
+ values,
23
+ formsValue,
24
+ maxRepeat,
25
+ onUpdate = () => { },
26
+ remove,
27
+ name,
28
+ add,
29
+ flat,
30
+ k,
31
+ isHighlighted,
32
32
  }) {
33
- const {
34
- t,
35
- ajaxOptions,
36
- user,
37
- ajaxForms,
38
- changeAjaxForms,
39
- getApiBaseUrl,
40
- getAppHeader,
41
- app,
42
- ...rest
43
- } = useEditContext();
44
- const [rowToEdit, setRowToEdit] = useState();
45
- const [linkingData, setLinkingData] = useState({});
46
- const [addModalRow, setAddModalRow] = useState(null);
47
- const { automaticallyLink } = useMemo(() => form, [form]);
48
- const entity = useMemo(
49
- () => automaticallyLink?.entity || form?.meta?.dataLinkEntity,
50
- [automaticallyLink, form],
51
- );
52
- const isAjaxModal = useMemo(() => !!form?.meta?.namespace, [form]);
53
- const isSingle = maxRepeat === 1;
54
- const notAvailable = (values?.meta?.inputs || {})[k]?.notAvailable;
55
- const notApplicable = (values?.meta?.inputs || {})[k]?.notApplicable;
56
-
57
- // const formData = isSingle ? (values[name] ? [{ ...values[name] }] : []) : getArray(values[name] || []).map((v, i) => ({ ...v, key: i }));
58
-
59
- const formData = isSingle
60
- ? Array.isArray(values[name])
61
- ? values[name].length === 0
62
- ? []
63
- : [{ ...values[name] }]
64
- : values[name]
65
- ? [{ ...values[name] }]
66
- : []
67
- : getArray(values[name] || []).map((v, i) => ({ ...v, key: i }));
68
-
69
- const noBody = notApplicable || notAvailable;
70
-
71
- const tableKeys = useMemo(() => {
72
- let val = form?.meta?.tableKeys;
73
- let toReturn = val;
74
-
75
- if (val && !Array.isArray(val)) {
76
- if (typeof val === "object") {
77
- Object.keys(val).forEach((k) => {
78
- const show = showHideInput({ showIf: k }, values);
79
-
80
- if (show) {
81
- toReturn = val[k];
82
- }
83
- });
84
- }
85
- }
86
-
87
- return toReturn;
88
- }, [form, values]);
89
-
90
- const prefilledValues = useMemo(() => {
91
- return Object.keys(form?.meta?.prefilledValues || {}).reduce((all, key) => {
92
- const val = form.meta.prefilledValues[key];
93
-
94
- if (typeof val === "object") {
95
- Object.keys(val).forEach((k) => {
96
- const show = showHideInput({ showIf: k }, values);
97
-
98
- if (show) {
99
- all[key] = val[k];
100
- }
101
- });
102
- } else {
103
- all[key] = val;
104
- }
105
-
106
- return all;
107
- }, {});
108
- }, [form, values]);
109
-
110
- const formScope = useMemo(() => {
111
- const val = form.meta.formScope;
112
- let toReturn = val;
113
-
114
- if (val) {
115
- if (typeof val === "object") {
116
- Object.keys(val).forEach((k) => {
117
- const show = showHideInput({ showIf: k }, values);
118
-
119
- if (show) {
120
- toReturn = val[k];
121
- }
122
- });
123
- }
124
- }
125
-
126
- return toReturn;
127
- }, [form, values]);
128
-
129
- useEffect(() => {
130
- if (values.linking && values.linking[entity]) {
131
- setLinkingData(values.linking[entity]);
132
- }
133
- if (values?.parent?.linking && values?.parent?.linking[entity]) {
134
- setLinkingData(values?.parent?.linking[entity]);
135
- }
136
- }, [entity, values.linking]);
137
-
138
- const ajaxModalValues = useAjaxModal({
139
- name: k,
140
- user,
141
- namespace: form?.meta?.namespace,
142
- skipFetch: form?.meta?.skipFetch,
143
- isAjaxModal,
144
- formScope,
145
- });
146
-
147
- const data = (formData || []).map((f, i) => {
148
- if (isAjaxModal && typeof f === "string" && linkingData[f]) {
149
- const value = linkingData[f];
150
- return { ...value, key: i };
151
- }
152
-
153
- if (form?.meta?.ajaxOptionsKey) {
154
- if (linkingData[f[form?.meta?.ajaxOptionsKey]]) {
155
- const value = linkingData[f[form?.meta?.ajaxOptionsKey]];
156
- const resData = { ...f, ...value, key: i };
157
- if (resData.id && !linkingData[resData.id]) {
158
- setLinkingData({
159
- ...linkingData,
160
- [resData.id]: resData,
161
- });
162
- }
163
- }
164
-
165
- const options = ajaxOptions[form?.meta?.ajaxOptionsKey];
166
-
167
- if (Array.isArray(options)) {
168
- const value = options.find((v) => v.value === f[form?.meta?.ajaxOptionsKey]);
169
- if (value) {
170
- const resData = { ...f, ...value, key: i };
171
- if (resData.id && !linkingData[resData.id]) {
172
- setLinkingData({
173
- ...linkingData,
174
- [resData.id]: resData,
175
- });
176
- }
177
- return resData;
178
- }
179
- }
180
- }
181
-
182
- const resData = { ...f, key: i };
183
- if (resData.id && !linkingData[resData.id]) {
184
- setLinkingData({
185
- ...linkingData,
186
- [resData.id]: resData,
187
- });
188
- }
189
- return resData;
190
- });
191
-
192
- const getDataSource = () => {
193
- if (notApplicable || notAvailable) {
194
- return [];
195
- }
196
-
197
- if (data.length > 3) {
198
- return data;
199
- }
200
-
201
- const max = isSingle ? 1 : 3;
202
-
203
- const emptyDataSource = [];
204
-
205
- for (let i = 0; i < max; i++) {
206
- emptyDataSource.push({ key: i, isEmpty: true });
207
- }
208
-
209
- return data.length !== 0
210
- ? emptyDataSource.map((empty, i) => data[i] || empty)
211
- : emptyDataSource;
212
- };
213
-
214
- const dataSource = getDataSource();
215
-
216
- const preSelected = data
217
- .map((d) => d.id)
218
- .filter((d) => !!d)
219
- .filter((d) => d !== rowToEdit?.id);
220
-
221
- const onCloseModal = useCallback(() => {
222
- setRowToEdit(null);
223
- setAddModalRow(false);
224
- }, []);
225
-
226
- const inputs = useMemo(
227
- () =>
228
- Object.values(ajaxModalValues.form)[0]
229
- ? Object.values(ajaxModalValues.form)[0]
230
- : form?.inputs || {},
231
- [form, ajaxModalValues.form],
232
- );
233
-
234
- const mapKey = useCallback(
235
- (val) => {
236
- const key = Array.isArray(val.content) && val.content.length ? val.content[0].key : val;
237
-
238
- const input = (inputs[key] || {}).tableLabel || inputs[key] || {};
239
- const _input = inputs[key] || {};
240
- const title =
241
- typeof input === "string"
242
- ? input
243
- : getInputLabel(
244
- (inputs[key] || {}).tableLabel || inputs[key] || {},
245
- values,
246
- true,
247
- );
248
-
249
- const getKey = (row) => {
250
- if (Array.isArray(val.content) && val.content.length) {
251
- for (let i = 0; i < val.content.length; i++) {
252
- const config = val.content[i];
253
- const show = showHideInput(config, row);
254
- if (show) {
255
- return config.key;
256
- }
257
- }
258
- }
259
-
260
- return key;
261
- };
262
-
263
- return {
264
- title: <Tooltip title={title}>{title}</Tooltip>,
265
- key: key,
266
- dataIndex: key,
267
- ellipsis: { showTitle: true },
268
- render: (_, all) => {
269
- if (all.isEmpty || ajaxModalValues.isFetching) {
270
- return <div className="daf-default-cell" />;
271
- }
272
-
273
- const key = getKey(all);
274
- const v = all[key] || all;
275
- const type = inputs[key]?.type;
276
-
277
- const render = (title) => {
278
- if (tooltipInputs.includes(type)) {
279
- return <Tooltip title={title}>{title} </Tooltip>;
280
- }
281
-
282
- return title;
283
- };
284
-
285
- const inputMeta = (all?.meta?.inputs || {})[key] || {};
286
-
287
- if (key === "datastakeId" && (!all[key] || all[key] === "new")) {
288
- return "";
289
- }
290
-
291
- if (
292
- v ||
293
- inputMeta?.notApplicable ||
294
- inputMeta?.notAvailable ||
295
- type === "ajaxSelect"
296
- ) {
297
- if (type === "ajaxSelect") {
298
- const data = all[key];
299
-
300
- if (data === "new") {
301
- return render(
302
- <BasicInput
303
- name={key}
304
- key={key}
305
- isModal
306
- config={{
307
- ...inputs[key],
308
- type: "text",
309
- }}
310
- valueOnlyString
311
- data={{
312
- ...all,
313
- [key]:
314
- (all?.meta?.inputs || {})[key]?.name ||
315
- all[_input?.meta?.onNewSetValueKey],
316
- scope: formScope,
317
- }}
318
- t={t}
319
- ajaxForms={ajaxForms}
320
- changeAjaxForms={changeAjaxForms}
321
- getApiBaseUrl={getApiBaseUrl}
322
- getAppHeader={getAppHeader}
323
- user={user}
324
- getToken={getToken}
325
- app={app}
326
- />,
327
- );
328
- }
329
-
330
- const { meta } = inputs[key];
331
-
332
- if (meta.onNewSetValueKey) {
333
- if (!all[meta.onNewSetValueKey]) {
334
- return render(
335
- <BasicInput
336
- t={t}
337
- isModal
338
- app={app}
339
- ajaxForms={ajaxForms}
340
- changeAjaxForms={changeAjaxForms}
341
- getApiBaseUrl={getApiBaseUrl}
342
- getAppHeader={getAppHeader}
343
- user={user}
344
- getToken={getToken}
345
- name={key}
346
- key={key}
347
- config={{
348
- ...inputs[key],
349
- type: "ajaxSelect",
350
- }}
351
- valueOnlyString
352
- data={{ ...all, scope: data?.scope }}
353
- linkingData={all?.linking}
354
- />,
355
- );
356
- }
357
- return render(
358
- <BasicInput
359
- isModal
360
- name={key}
361
- key={key}
362
- config={{
363
- ...inputs[key],
364
- type: "text",
365
- }}
366
- t={t}
367
- ajaxForms={ajaxForms}
368
- changeAjaxForms={changeAjaxForms}
369
- getApiBaseUrl={getApiBaseUrl}
370
- getAppHeader={getAppHeader}
371
- user={user}
372
- getToken={getToken}
373
- app={app}
374
- valueOnlyString
375
- data={{
376
- ...all,
377
- [key]: all[meta.onNewSetValueKey],
378
- scope: formScope,
379
- }}
380
- />,
381
- );
382
- }
383
- }
384
-
385
- return render(
386
- <BasicInput
387
- isModal
388
- name={key}
389
- key={key}
390
- t={t}
391
- ajaxForms={ajaxForms}
392
- changeAjaxForms={changeAjaxForms}
393
- getApiBaseUrl={getApiBaseUrl}
394
- getAppHeader={getAppHeader}
395
- user={user}
396
- getToken={getToken}
397
- app={app}
398
- config={inputs[key]}
399
- valueOnlyString
400
- data={{ ...all, scope: formScope }}
401
- />,
402
- );
403
- }
404
-
405
- if ((inputs[key] || {}).noValue) {
406
- return render(inputs[key] || {}).noValue[user?.language];
407
- }
408
- },
409
- };
410
- },
411
- [inputs, ajaxModalValues, formScope],
412
- );
413
-
414
- const onEditClick = useCallback(
415
- (all) => {
416
- if (form?.meta?.ajaxOptionsKey) {
417
- const _all = Object.keys(all).reduce((val, key) => {
418
- val[key] = all[key];
419
- return val;
420
- }, {});
421
-
422
- setRowToEdit(
423
- form?.meta?.linkingKey
424
- ? {
425
- ..._all,
426
- key: all.key,
427
- meta: all.meta,
428
- scope: formScope,
429
- [form?.meta?.ajaxOptionsKey]:
430
- all.id || all.value || all[form?.meta?.ajaxOptionsKey],
431
- [form?.meta?.linkingKey]:
432
- all._id ||
433
- all.id ||
434
- all.value ||
435
- all[form?.meta?.ajaxOptionsKey] ||
436
- all[form?.meta?.linkingKey],
437
- }
438
- : {
439
- ..._all,
440
- scope: formScope,
441
- key: all.key,
442
- meta: all.meta,
443
- [form?.meta?.ajaxOptionsKey]:
444
- all.id || all.value || all[form?.meta?.ajaxOptionsKey],
445
- },
446
- );
447
- return;
448
- }
449
-
450
- setRowToEdit({ ...all, scope: formScope });
451
- },
452
- [form, formScope],
453
- );
454
-
455
- const canAdd = (maxRepeat ? formData.length < maxRepeat : true) && !noBody;
456
-
457
- const columns = [
458
- ...(Array.isArray(tableKeys)
459
- ? tableKeys.map(mapKey)
460
- : Object.keys(inputs || {}).map(mapKey)),
461
- {
462
- key: "actions",
463
- dataIndex: "actions",
464
- title: (
465
- <Button
466
- size="small"
467
- className="add-btn"
468
- disabled={!canAdd || (isAjaxModal ? ajaxModalValues?.isFetching : false)}
469
- onClick={() => {
470
- const _values = (form?.meta?.subjectPrefilledValuesKeys || []).reduce(
471
- (all, key) => {
472
- if (values[key]) {
473
- all[key] = values[key];
474
- }
475
- return all;
476
- },
477
- {},
478
- );
479
-
480
- const _prefilled = {};
481
-
482
- Object.keys(_form?.inputs || {}).forEach((key) => {
483
- if (_form?.inputs?.[key]?.meta?.defaultValue) {
484
- _prefilled[key] = _form?.inputs?.[key]?.meta?.defaultValue;
485
- }
486
- });
487
-
488
- setAddModalRow({
489
- ...(_values || {}),
490
- ...(prefilledValues || {}),
491
- ..._prefilled,
492
- scope: formScope,
493
- });
494
- }}
495
- >
496
- <CustomIcon name="Add" width={16} height={16} />
497
- </Button>
498
- ),
499
- width: 70,
500
- render: (_, all) => {
501
- if (all.isEmpty || ajaxModalValues.isFetching) {
502
- return <div className="daf-default-cell" />;
503
- }
504
-
505
- const items = [];
506
-
507
- const canEdit =
508
- form?.meta?.allowedActions?.edit === false
509
- ? false
510
- : form?.meta?.allowedActions?.edit || (isSingle && formData.length === 1);
511
-
512
- if (canEdit) {
513
- items.push({
514
- label: t("Edit"),
515
- key: "edit",
516
- onClick: () => onEditClick(all),
517
- });
518
- }
519
-
520
- if (form?.meta?.allowedActions?.remove) {
521
- items.push({
522
- label: t("Delete"),
523
- key: "remove",
524
- onClick: () => {
525
- Modal.confirm({
526
- title: t("Delete information"),
527
- content: t("Are you sure you want to delete this information?"),
528
- okText: t("Yes"),
529
- cancelText: t("Cancel"),
530
- onOk: () => {
531
- remove(all);
532
- },
533
- });
534
- },
535
- });
536
- }
537
-
538
- if (items.length) {
539
- return <MoreMenu items={items} />;
540
- }
541
-
542
- return null;
543
- },
544
- },
545
- ];
546
-
547
- const unsortedForm = { inputs };
548
-
549
- const _form = Object.keys(unsortedForm).reduce((all, key) => {
550
- const _this = Object.keys(unsortedForm[key])
551
- .sort((a, b) => unsortedForm[key][a].position - unsortedForm[key][b].position)
552
- .reduce((all, k) => {
553
- if (k === "scope") {
554
- return all;
555
- }
556
-
557
- all[k] = unsortedForm[key][k];
558
- return all;
559
- }, {});
560
-
561
- all[key] = _this;
562
- return all;
563
- }, {});
564
-
565
- console.log("form", dataSource);
566
-
567
- return (
568
- <div
569
- className={formatClassname([
570
- "data-link-cont mt-2",
571
- noBody && "no-body",
572
- isHighlighted && "highlighted",
573
- ])}
574
- style={form?.meta?.style}
575
- >
576
- <div className="daf-table-wrapper no-padding repeatable-form-table no-pagination">
577
- <ConfigProvider
578
- renderEmpty={() =>
579
- notApplicable ? t("Not applicable") : t("No information available")
580
- }
581
- >
582
- <Table
583
- locale={getLocales(t)}
584
- loading={ajaxModalValues.isFetching}
585
- pagination={false}
586
- columns={columns}
587
- dataSource={dataSource}
588
- rowKey="key"
589
- scroll={noBody ? undefined : { y: 170 }}
590
- />
591
- </ConfigProvider>
592
- </div>
593
-
594
- <Modal
595
- open={!!addModalRow || !!rowToEdit}
596
- footer={null}
597
- width={650}
598
- className="max-h-50"
599
- destroyOnClose
600
- onCancel={onCloseModal}
601
- title={
602
- <ModalHeader
603
- title={getLabelFromScopeForDataLink(
604
- form?.modalLabel || form?.label,
605
- formScope,
606
- )}
607
- />
608
- }
609
- >
610
- <div className="repeatable-modal-form">
611
- <DynamicForm
612
- isModal
613
- showSaveAndNext={false}
614
- form={_form}
615
- code={form?.meta?.code}
616
- excludedKeys={[...(form?.meta?.excludedKeys || [])]}
617
- data={{ ...(rowToEdit || addModalRow || {}) }}
618
- disabledInputs={form?.meta?.disabledKeys || []}
619
- onCancel={onCloseModal}
620
- preSelected={preSelected}
621
- changeErrors={changeErrors}
622
- staticWidth="500px"
623
- submit={(_payload) => {
624
- const { form, ...payload } = _payload;
625
-
626
- if (rowToEdit) {
627
- const { key, ...rest } = payload;
628
- onUpdate(rest, key);
629
- onCloseModal();
630
- } else {
631
- add(convertUndefinedToNull(payload));
632
- onCloseModal();
633
- }
634
- }}
635
- {...{
636
- t,
637
- ajaxOptions,
638
- user,
639
- ajaxForms,
640
- changeAjaxForms,
641
- getApiBaseUrl,
642
- getAppHeader,
643
- app,
644
- ...rest,
645
- }}
646
- />
647
- </div>
648
- </Modal>
649
- </div>
650
- );
33
+ const {
34
+ t,
35
+ ajaxOptions,
36
+ user,
37
+ ajaxForms,
38
+ changeAjaxForms,
39
+ getApiBaseUrl,
40
+ getAppHeader,
41
+ app,
42
+ ...rest
43
+ } = useEditContext();
44
+ const [rowToEdit, setRowToEdit] = useState();
45
+ const [linkingData, setLinkingData] = useState({});
46
+ const [addModalRow, setAddModalRow] = useState(null);
47
+ const { automaticallyLink } = useMemo(() => form, [form]);
48
+ const entity = useMemo(() => automaticallyLink?.entity || form?.meta?.dataLinkEntity, [automaticallyLink, form]);
49
+ const isAjaxModal = useMemo(() => !!form?.meta?.namespace, [form]);
50
+ const isSingle = maxRepeat === 1;
51
+ const notAvailable = (values?.meta?.inputs || {})[k]?.notAvailable;
52
+ const notApplicable = (values?.meta?.inputs || {})[k]?.notApplicable;
53
+
54
+ // const formData = isSingle ? (values[name] ? [{ ...values[name] }] : []) : getArray(values[name] || []).map((v, i) => ({ ...v, key: i }));
55
+
56
+ const formData = isSingle
57
+ ? Array.isArray(values[name])
58
+ ? values[name].length === 0
59
+ ? []
60
+ : [{ ...values[name] }]
61
+ : values[name] ? [{ ...values[name] }] : []
62
+ : getArray(values[name] || []).map((v, i) => ({ ...v, key: i }));
63
+
64
+
65
+ const noBody = notApplicable || notAvailable;
66
+
67
+ const tableKeys = useMemo(() => {
68
+ let val = form?.meta?.tableKeys;
69
+ let toReturn = val;
70
+
71
+ if (val && !Array.isArray(val)) {
72
+ if (typeof val === 'object') {
73
+ Object.keys(val).forEach((k) => {
74
+ const show = showHideInput(
75
+ { showIf: k },
76
+ values,
77
+ );
78
+
79
+ if (show) {
80
+ toReturn = val[k];
81
+ }
82
+ })
83
+ }
84
+ }
85
+
86
+ return toReturn;
87
+ }, [form, values]);
88
+
89
+ const prefilledValues = useMemo(() => {
90
+ return Object.keys(form?.meta?.prefilledValues || {}).reduce((all, key) => {
91
+ const val = form.meta.prefilledValues[key];
92
+
93
+ if (typeof val === 'object') {
94
+ Object.keys(val).forEach((k) => {
95
+ const show = showHideInput(
96
+ { showIf: k },
97
+ values,
98
+ );
99
+
100
+ if (show) {
101
+ all[key] = val[k];
102
+ }
103
+ })
104
+ } else {
105
+ all[key] = val;
106
+ }
107
+
108
+ return all;
109
+ }, {});
110
+ }, [form, values]);
111
+
112
+ const formScope = useMemo(() => {
113
+ const val = form.meta.formScope;
114
+ let toReturn = val;
115
+
116
+ if (val) {
117
+ if (typeof val === 'object') {
118
+ Object.keys(val).forEach((k) => {
119
+ const show = showHideInput(
120
+ { showIf: k },
121
+ values,
122
+ );
123
+
124
+ if (show) {
125
+ toReturn = val[k];
126
+ }
127
+ })
128
+ }
129
+ }
130
+
131
+ return toReturn;
132
+ }, [form, values]);
133
+
134
+ useEffect(() => {
135
+ if (values.linking && values.linking[entity]) {
136
+ setLinkingData(values.linking[entity]);
137
+ }
138
+ if (values?.parent?.linking && values?.parent?.linking[entity]) {
139
+ setLinkingData(values?.parent?.linking[entity]);
140
+ }
141
+ }, [entity, values.linking])
142
+
143
+ const ajaxModalValues = useAjaxModal({
144
+ name: k,
145
+ user,
146
+ namespace: form?.meta?.namespace,
147
+ skipFetch: form?.meta?.skipFetch,
148
+ isAjaxModal,
149
+ formScope,
150
+ })
151
+
152
+ const data = (formData || []).map((f, i) => {
153
+ if (isAjaxModal && typeof f === 'string' && linkingData[f]) {
154
+ const value = linkingData[f];
155
+ return { ...value, key: i };
156
+ }
157
+
158
+ if (form?.meta?.ajaxOptionsKey) {
159
+ if (linkingData[f[form?.meta?.ajaxOptionsKey]]) {
160
+ const value = linkingData[f[form?.meta?.ajaxOptionsKey]];
161
+ const resData = { ...f, ...value, key: i };
162
+ if (resData.id && !linkingData[resData.id]) {
163
+ setLinkingData({
164
+ ...linkingData,
165
+ [resData.id]: resData,
166
+ })
167
+ }
168
+ }
169
+
170
+ const options = ajaxOptions[form?.meta?.ajaxOptionsKey];
171
+
172
+ if (Array.isArray(options)) {
173
+ const value = options.find((v) => v.value === f[form?.meta?.ajaxOptionsKey])
174
+ if (value) {
175
+ const resData = { ...f, ...value, key: i };
176
+ if (resData.id && !linkingData[resData.id]) {
177
+ setLinkingData({
178
+ ...linkingData,
179
+ [resData.id]: resData,
180
+ })
181
+ }
182
+ return resData;
183
+ }
184
+ }
185
+ }
186
+
187
+ const resData = { ...f, key: i };
188
+ if (resData.id && !linkingData[resData.id]) {
189
+ setLinkingData({
190
+ ...linkingData,
191
+ [resData.id]: resData,
192
+ })
193
+ }
194
+ return resData;
195
+ });
196
+
197
+ const getDataSource = () => {
198
+ if (notApplicable || notAvailable) {
199
+ return [];
200
+ }
201
+
202
+ if (data.length > 3) {
203
+ return data;
204
+ }
205
+
206
+ const max = isSingle ? 1 : 3;
207
+
208
+ const emptyDataSource = [];
209
+
210
+ for (let i = 0; i < max; i++) {
211
+ emptyDataSource.push({ key: i, isEmpty: true });
212
+ }
213
+
214
+ return data.length !== 0 ? emptyDataSource.map((empty, i) => data[i] || empty) : emptyDataSource;
215
+ };
216
+
217
+ const dataSource = getDataSource();
218
+
219
+ const preSelected = data.map((d) => d.id).filter((d) => !!d).filter((d) => d !== rowToEdit?.id);
220
+
221
+ const onCloseModal = useCallback(() => {
222
+ setRowToEdit(null);
223
+ setAddModalRow(false);
224
+ }, []);
225
+
226
+ const inputs = useMemo(() =>
227
+ Object.values(ajaxModalValues.form)[0]
228
+ ? Object.values(ajaxModalValues.form)[0]
229
+ : form?.inputs || {}
230
+ , [form, ajaxModalValues.form])
231
+
232
+ const mapKey = useCallback((val) => {
233
+ const key = Array.isArray(val.content) && val.content.length
234
+ ? val.content[0].key
235
+ : val;
236
+
237
+ const input = (inputs[key] || {}).tableLabel || (inputs[key] || {});
238
+ const _input = (inputs[key] || {});
239
+ const title = typeof input === 'string'
240
+ ? input
241
+ : getInputLabel((inputs[key] || {}).tableLabel || (inputs[key] || {}), values, true);
242
+
243
+ const getKey = (row) => {
244
+ if (Array.isArray(val.content) && val.content.length) {
245
+ for (let i = 0; i < val.content.length; i++) {
246
+ const config = val.content[i];
247
+ const show = showHideInput(config, row);
248
+ if (show) {
249
+ return config.key;
250
+ }
251
+ }
252
+ }
253
+
254
+ return key;
255
+ }
256
+
257
+ return {
258
+ title: <Tooltip title={title}>{title}</Tooltip>,
259
+ key: key,
260
+ dataIndex: key,
261
+ ellipsis: { showTitle: true },
262
+ render: (_, all) => {
263
+ if (all.isEmpty || ajaxModalValues.isFetching) {
264
+ return <div className="daf-default-cell" />;
265
+ }
266
+
267
+ const key = getKey(all);
268
+ const v = all[key] || all;
269
+ const type = inputs[key]?.type;
270
+
271
+ const render = (title) => {
272
+ if (tooltipInputs.includes(type)) {
273
+ return (
274
+ <Tooltip title={title}>
275
+ {title} {' '}
276
+ </Tooltip>
277
+ );
278
+ }
279
+
280
+ return title;
281
+ };
282
+
283
+ const inputMeta = (all?.meta?.inputs || {})[key] || {};
284
+
285
+ if (key === 'datastakeId' && (!all[key] || all[key] === 'new')) {
286
+ return '';
287
+ }
288
+
289
+ if (v || inputMeta?.notApplicable || inputMeta?.notAvailable || type === 'ajaxSelect') {
290
+ if (type === 'ajaxSelect') {
291
+ const data = all[key];
292
+
293
+ if (data === 'new') {
294
+ return render(
295
+ <BasicInput
296
+ name={key}
297
+ key={key}
298
+ isModal
299
+ config={{
300
+ ...inputs[key],
301
+ type: 'text',
302
+ }}
303
+ valueOnlyString
304
+ data={{
305
+ ...all,
306
+ [key]: (all?.meta?.inputs || {})[key]?.name || all[_input?.meta?.onNewSetValueKey],
307
+ scope: formScope,
308
+ }}
309
+ t={t}
310
+ ajaxForms={ajaxForms}
311
+ changeAjaxForms={changeAjaxForms}
312
+ getApiBaseUrl={getApiBaseUrl}
313
+ getAppHeader={getAppHeader}
314
+ user={user}
315
+ getToken={getToken}
316
+ app={app}
317
+ />
318
+ );
319
+ }
320
+
321
+ const { meta } = inputs[key];
322
+
323
+ if (meta.onNewSetValueKey) {
324
+ if(!all[meta.onNewSetValueKey]) {
325
+ return render(
326
+ <BasicInput
327
+ t={t}
328
+ isModal
329
+ app={app}
330
+ ajaxForms={ajaxForms}
331
+ changeAjaxForms={changeAjaxForms}
332
+ getApiBaseUrl={getApiBaseUrl}
333
+ getAppHeader={getAppHeader}
334
+ user={user}
335
+ getToken={getToken}
336
+ name={key}
337
+ key={key}
338
+ config={{
339
+ ...inputs[key],
340
+ type: 'ajaxSelect',
341
+ }}
342
+ valueOnlyString
343
+ data={{ ...all, scope: data?.scope }}
344
+ linkingData={all?.linking}
345
+ />
346
+ )
347
+ }
348
+ return render(
349
+ <BasicInput
350
+ isModal
351
+ name={key}
352
+ key={key}
353
+ config={{
354
+ ...inputs[key],
355
+ type: 'text',
356
+ }}
357
+ t={t}
358
+ ajaxForms={ajaxForms}
359
+ changeAjaxForms={changeAjaxForms}
360
+ getApiBaseUrl={getApiBaseUrl}
361
+ getAppHeader={getAppHeader}
362
+ user={user}
363
+ getToken={getToken}
364
+ app={app}
365
+ valueOnlyString
366
+ data={{ ...all, [key]: all[meta.onNewSetValueKey], scope: formScope, }}
367
+ />
368
+ );
369
+ }
370
+ }
371
+
372
+ return render(
373
+ <BasicInput
374
+ isModal
375
+ name={key}
376
+ key={key}
377
+ t={t}
378
+ ajaxForms={ajaxForms}
379
+ changeAjaxForms={changeAjaxForms}
380
+ getApiBaseUrl={getApiBaseUrl}
381
+ getAppHeader={getAppHeader}
382
+ user={user}
383
+ getToken={getToken}
384
+ app={app}
385
+ config={inputs[key]}
386
+ valueOnlyString
387
+ data={{ ...all, scope: formScope, }}
388
+ />
389
+ );
390
+ }
391
+
392
+ if ((inputs[key] || {}).noValue) {
393
+ return render(inputs[key] || {}).noValue[user?.language]
394
+ }
395
+ },
396
+ }
397
+ }, [inputs, ajaxModalValues, formScope]);
398
+
399
+ const onEditClick = useCallback((all) => {
400
+ if (form?.meta?.ajaxOptionsKey) {
401
+ const _all = Object.keys(all)
402
+ .reduce((val, key) => {
403
+ val[key] = all[key];
404
+ return val;
405
+ }, {});
406
+
407
+ setRowToEdit(form?.meta?.linkingKey ? {
408
+ ..._all,
409
+ key: all.key,
410
+ meta: all.meta,
411
+ scope: formScope,
412
+ [form?.meta?.ajaxOptionsKey]: all.id || all.value || all[form?.meta?.ajaxOptionsKey],
413
+ [form?.meta?.linkingKey]: all._id || all.id || all.value || all[form?.meta?.ajaxOptionsKey] || all[form?.meta?.linkingKey],
414
+ } : {
415
+ ..._all,
416
+ scope: formScope,
417
+ key: all.key,
418
+ meta: all.meta,
419
+ [form?.meta?.ajaxOptionsKey]: all.id || all.value || all[form?.meta?.ajaxOptionsKey],
420
+ })
421
+ return;
422
+ }
423
+
424
+ setRowToEdit({ ...all, scope: formScope, })
425
+ }, [form, formScope]);
426
+
427
+ const canAdd = (maxRepeat ? formData.length < maxRepeat : true) && !noBody;
428
+
429
+ const columns = [
430
+ ...(Array.isArray(tableKeys) ? tableKeys.map(mapKey) : Object.keys(inputs || {}).map(mapKey)),
431
+ {
432
+ key: 'actions',
433
+ dataIndex: 'actions',
434
+ title: (
435
+ <Button
436
+ size="small"
437
+ className="add-btn"
438
+ disabled={!canAdd || (isAjaxModal ? ajaxModalValues?.isFetching : false)}
439
+ onClick={() => {
440
+ const _values = (form?.meta?.subjectPrefilledValuesKeys || []).reduce((all, key) => {
441
+ if (values[key]) {
442
+ all[key] = values[key];
443
+ }
444
+ return all;
445
+ }, {});
446
+
447
+ const _prefilled = {};
448
+
449
+ Object.keys(_form?.inputs || {}).forEach((key) => {
450
+ if (_form?.inputs?.[key]?.meta?.defaultValue) {
451
+ _prefilled[key] = _form?.inputs?.[key]?.meta?.defaultValue;
452
+ }
453
+ });
454
+
455
+ setAddModalRow({ ...(_values || {}), ...(prefilledValues || {}), ..._prefilled, scope: formScope, });
456
+ }}
457
+ >
458
+ <CustomIcon name="Add" width={16} height={16} />
459
+ </Button>
460
+ ),
461
+ width: 70,
462
+ render: (_, all) => {
463
+ if (all.isEmpty || ajaxModalValues.isFetching) {
464
+ return <div className="daf-default-cell" />;
465
+ }
466
+
467
+ const items = [];
468
+
469
+ const canEdit = form?.meta?.allowedActions?.edit === false
470
+ ? false
471
+ : form?.meta?.allowedActions?.edit || (isSingle && formData.length === 1);
472
+
473
+ if (canEdit) {
474
+ items.push({
475
+ label: t('Edit'),
476
+ key: "edit",
477
+ onClick: () => onEditClick(all),
478
+ });
479
+ }
480
+
481
+ if (form?.meta?.allowedActions?.remove) {
482
+ items.push({
483
+ label: t('Delete'),
484
+ key: "remove",
485
+ onClick: () => {
486
+ Modal.confirm({
487
+ title: t('Delete information'),
488
+ content: t('Are you sure you want to delete this information?'),
489
+ okText: t('Yes'),
490
+ cancelText: t('Cancel'),
491
+ onOk: () => {
492
+ remove(all);
493
+ },
494
+ })
495
+ },
496
+ });
497
+ }
498
+
499
+ if (items.length) {
500
+ return (
501
+ <MoreMenu items={items} />
502
+ )
503
+ }
504
+
505
+ return null;
506
+ }
507
+ }
508
+ ];
509
+
510
+ const unsortedForm = { inputs };
511
+
512
+ const _form = Object.keys(unsortedForm).reduce((all, key) => {
513
+ const _this = Object.keys(unsortedForm[key])
514
+ .sort((a, b) => unsortedForm[key][a].position - unsortedForm[key][b].position)
515
+ .reduce((all, k) => {
516
+ if (k === 'scope') {
517
+ return all;
518
+ }
519
+
520
+ all[k] = unsortedForm[key][k];
521
+ return all;
522
+ }, {});
523
+
524
+ all[key] = _this;
525
+ return all;
526
+ }, {});
527
+
528
+
529
+ return (
530
+ <div className={formatClassname(['data-link-cont mt-2', noBody && 'no-body', isHighlighted && 'highlighted'])} style={form?.meta?.style}>
531
+ <div className="daf-table-wrapper no-padding repeatable-form-table no-pagination">
532
+ <ConfigProvider renderEmpty={() => notApplicable ? t('Not applicable') : t('No information available')}>
533
+ <Table
534
+ locale={getLocales(t)}
535
+ loading={ajaxModalValues.isFetching}
536
+ pagination={false}
537
+ columns={columns}
538
+ dataSource={dataSource}
539
+ rowKey="key"
540
+ scroll={noBody ? undefined : { y: 170 }}
541
+ />
542
+ </ConfigProvider>
543
+ </div>
544
+
545
+ <Modal
546
+ open={!!addModalRow || !!rowToEdit}
547
+ footer={null}
548
+ width={650}
549
+ className="max-h-50"
550
+ destroyOnClose
551
+ onCancel={onCloseModal}
552
+ title={(
553
+ <ModalHeader title={getLabelFromScopeForDataLink(form?.modalLabel || form?.label, formScope)} />
554
+ )}
555
+ >
556
+ <div className="repeatable-modal-form">
557
+ <DynamicForm
558
+ isModal
559
+ showSaveAndNext={false}
560
+ form={_form}
561
+ code={form?.meta?.code}
562
+ excludedKeys={[...(form?.meta?.excludedKeys || [])]}
563
+ data={{ ...(rowToEdit || addModalRow || {}) }}
564
+ disabledInputs={form?.meta?.disabledKeys || []}
565
+ onCancel={onCloseModal}
566
+ preSelected={preSelected}
567
+ changeErrors={changeErrors}
568
+ staticWidth = '500px'
569
+ submit={(_payload) => {
570
+ const { form, ...payload } = _payload;
571
+
572
+ if (rowToEdit) {
573
+ const { key, ...rest } = payload;
574
+ onUpdate(rest, key);
575
+ onCloseModal();
576
+ } else {
577
+ add(convertUndefinedToNull(payload));
578
+ onCloseModal();
579
+ }
580
+ }}
581
+ {...({
582
+ t,
583
+ ajaxOptions,
584
+ user,
585
+ ajaxForms,
586
+ changeAjaxForms,
587
+ getApiBaseUrl,
588
+ getAppHeader,
589
+ app,
590
+ ...rest
591
+ })}
592
+ />
593
+ </div>
594
+
595
+ </Modal>
596
+ </div>
597
+ )
651
598
  }