@visns-studio/visns-components 1.5.5 → 1.5.6

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.
@@ -1,9 +1,9 @@
1
1
  import React, {
2
- forwardRef,
3
- useCallback,
4
- useEffect,
5
- useImperativeHandle,
6
- useState,
2
+ forwardRef,
3
+ useCallback,
4
+ useEffect,
5
+ useImperativeHandle,
6
+ useState,
7
7
  } from "react";
8
8
  import { useNavigate } from "react-router-dom";
9
9
  import axios from "axios";
@@ -19,19 +19,19 @@ import ReactDataGrid from "@inovua/reactdatagrid-community";
19
19
  import { confirmAlert } from "react-confirm-alert";
20
20
  import { toast } from "react-toastify";
21
21
  import {
22
- Backspace,
23
- Check,
24
- Clock,
25
- CloudDownload,
26
- Copy,
27
- Edit,
28
- Folder,
29
- Inbox,
30
- Network,
31
- Ribbon,
32
- Search,
33
- ShippingBoxV1,
34
- TriangleAlert,
22
+ Backspace,
23
+ Check,
24
+ Clock,
25
+ CloudDownload,
26
+ Copy,
27
+ Edit,
28
+ Folder,
29
+ Inbox,
30
+ Network,
31
+ Ribbon,
32
+ Search,
33
+ ShippingBoxV1,
34
+ TriangleAlert,
35
35
  } from "akar-icons";
36
36
 
37
37
  import "@inovua/reactdatagrid-community/index.css";
@@ -41,1497 +41,1496 @@ import CustomFetch from "./visns-fetch";
41
41
  import Form from "./visns-form";
42
42
 
43
43
  const loadData = async (
44
- { skip, limit, sortInfo, currentData, filterValue, groupBy },
45
- search,
46
- ajaxSetting
44
+ { skip, limit, sortInfo, currentData, filterValue, groupBy },
45
+ search,
46
+ ajaxSetting
47
47
  ) => {
48
- const url = ajaxSetting.url;
49
- const page = Math.floor(skip / limit) + 1;
50
- const params = { page, sortInfo, take: limit, search: search, where: [] };
51
-
52
- if (sortInfo) {
53
- params.sortBy = sortInfo.name;
54
- params.sort = sortInfo.dir === 1 ? "asc" : "desc";
55
- }
56
-
57
- if (ajaxSetting && ajaxSetting.where) {
58
- params.where = ajaxSetting.where;
59
- }
60
-
61
- if (filterValue) {
62
- filterValue.forEach((fv) => {
63
- const identifier = fv.key ? fv.key : fv.name;
64
- const existingIndex = params.where.findIndex(
65
- (w) => w.id === identifier
66
- );
67
-
68
- if (fv.value !== null && fv.value !== "") {
69
- if (existingIndex !== -1) {
70
- params.where[existingIndex].value = fv.value;
71
- } else {
72
- params.where.push({
73
- id: identifier,
74
- value: fv.value,
75
- operator: fv.operator,
76
- });
77
- }
78
- } else {
79
- if (existingIndex !== -1) {
80
- params.where.splice(existingIndex, 1);
81
- }
82
- }
83
- });
84
- }
85
-
86
- try {
87
- const response = await axios.post(url, params);
88
- const { data, total } = response.data;
89
- return { data, count: total };
90
- } catch (error) {
91
- console.error("Error fetching data:", error);
92
- return { data: [], count: 0 };
93
- }
48
+ const url = ajaxSetting.url;
49
+ const page = Math.floor(skip / limit) + 1;
50
+ const params = { page, sortInfo, take: limit, search: search, where: [] };
51
+
52
+ if (sortInfo) {
53
+ params.sortBy = sortInfo.name;
54
+ params.sort = sortInfo.dir === 1 ? "asc" : "desc";
55
+ }
56
+
57
+ if (ajaxSetting && ajaxSetting.where) {
58
+ params.where = ajaxSetting.where;
59
+ }
60
+
61
+ if (filterValue) {
62
+ filterValue.forEach((fv) => {
63
+ const identifier = fv.key ? fv.key : fv.name;
64
+ const existingIndex = params.where.findIndex(
65
+ (w) => w.id === identifier
66
+ );
67
+
68
+ if (fv.value !== null && fv.value !== "") {
69
+ if (existingIndex !== -1) {
70
+ params.where[existingIndex].value = fv.value;
71
+ } else {
72
+ params.where.push({
73
+ id: identifier,
74
+ value: fv.value,
75
+ operator: fv.operator,
76
+ });
77
+ }
78
+ } else {
79
+ if (existingIndex !== -1) {
80
+ params.where.splice(existingIndex, 1);
81
+ }
82
+ }
83
+ });
84
+ }
85
+
86
+ try {
87
+ const response = await axios.post(url, params);
88
+ const { data, total } = response.data;
89
+ return { data, count: total };
90
+ } catch (error) {
91
+ console.error("Error fetching data:", error);
92
+ return { data: [], count: 0 };
93
+ }
94
94
  };
95
95
 
96
96
  const DataGrid = forwardRef(
97
- (
98
- {
99
- ajaxSetting,
100
- form,
101
- columns,
102
- settings,
103
- setFilterData,
104
- style,
105
- tableFilters,
106
- historySearch,
107
- historyUrl,
108
- },
109
- ref
110
- ) => {
111
- const navigate = useNavigate();
112
-
113
- /** Modal States */
114
- const [formData, setFormData] = useState(form);
115
- const [formType, setFormType] = useState("");
116
- const [formId, setFormId] = useState(0);
117
- const [modalShow, setModalShow] = useState(false);
118
-
119
- /** Modal Functions */
120
- const childDropdownCallback = (data) => {
121
- let _form = { ...formData };
122
-
123
- if (formData.fields.length > 0) {
124
- formData.fields.forEach((a, b) => {
125
- if (a.id === data.id) {
126
- _form.fields[b].options = data.data;
127
- }
128
- });
129
- }
130
-
131
- setFormData(_form);
132
- };
133
-
134
- const modalOpen = (formType, formId) => {
135
- setModalShow(true);
136
- setFormType(formType);
137
- setFormId(formId);
138
- };
139
-
140
- const modalClose = () => {
141
- setModalShow(false);
142
- };
143
-
144
- /** Table States */
145
- const [dataReload, setDataReload] = useState(0);
146
- const [dSearch, setDSearch] = useState("");
147
- const data = useCallback(
148
- (params) => loadData(params, dSearch, ajaxSetting),
149
- [ajaxSetting, dataReload, dSearch]
150
- );
151
- const [filterDataSource, setFilterDataSource] = useState([]);
152
- const [filterValue, setFilterValue] = useState([]);
153
- const [gridColumns, setGridColumns] = useState([]);
154
- const limit = ajaxSetting.take || 10;
155
- const [search, setSearch] = useState("");
156
- const sortInfo =
157
- ajaxSetting.sortBy && ajaxSetting.sort
158
- ? {
159
- name: ajaxSetting.sortBy,
160
- dir: ajaxSetting.sort === "asc" ? 1 : -1,
161
- }
162
- : null;
163
-
164
- /** Table Functions */
165
- useImperativeHandle(ref, () => ({
166
- reload: () => {
167
- handleReload();
168
- },
169
- }));
170
-
171
- const handleChangeSearch = (e) => {
172
- const { value } = e.target;
173
- setSearch(value);
174
- };
175
-
176
- const handleSettingClick = (s, d) => {
177
- switch (s.id) {
178
- case "update":
179
- modalOpen("update", d.id);
180
- break;
181
- case "delete":
182
- confirmAlert({
183
- title: "Delete Item",
184
- message: `Are you sure you want to delete ${
185
- d.name ? d.name : d.label ? d.label : "this item"
186
- }?`,
187
- buttons: [
188
- {
189
- label: "Yes",
190
- onClick: () =>
191
- CustomFetch(
192
- form.url + "/" + d[form.primaryKey],
193
- "DELETE",
194
- {},
195
- (result) => {
196
- if (result.error === "") {
197
- const min = 0;
198
- const max = 999999999;
199
- const randomNumber = Math.floor(
200
- Math.random() *
201
- (max - min + 1) +
202
- min
203
- );
204
-
205
- setDataReload(randomNumber);
206
-
207
- toast.success(
208
- `The selected item has been deleted.`
209
- );
210
- } else {
211
- toast.error(
212
- <div>
213
- {parse(result.error)}
214
- </div>
215
- );
216
- }
217
- }
218
- ),
219
- },
220
- {
221
- label: "No",
222
- onClick: () => close(),
223
- },
224
- ],
225
- });
226
- break;
227
- default:
228
- confirmAlert({
229
- title: s.title,
230
- message: "",
231
- buttons: [
232
- {
233
- label: "Yes",
234
- onClick: () =>
235
- CustomFetch(
236
- s.url + "/" + d[form.primaryKey],
237
- s.method,
238
- {},
239
- function (result) {
240
- if (result.error === "") {
241
- const min = 0;
242
- const max = 999999999;
243
- const randomNumber = Math.floor(
244
- Math.random() *
245
- (max - min + 1) +
246
- min
247
- );
248
-
249
- setDataReload(randomNumber);
250
-
251
- toast.success(String(s.title));
252
- } else {
253
- toast.error(
254
- <div>
255
- {parse(result.error)}
256
- </div>
257
- );
258
- }
259
- }
260
- ),
261
- },
262
- {
263
- label: "No",
264
- onClick: () => close(),
265
- },
266
- ],
267
- });
268
- break;
269
- }
270
- };
271
-
272
- const handleReload = () => {
273
- const min = 0;
274
- const max = 999999999;
275
- const randomNumber = Math.floor(
276
- Math.random() * (max - min + 1) + min
277
- );
278
-
279
- setDataReload(randomNumber);
280
- };
281
-
282
- const onRowClick = useCallback((rowProps, event) => {
283
- const cellElement = event.target.closest(
284
- ".InovuaReactDataGrid__cell"
285
- );
286
- const columnIndex = Array.from(
287
- cellElement.parentNode.children
288
- ).indexOf(cellElement);
289
-
290
- if (columnIndex === rowProps.columns.length - 1) {
291
- } else {
292
- if (
293
- rowProps.columns[columnIndex].link &&
294
- rowProps.columns[columnIndex].link.url
295
- ) {
296
- if (rowProps.columns[columnIndex].link.folder) {
297
- window.location.href =
298
- rowProps.columns[columnIndex].link.url +
299
- rowProps.data[
300
- rowProps.columns[columnIndex].link.key
301
- ] +
302
- "/" +
303
- rowProps.columns[columnIndex].link.folder;
304
- } else {
305
- navigate(
306
- `${rowProps.columns[columnIndex].link.url}${
307
- rowProps.data[
308
- rowProps.columns[columnIndex].link.key
309
- ]
310
- }`
311
- );
312
- }
313
- } else if (rowProps.columns[columnIndex].link.popup) {
314
- window.open(
315
- `${rowProps.columns[columnIndex].link.popup}/${
316
- rowProps.data[
317
- rowProps.columns[columnIndex].link.key
318
- ]
319
- }`,
320
- "",
321
- "width=1440,height=1024,menubar=1,resizable=1,status=1,titlebar=1,toolbar=1"
322
- );
323
- } else {
324
- modalOpen("update", rowProps.data[form.primaryKey]);
325
- }
326
- }
327
- }, []);
328
-
329
- const onRenderRow = useCallback((rowProps) => {
330
- // save the original handlers to be called later
331
- const { onClick } = rowProps;
332
-
333
- rowProps.onClick = (event) => {
334
- onRowClick(rowProps, event);
335
- if (onClick) {
336
- onClick(event);
337
- }
338
- };
339
- }, []);
340
-
341
- const renderCreateButton = () => {
342
- if (form.createDisable && form.createDisable === true) {
343
- return null;
344
- }
345
-
346
- return (
347
- <div className="filterAction--alt">
348
- <button
349
- className="btn"
350
- onClick={() => {
351
- if (form.create) {
352
- modalOpen("create", 0);
353
- } else {
354
- if (form.url) {
355
- window.open(
356
- form.url,
357
- "",
358
- "width=1440,height=1024,menubar=1,resizable=1,status=1,titlebar=1,toolbar=1"
359
- );
360
- }
361
- }
362
- }}
363
- >
364
- <div className="icon-plus"></div> Create
365
- </button>
366
- </div>
367
- );
368
- };
369
-
370
- const renderSearch = () => {
371
- const searchEnable = !(
372
- form.searchDisable && form.searchDisable === true
373
- );
374
-
375
- return searchEnable ? (
376
- <div className="filterInput--alt">
377
- <div className="icon-search">
378
- <Search strokeWidth={2} size={18} />
379
- </div>
380
- <input
381
- type="text"
382
- placeholder="Search"
383
- value={search}
384
- onChange={handleChangeSearch}
385
- />
386
- </div>
387
- ) : null;
388
- };
389
-
390
- const handleCoding = (c, data) => {
391
- const icons = [];
392
- const assetPath =
393
- import.meta.env.REACT_APP_ASSET_PATH !== undefined
394
- ? import.meta.env.REACT_APP_ASSET_PATH
395
- : "";
396
-
397
- const addIcon = (src, icon, alt, tooltipContent, key) => {
398
- if (src && src !== "") {
399
- icons.push(
400
- <img
401
- src={`${assetPath}${src}`}
402
- alt={alt}
403
- data-tooltip-id="system-tooltip"
404
- data-tooltip-content={tooltipContent}
405
- key={key}
406
- />
407
- );
408
- } else {
409
- let IconComponent = null;
410
-
411
- if (icon === "archive") {
412
- IconComponent = ShippingBoxV1;
413
- } else if (icon === "folder") {
414
- IconComponent = Folder;
415
- } else if (icon === "inbox") {
416
- IconComponent = Inbox;
417
- } else if (icon === "network") {
418
- IconComponent = Network;
419
- } else if (icon === "urgent") {
420
- IconComponent = TriangleAlert;
421
- }
422
-
423
- if (IconComponent) {
424
- icons.push(
425
- <IconComponent
426
- data-tooltip-id="system-tooltip"
427
- data-tooltip-content={tooltipContent}
428
- strokeWidth={2}
429
- size={18}
430
- className="tdaction"
431
- key={key}
432
- />
433
- );
434
- }
435
- }
436
- };
437
-
438
- c.config.forEach((config, configKey) => {
439
- const fieldValue = config.id.reduce((acc, id) => {
440
- if (acc === "") {
441
- return data[id];
442
- } else {
443
- return acc[id];
444
- }
445
- }, "");
446
-
447
- const iconData = config.icons.find(
448
- (icon) => icon.value === fieldValue
449
- );
450
-
451
- if (
452
- iconData &&
453
- (iconData.src || iconData.icon) &&
454
- iconData.alt &&
455
- iconData.tooltipContent
456
- ) {
457
- addIcon(
458
- iconData.src ? iconData.src : null,
459
- iconData.icon ? iconData.icon : null,
460
- iconData.alt,
461
- iconData.tooltipContent,
462
- `coding-${config.id[0]}-${configKey}`
463
- );
464
- }
465
- });
466
-
467
- return icons;
468
- };
469
-
470
- /** Table Hooks */
471
- useEffect(() => {
472
- const delayedSetDSearch = debounce(() => {
473
- setDSearch(search);
474
- }, 300); // Adjust the delay (in milliseconds) according to your needs
475
-
476
- delayedSetDSearch();
477
-
478
- return delayedSetDSearch.cancel; // Cleanup the debounce timer on unmount
479
- }, [search, setDSearch]);
480
-
481
- const renderSetting = (s, d) => {
482
- let iconStyle = {};
483
- let allow = true;
484
-
485
- if (s.active) {
486
- if (
487
- d.hasOwnProperty(s.active.id) &&
488
- (!s.active.hasOwnProperty("type") ||
489
- d[s.active.id] === s.active.value)
490
- ) {
491
- iconStyle = {
492
- color: s.active.colour,
493
- };
494
- } else if (
495
- s.active.type === "greater" &&
496
- d[s.active.id] > s.active.value
497
- ) {
498
- iconStyle = {
499
- color: s.active.colour,
500
- };
501
- } else if (
502
- s.active.type === "not null" &&
503
- d[s.active.id] !== null
504
- ) {
505
- iconStyle = {
506
- color: s.active.colour,
507
- };
508
- }
509
- }
510
-
511
- const handleIconClick = (e) => {
512
- e.preventDefault();
513
- e.stopPropagation();
514
- handleSettingClick(s, d);
515
- };
516
-
517
- const tooltipContent =
518
- iconStyle && iconStyle.color ? s.active.title : s.title;
519
-
520
- if (s.hide && s.hide.id && s.hide.value > 0) {
521
- if (
522
- d.hasOwnProperty(s.hide.id) &&
523
- d[s.hide.id] === s.hide.value
524
- ) {
525
- allow = false;
526
- }
527
- }
528
-
529
- if (allow) {
530
- switch (s.id) {
531
- case "archive":
532
- return getIconComponent(ShippingBoxV1);
533
- case "complete":
534
- return getIconComponent(Check);
535
- case "copy":
536
- return getIconComponent(Copy);
537
- case "delete":
538
- return getIconComponent(Backspace);
539
- case "family":
540
- return getIconComponent(Network);
541
- case "primary":
542
- return getIconComponent(Ribbon);
543
- case "ssa":
544
- return getIconComponent(Clock);
545
- case "update":
546
- return getIconComponent(Edit);
547
- default:
548
- return null;
549
- }
550
- } else {
551
- return null;
552
- }
553
-
554
- function getIconComponent(IconComponent) {
555
- return (
556
- <span key={`setting-${s.id}`} onClick={handleIconClick}>
557
- <IconComponent
558
- data-tooltip-id="system-tooltip"
559
- data-tooltip-content={tooltipContent}
560
- strokeWidth={2}
561
- size={18}
562
- className="tdaction"
563
- style={iconStyle}
564
- />
565
- </span>
566
- );
567
- }
568
- };
569
-
570
- const renderColumnContextMenu = useCallback(
571
- (menuProps, { cellProps }) => {
572
- const filteredItems = menuProps.items
573
- .map((item, key) => {
574
- if (key >= 6 && key <= 9) {
575
- return null; // Skip items with keys 6-9 [lock columns setting]
576
- }
577
- return item;
578
- })
579
- .filter(Boolean); // Filter out any null items
580
-
581
- menuProps.items = filteredItems;
582
- },
583
- []
584
- );
585
-
586
- useEffect(() => {
587
- const renderColumn = (column) => {
588
- let childValue;
589
- let date;
590
- let filterEditor = null;
591
- let filterEditorProps = null;
592
- let fileUrl = null;
593
- let icons = [];
594
- let relationName;
595
- let selectedDataSource = null;
596
- let stage;
597
- let style;
598
- let value;
599
-
600
- const commonProps = {
601
- header: column.label,
602
- defaultFlex: 1,
603
- link: column.link ? column.link : {},
604
- minWidth:
605
- column.minWidth && column.minWidth > 0
606
- ? column.minWidth
607
- : undefined,
608
- maxWidth:
609
- column.maxWidth && column.maxWidth > 0
610
- ? column.maxWidth
611
- : undefined,
612
- };
613
-
614
- switch (column.type) {
615
- case "address":
616
- return {
617
- ...commonProps,
618
- name: `${column.type}.${column.id}`,
619
- render: ({ data }) => {
620
- value = "";
621
-
622
- if (data.address1) {
623
- value += `${data.address1} `;
624
- }
625
-
626
- if (data.address2) {
627
- value += `${data.address2} `;
628
- }
629
-
630
- if (data.suburb) {
631
- value += `${data.suburb} `;
632
- }
633
-
634
- if (data.postcode) {
635
- value += `${data.postcode} `;
636
- }
637
-
638
- return <span>{value}</span>;
639
- },
640
- };
641
- case "age":
642
- if (column.filter && column.filter.type) {
643
- switch (column.filter.type) {
644
- case "number":
645
- filterEditor = NumberFilter;
646
- break;
647
- default:
648
- filterEditor = null;
649
- break;
650
- }
651
- } else {
652
- filterEditor = null;
653
- }
654
-
655
- return {
656
- ...commonProps,
657
- type: "number",
658
- filterEditor: filterEditor,
659
- name: `${column.type}.${column.id}`,
660
- render: ({ data }) => {
661
- value = column.id.reduce((acc, id) => {
662
- if (acc === "") {
663
- return data[id];
664
- } else {
665
- return acc[id];
666
- }
667
- }, "");
668
-
669
- if (value !== "" && value !== null) {
670
- date = moment(value);
671
-
672
- return (
673
- <span>
674
- {moment().diff(
675
- date,
676
- column.duration
677
- ? column.duration
678
- : "years"
679
- )}
680
- </span>
681
- );
682
- } else {
683
- return null;
684
- }
685
- },
686
- };
687
- case "arrayCount":
688
- return {
689
- ...commonProps,
690
- name: `${column.type}.${column.id}`,
691
- sortable: false,
692
- render: ({ data }) => {
693
- if (data && data[column.id]) {
694
- return data[column.id].length;
695
- } else {
696
- return null;
697
- }
698
- },
699
- };
700
- case "coding":
701
- return {
702
- ...commonProps,
703
- name: `${column.type}.${column.id}`,
704
- sortable: false,
705
- render: ({ data }) => {
706
- if (data) {
707
- return handleCoding(column, data);
708
- } else {
709
- return null;
710
- }
711
- },
712
- };
713
- case "created_by":
714
- return {
715
- ...commonProps,
716
- name: `${column.type}.${column.id}`,
717
- sortable: false,
718
- render: ({ data }) => {
719
- if (
720
- data.audits[0] &&
721
- data.audits[0].user &&
722
- data.audits[0].user.name
723
- ) {
724
- value = data.audits[0].user.name;
725
-
726
- return <span>{value}</span>;
727
- } else {
728
- return null;
729
- }
730
- },
731
- };
732
- case "currency":
733
- return {
734
- ...commonProps,
735
- name: column.id,
736
- render: ({ data }) => {
737
- if (data) {
738
- data = numeral(data[column.id]).format(
739
- "$0,0.00"
740
- );
741
-
742
- return <span>{data}</span>;
743
- } else {
744
- return null;
745
- }
746
- },
747
- };
748
- case "date":
749
- if (column.filter && column.filter.type) {
750
- filterEditor = DateFilter;
751
- filterEditorProps = (props, { index }) => {
752
- return {
753
- dateFormat: "DD-MM-YYYY",
754
- placeholder: "All Dates",
755
- };
756
- };
757
- } else {
758
- filterEditor = null;
759
- filterEditorProps = null;
760
- }
761
-
762
- return {
763
- ...commonProps,
764
- name: column.id,
765
- filterEditor: filterEditor,
766
- filterEditorProps: filterEditorProps,
767
- render: ({ data }) => {
768
- if (
769
- data &&
770
- data[column.id] &&
771
- moment(data[column.id]).isValid() &&
772
- moment(data[column.id]).format(
773
- "YYYY-MM-DD"
774
- ) !== "1970-01-01"
775
- ) {
776
- const value = moment(
777
- data[column.id]
778
- ).format(column.format || "DD-MM-YYYY");
779
- style = {};
780
-
781
- if (
782
- column.active &&
783
- column.active.id &&
784
- column.active.value === "expired"
785
- ) {
786
- if (
787
- data[column.active.id] &&
788
- moment(
789
- data[column.active.id]
790
- ).isBefore(moment())
791
- ) {
792
- style = {
793
- color: column.active.colour,
794
- };
795
- }
796
- }
797
-
798
- return <span style={style}>{value}</span>;
799
- } else {
800
- return null;
801
- }
802
- },
803
- };
804
- case "datetime":
805
- if (column.filter && column.filter.type) {
806
- filterEditor = DateFilter;
807
- filterEditorProps = (props, { index }) => {
808
- return {
809
- dateFormat: "DD-MM-YYYY",
810
- placeholder: "All Dates",
811
- };
812
- };
813
- } else {
814
- filterEditor = null;
815
- filterEditorProps = null;
816
- }
817
-
818
- return {
819
- ...commonProps,
820
- name: column.id,
821
- filterEditor: filterEditor,
822
- filterEditorProps: filterEditorProps,
823
- render: ({ data }) => {
824
- if (
825
- data &&
826
- data[column.id] &&
827
- moment(data[column.id]).isValid() &&
828
- moment(data[column.id]).format(
829
- "YYYY-MM-DD"
830
- ) !== "1970-01-01"
831
- ) {
832
- data = moment(data[column.id]).format(
833
- column.format
834
- ? column.format
835
- : "DD-MM-YYYY hh:mm A"
836
- );
837
-
838
- return <span>{data}</span>;
839
- } else {
840
- return null;
841
- }
842
- },
843
- };
844
- case "file":
845
- return {
846
- ...commonProps,
847
- name: column.id,
848
- sortable: false,
849
- render: ({ data }) => {
850
- if (data) {
851
- return (
852
- <span>
853
- <CloudDownload
854
- data-tooltip-id="system-tooltip"
855
- data-tooltip-content="Download File"
856
- strokeWidth={2}
857
- size={18}
858
- className="tdaction"
859
- />
860
- </span>
861
- );
862
- } else {
863
- return null;
864
- }
865
- },
866
- };
867
- case "icons":
868
- return {
869
- ...commonProps,
870
- name: column.id,
871
- sortable: false,
872
- render: ({ data }) => {
873
- icons = [];
874
-
875
- if (column.icons && column.icons.length > 0) {
876
- column.icons.forEach((icon) => {
877
- icons.push(
878
- <img
879
- src={icon.url}
880
- alt={icon.name}
881
- data-tooltip-id="system-tooltip"
882
- data-tooltip-content={icon.name}
883
- />
884
- );
885
- });
886
- }
887
-
888
- if (data) {
889
- return <span>{icons}</span>;
890
- } else {
891
- return null;
892
- }
893
- },
894
- };
895
- case "image":
896
- return {
897
- ...commonProps,
898
- name: column.id,
899
- render: ({ data }) => {
900
- if (data && data[column.id]) {
901
- const fileUrl =
902
- data[column.id][0]?.file_url ||
903
- data[column.id]?.file_url;
904
-
905
- if (fileUrl) {
906
- return (
907
- <span>
908
- {parse(
909
- `<img width="150" src="${fileUrl}" />`
910
- )}
911
- </span>
912
- );
913
- }
914
- }
915
-
916
- return null;
917
- },
918
- };
919
- case "json":
920
- return {
921
- ...commonProps,
922
- name: column.id,
923
- render: ({ data }) => {
924
- if (
925
- data &&
926
- data[column.id] &&
927
- data[column.id][column.jsonData]
928
- ) {
929
- data = data[column.id][column.jsonData];
930
-
931
- return <span>{data}</span>;
932
- } else {
933
- return null;
934
- }
935
- },
936
- };
937
- case "number":
938
- return {
939
- ...commonProps,
940
- name: column.id,
941
- type: "number",
942
- };
943
- case "option":
944
- selectedDataSource = filterDataSource.reduce(
945
- (acc, fds) => {
946
- if (fds.id === column.id) {
947
- return fds.dataset;
948
- }
949
- return acc;
950
- },
951
- null
952
- );
953
-
954
- if (
955
- column.filter &&
956
- column.filter.type &&
957
- column.filter.type === "select"
958
- ) {
959
- filterEditor = SelectFilter;
960
- filterEditorProps = {
961
- placeholder: column.filter.placeholder
962
- ? column.filter.placeholder
963
- : "All Options",
964
- dataSource: selectedDataSource,
965
- };
966
- } else {
967
- filterEditor = null;
968
- filterEditorProps = null;
969
- }
970
-
971
- return {
972
- ...commonProps,
973
- name: column.id,
974
- filterEditor: filterEditor,
975
- filterEditorProps: filterEditorProps,
976
- render: ({ data }) => {
977
- if (
978
- data &&
979
- column.options &&
980
- column.options[data[column.id]]
981
- ) {
982
- data = column.options[data[column.id]];
983
-
984
- return <span>{data}</span>;
985
- } else {
986
- return null;
987
- }
988
- },
989
- };
990
- case "placeholder":
991
- return {
992
- ...commonProps,
993
- name: column.id,
994
- sortable: false,
995
- render: ({ data }) => {
996
- if (data) {
997
- return <span>{column.placeholder}</span>;
998
- } else {
999
- return null;
1000
- }
1001
- },
1002
- };
1003
- case "relation":
1004
- relationName = column.id.reduce(
1005
- (acc, id) => acc + `${id}.`,
1006
- ""
1007
- );
1008
- relationName += column.nameFrom;
1009
-
1010
- selectedDataSource = filterDataSource.reduce(
1011
- (acc, fds) => {
1012
- if (fds.id === relationName) {
1013
- return fds.dataset;
1014
- }
1015
- return acc;
1016
- },
1017
- null
1018
- );
1019
-
1020
- if (
1021
- column.filter &&
1022
- column.filter.type &&
1023
- column.filter.type === "select"
1024
- ) {
1025
- filterEditor = SelectFilter;
1026
- filterEditorProps = {
1027
- placeholder: column.filter.placeholder
1028
- ? column.filter.placeholder
1029
- : "All Options",
1030
- dataSource: selectedDataSource,
1031
- };
1032
- } else {
1033
- filterEditor = null;
1034
- filterEditorProps = null;
1035
- }
1036
-
1037
- return {
1038
- ...commonProps,
1039
- name: relationName,
1040
- sortable: false,
1041
- filterEditor: filterEditor,
1042
- filterEditorProps: filterEditorProps,
1043
- render: ({ data }) => {
1044
- value = column.id.reduce((acc, id) => {
1045
- if (acc === "") {
1046
- return data[id];
1047
- } else {
1048
- if (acc && acc[id]) {
1049
- return acc[id];
1050
- } else {
1051
- return "";
1052
- }
1053
- }
1054
- }, "");
1055
-
1056
- if (value) {
1057
- if (Array.isArray(column.nameFrom)) {
1058
- value = column.nameFrom
1059
- .map((nf) => value[nf])
1060
- .join(" ");
1061
- } else {
1062
- value = value[column.nameFrom];
1063
- }
1064
-
1065
- if (
1066
- column.placeholder &&
1067
- column.placeholder !== ""
1068
- ) {
1069
- return (
1070
- <span>{column.placeholder}</span>
1071
- );
1072
- } else {
1073
- return <span>{value}</span>;
1074
- }
1075
- } else {
1076
- return null;
1077
- }
1078
- },
1079
- };
1080
- case "relationArray":
1081
- relationName =
1082
- column.id.reduce((acc, id) => acc + `${id}.`, "") +
1083
- column.nameFrom;
1084
-
1085
- selectedDataSource = filterDataSource.reduce(
1086
- (acc, fds) => {
1087
- if (fds.id === relationName) {
1088
- return fds.dataset;
1089
- }
1090
- return acc;
1091
- },
1092
- null
1093
- );
1094
-
1095
- if (
1096
- column.filter &&
1097
- column.filter.type &&
1098
- column.filter.type === "select"
1099
- ) {
1100
- filterEditor = SelectFilter;
1101
- filterEditorProps = {
1102
- placeholder: column.filter.placeholder
1103
- ? column.filter.placeholder
1104
- : "All Options",
1105
- dataSource: selectedDataSource,
1106
- };
1107
- } else {
1108
- filterEditor = null;
1109
- filterEditorProps = null;
1110
- }
1111
-
1112
- return {
1113
- ...commonProps,
1114
- name: relationName,
1115
- sortable: false,
1116
- filterEditor: filterEditor,
1117
- filterEditorProps: filterEditorProps,
1118
- render: ({ data }) => {
1119
- const relationValue = column.id.reduce(
1120
- (acc, id) =>
1121
- acc === ""
1122
- ? data[id]
1123
- : acc && acc[id]
1124
- ? acc[id]
1125
- : "",
1126
- ""
1127
- );
1128
-
1129
- if (
1130
- relationValue &&
1131
- Array.isArray(relationValue)
1132
- ) {
1133
- const nameProperties = Array.isArray(
1134
- column.nameFrom
1135
- )
1136
- ? column.nameFrom
1137
- : [column.nameFrom];
1138
-
1139
- const values = relationValue
1140
- .map((rv) => {
1141
- if (
1142
- column.childId &&
1143
- Array.isArray(column.childId)
1144
- ) {
1145
- childValue = rv;
1146
-
1147
- column.childId.forEach((a) => {
1148
- if (childValue[a]) {
1149
- childValue =
1150
- childValue[a];
1151
- }
1152
- });
1153
-
1154
- if (childValue) {
1155
- const propertyValues =
1156
- nameProperties
1157
- .map(
1158
- (prop) =>
1159
- childValue[
1160
- prop
1161
- ]
1162
- )
1163
- .filter(
1164
- (value) =>
1165
- value !==
1166
- undefined &&
1167
- value !==
1168
- null
1169
- );
1170
- return propertyValues.join(
1171
- " "
1172
- );
1173
- }
1174
- } else {
1175
- const propertyValues =
1176
- nameProperties
1177
- .map((prop) => rv[prop])
1178
- .filter(
1179
- (value) =>
1180
- value !==
1181
- undefined &&
1182
- value !== null
1183
- );
1184
- return propertyValues.join(" ");
1185
- }
1186
- })
1187
- .filter((value) => value !== "");
1188
-
1189
- if (values.length > 0) {
1190
- let result = "";
1191
-
1192
- if (
1193
- column.relation?.id &&
1194
- column.relation?.key
1195
- ) {
1196
- const relationValue =
1197
- column.relation.key.reduce(
1198
- (acc, id) =>
1199
- acc === ""
1200
- ? data[id]
1201
- : acc && acc[id]
1202
- ? acc[id]
1203
- : "",
1204
- ""
1205
- );
1206
-
1207
- result = `${
1208
- relationValue[
1209
- column.relation.id
1210
- ]
1211
- }<br />`;
1212
- }
1213
-
1214
- if (
1215
- column.where &&
1216
- column.where.id &&
1217
- column.where.value
1218
- ) {
1219
- if (
1220
- data[column.where.id] ===
1221
- column.where.value
1222
- ) {
1223
- result += values.join("<br />");
1224
- }
1225
- } else {
1226
- result += values.join("<br />");
1227
- }
1228
-
1229
- return <span>{parse(result)}</span>;
1230
- }
1231
- }
1232
-
1233
- return null;
1234
- },
1235
- };
1236
- case "richtext":
1237
- return {
1238
- ...commonProps,
1239
- name: column.id,
1240
- render: ({ data }) => {
1241
- if (data && data[column.id]) {
1242
- return (
1243
- <span>{parse(data[column.id])}</span>
1244
- );
1245
- } else {
1246
- return null;
1247
- }
1248
- },
1249
- };
1250
- case "stage":
1251
- return {
1252
- ...commonProps,
1253
- name: column.id,
1254
- sortable: false,
1255
- render: ({ data }) => {
1256
- stage = "";
1257
-
1258
- column.keyIds.forEach((a, b) => {
1259
- if (data[a] === 1) {
1260
- stage = column.valueIds[b];
1261
- }
1262
- });
1263
-
1264
- return <span>{stage}</span>;
1265
- },
1266
- };
1267
- case "time":
1268
- return {
1269
- ...commonProps,
1270
- name: column.id,
1271
- render: ({ data }) => {
1272
- if (
1273
- data &&
1274
- data[column.id] &&
1275
- moment(data[column.id]).isValid() &&
1276
- moment(data[column.id]).format(
1277
- "YYYY-MM-DD"
1278
- ) !== "1970-01-01"
1279
- ) {
1280
- data = moment(data[column.id]).format(
1281
- column.format
1282
- ? column.format
1283
- : "hh:mm A"
1284
- );
1285
-
1286
- return <span>{data}</span>;
1287
- } else {
1288
- return null;
1289
- }
1290
- },
1291
- };
1292
- case "url":
1293
- return {
1294
- ...commonProps,
1295
- name: column.id,
1296
- render: ({ data }) => {
1297
- if (data && data[column.id]) {
1298
- return (
1299
- <span>
1300
- <a
1301
- href={data[column.id]}
1302
- target="_blank"
1303
- >
1304
- Link
1305
- </a>
1306
- </span>
1307
- );
1308
- } else {
1309
- return null;
1310
- }
1311
- },
1312
- };
1313
- default:
1314
- selectedDataSource = filterDataSource.reduce(
1315
- (acc, fds) => {
1316
- if (fds.id === column.id) {
1317
- return fds.dataset;
1318
- }
1319
- return acc;
1320
- },
1321
- null
1322
- );
1323
-
1324
- if (column.filter && column.filter.type) {
1325
- switch (column.filter.type) {
1326
- case "select":
1327
- filterEditor = SelectFilter;
1328
- filterEditorProps = {
1329
- placeholder: column.filter.placeholder
1330
- ? column.filter.placeholder
1331
- : "All Options",
1332
- dataSource: selectedDataSource,
1333
- };
1334
- break;
1335
- default:
1336
- filterEditor = null;
1337
- filterEditorProps = null;
1338
- break;
1339
- }
1340
- } else {
1341
- filterEditor = null;
1342
- filterEditorProps = null;
1343
- }
1344
-
1345
- return {
1346
- ...commonProps,
1347
- name: column.id,
1348
- filterEditor: filterEditor,
1349
- filterEditorProps: filterEditorProps,
1350
- };
1351
- }
1352
- };
1353
-
1354
- const newColumns = columns.map(renderColumn);
1355
-
1356
- if (settings.length > 0) {
1357
- newColumns.push({
1358
- name: "setting",
1359
- header: "Action",
1360
- defaultWidth: 100,
1361
- textAlign: "center",
1362
- render: ({ data }) => {
1363
- return (
1364
- <div className="tdactions">
1365
- {settings.map((setting) =>
1366
- renderSetting(setting, data)
1367
- )}
1368
- </div>
1369
- );
1370
- },
1371
- });
1372
- }
1373
-
1374
- setGridColumns(newColumns);
1375
-
1376
- const newFilterValue = columns
1377
- .filter((column) => column.filter && column.filter.type) // Only consider columns with a filter type
1378
- .map((column) => {
1379
- // Map these filtered columns to a new object array
1380
- const filterName = Array.isArray(column.id)
1381
- ? column.id.reduce((acc, id) => acc + `${id}.`, "") +
1382
- column.nameFrom
1383
- : column.id;
1384
-
1385
- return {
1386
- key: column.filter.key ? column.filter.key : null,
1387
- name: filterName,
1388
- operator: column.filter.operator,
1389
- type: column.filter.type,
1390
- value: column.filter.value ? column.filter.value : "",
1391
- };
1392
- });
1393
-
1394
- setFilterValue(newFilterValue);
1395
- }, [columns, filterDataSource]);
1396
-
1397
- useEffect(() => {
1398
- if (setFilterData) {
1399
- setFilterData(filterValue);
1400
- }
1401
- }, [filterValue]);
1402
-
1403
- useEffect(() => {
1404
- columns.forEach((column) => {
1405
- if (column.filter && column.filter.url) {
1406
- CustomFetch(
1407
- column.filter.url,
1408
- "POST",
1409
- column.filter.where
1410
- ? { where: column.filter.where }
1411
- : {},
1412
- (res) => {
1413
- const filterName = Array.isArray(column.id)
1414
- ? column.id.reduce(
1415
- (acc, id) => acc + `${id}.`,
1416
- ""
1417
- ) + column.nameFrom
1418
- : column.id;
1419
-
1420
- const filterIndex = filterDataSource.findIndex(
1421
- (filter) => filter.id === filterName
1422
- );
1423
-
1424
- if (filterIndex >= 0) {
1425
- const updatedFilter = {
1426
- ...filterDataSource[filterIndex],
1427
- dataset: res.data,
1428
- };
1429
- const updatedDataSource = [
1430
- ...filterDataSource.slice(0, filterIndex),
1431
- updatedFilter,
1432
- ...filterDataSource.slice(filterIndex + 1),
1433
- ];
1434
- setFilterDataSource(updatedDataSource);
1435
- } else {
1436
- const newFilter = {
1437
- id: filterName,
1438
- dataset: res.data,
1439
- };
1440
- setFilterDataSource((prevState) => [
1441
- ...prevState,
1442
- newFilter,
1443
- ]);
1444
- }
1445
- }
1446
- );
1447
- } else if (column.filter && column.filter.options) {
1448
- const filterName = Array.isArray(column.id)
1449
- ? column.id.reduce((acc, id) => acc + `${id}.`, "") +
1450
- column.nameFrom
1451
- : column.id;
1452
-
1453
- const newFilter = {
1454
- id: filterName,
1455
- dataset: column.filter.options,
1456
- };
1457
-
1458
- setFilterDataSource((prevState) => [
1459
- ...prevState,
1460
- newFilter,
1461
- ]);
1462
- }
1463
- });
1464
- }, []);
1465
-
1466
- // Data Grid Styling
1467
- const [windowHeight, setWindowHeight] = useState(window.innerHeight);
1468
- const gridStyle = {
1469
- minHeight: windowHeight * 0.7 > 550 ? windowHeight * 0.7 : 550,
1470
- boxShadow: "none",
1471
- };
1472
- const headerProps = {
1473
- style: style ? style : {},
1474
- };
1475
-
1476
- useEffect(() => {
1477
- const handleResize = () => {
1478
- setWindowHeight(window.innerHeight);
1479
- };
1480
-
1481
- window.addEventListener("resize", handleResize);
1482
-
1483
- // Clean up the event listener
1484
- return () => {
1485
- window.removeEventListener("resize", handleResize);
1486
- };
1487
- }, []);
1488
-
1489
- return (
1490
- <>
1491
- {renderSearch()}
1492
- {renderCreateButton()}
1493
- <ReactDataGrid
1494
- columns={gridColumns}
1495
- dataSource={data}
1496
- filterValue={filterValue}
1497
- defaultLimit={limit}
1498
- defaultSortInfo={sortInfo}
1499
- enableColumnAutosize={false}
1500
- enableColumnFilterContextMenu={false}
1501
- enableSelection={false}
1502
- headerProps={headerProps}
1503
- idProperty={`visns-datagrid-${ajaxSetting.url.replace(
1504
- /\//g,
1505
- "-"
1506
- )}`}
1507
- minRowHeight={40}
1508
- onFilterValueChange={setFilterValue}
1509
- onRenderRow={onRenderRow}
1510
- pagination
1511
- renderColumnContextMenu={renderColumnContextMenu}
1512
- rowHeight={null}
1513
- showZebraRows={true}
1514
- style={gridStyle}
1515
- />
1516
- <Popup
1517
- open={modalShow}
1518
- onClose={modalClose}
1519
- closeOnDocumentClick={false}
1520
- >
1521
- <Form
1522
- closeModal={modalClose}
1523
- fetchTable={handleReload}
1524
- formSettings={formData}
1525
- formType={formType}
1526
- updateForm={setFormData}
1527
- columnId={formId}
1528
- childDropdownCallback={childDropdownCallback}
1529
- style={style}
1530
- />
1531
- </Popup>
1532
- </>
1533
- );
1534
- }
97
+ (
98
+ {
99
+ ajaxSetting,
100
+ form,
101
+ columns,
102
+ settings,
103
+ setFilterData,
104
+ style,
105
+ tableFilters,
106
+ historySearch,
107
+ historyUrl,
108
+ },
109
+ ref
110
+ ) => {
111
+ const navigate = useNavigate();
112
+
113
+ /** Modal States */
114
+ const [formData, setFormData] = useState(form);
115
+ const [formType, setFormType] = useState("");
116
+ const [formId, setFormId] = useState(0);
117
+ const [modalShow, setModalShow] = useState(false);
118
+
119
+ /** Modal Functions */
120
+ const childDropdownCallback = (data) => {
121
+ let _form = { ...formData };
122
+
123
+ if (formData.fields.length > 0) {
124
+ formData.fields.forEach((a, b) => {
125
+ if (a.id === data.id) {
126
+ _form.fields[b].options = data.data;
127
+ }
128
+ });
129
+ }
130
+
131
+ setFormData(_form);
132
+ };
133
+
134
+ const modalOpen = (formType, formId) => {
135
+ setModalShow(true);
136
+ setFormType(formType);
137
+ setFormId(formId);
138
+ };
139
+
140
+ const modalClose = () => {
141
+ setModalShow(false);
142
+ };
143
+
144
+ /** Table States */
145
+ const [dataReload, setDataReload] = useState(0);
146
+ const [dSearch, setDSearch] = useState("");
147
+ const data = useCallback(
148
+ (params) => loadData(params, dSearch, ajaxSetting),
149
+ [ajaxSetting, dataReload, dSearch]
150
+ );
151
+ const [filterDataSource, setFilterDataSource] = useState([]);
152
+ const [filterValue, setFilterValue] = useState([]);
153
+ const [gridColumns, setGridColumns] = useState([]);
154
+ const limit = ajaxSetting.take || 10;
155
+ const [search, setSearch] = useState("");
156
+ const sortInfo =
157
+ ajaxSetting.sortBy && ajaxSetting.sort
158
+ ? {
159
+ name: ajaxSetting.sortBy,
160
+ dir: ajaxSetting.sort === "asc" ? 1 : -1,
161
+ }
162
+ : null;
163
+
164
+ /** Table Functions */
165
+ useImperativeHandle(ref, () => ({
166
+ reload: () => {
167
+ handleReload();
168
+ },
169
+ }));
170
+
171
+ const handleChangeSearch = (e) => {
172
+ const { value } = e.target;
173
+ setSearch(value);
174
+ };
175
+
176
+ const handleSettingClick = (s, d) => {
177
+ switch (s.id) {
178
+ case "update":
179
+ modalOpen("update", d.id);
180
+ break;
181
+ case "delete":
182
+ confirmAlert({
183
+ title: "Delete Item",
184
+ message: `Are you sure you want to delete ${
185
+ d.name ? d.name : d.label ? d.label : "this item"
186
+ }?`,
187
+ buttons: [
188
+ {
189
+ label: "Yes",
190
+ onClick: () =>
191
+ CustomFetch(
192
+ form.url + "/" + d[form.primaryKey],
193
+ "DELETE",
194
+ {},
195
+ (result) => {
196
+ if (result.error === "") {
197
+ const min = 0;
198
+ const max = 999999999;
199
+ const randomNumber = Math.floor(
200
+ Math.random() *
201
+ (max - min + 1) +
202
+ min
203
+ );
204
+
205
+ setDataReload(randomNumber);
206
+
207
+ toast.success(
208
+ `The selected item has been deleted.`
209
+ );
210
+ } else {
211
+ toast.error(
212
+ <div>
213
+ {parse(result.error)}
214
+ </div>
215
+ );
216
+ }
217
+ }
218
+ ),
219
+ },
220
+ {
221
+ label: "No",
222
+ onClick: () => close(),
223
+ },
224
+ ],
225
+ });
226
+ break;
227
+ default:
228
+ confirmAlert({
229
+ title: s.title,
230
+ message: "",
231
+ buttons: [
232
+ {
233
+ label: "Yes",
234
+ onClick: () =>
235
+ CustomFetch(
236
+ s.url + "/" + d[form.primaryKey],
237
+ s.method,
238
+ {},
239
+ function (result) {
240
+ if (result.error === "") {
241
+ const min = 0;
242
+ const max = 999999999;
243
+ const randomNumber = Math.floor(
244
+ Math.random() *
245
+ (max - min + 1) +
246
+ min
247
+ );
248
+
249
+ setDataReload(randomNumber);
250
+
251
+ toast.success(String(s.title));
252
+ } else {
253
+ toast.error(
254
+ <div>
255
+ {parse(result.error)}
256
+ </div>
257
+ );
258
+ }
259
+ }
260
+ ),
261
+ },
262
+ {
263
+ label: "No",
264
+ onClick: () => close(),
265
+ },
266
+ ],
267
+ });
268
+ break;
269
+ }
270
+ };
271
+
272
+ const handleReload = () => {
273
+ const min = 0;
274
+ const max = 999999999;
275
+ const randomNumber = Math.floor(
276
+ Math.random() * (max - min + 1) + min
277
+ );
278
+
279
+ setDataReload(randomNumber);
280
+ };
281
+
282
+ const onRowClick = useCallback((rowProps, event) => {
283
+ const cellElement = event.target.closest(
284
+ ".InovuaReactDataGrid__cell"
285
+ );
286
+ const columnIndex = Array.from(
287
+ cellElement.parentNode.children
288
+ ).indexOf(cellElement);
289
+
290
+ if (columnIndex === rowProps.columns.length - 1) {
291
+ } else {
292
+ if (
293
+ rowProps.columns[columnIndex].link &&
294
+ rowProps.columns[columnIndex].link.url
295
+ ) {
296
+ if (rowProps.columns[columnIndex].link.folder) {
297
+ window.location.href =
298
+ rowProps.columns[columnIndex].link.url +
299
+ rowProps.data[
300
+ rowProps.columns[columnIndex].link.key
301
+ ] +
302
+ "/" +
303
+ rowProps.columns[columnIndex].link.folder;
304
+ } else {
305
+ navigate(
306
+ `${rowProps.columns[columnIndex].link.url}${
307
+ rowProps.data[
308
+ rowProps.columns[columnIndex].link.key
309
+ ]
310
+ }`
311
+ );
312
+ }
313
+ } else if (rowProps.columns[columnIndex].link.popup) {
314
+ window.open(
315
+ `${rowProps.columns[columnIndex].link.popup}/${
316
+ rowProps.data[
317
+ rowProps.columns[columnIndex].link.key
318
+ ]
319
+ }`,
320
+ "",
321
+ "width=1440,height=1024,menubar=1,resizable=1,status=1,titlebar=1,toolbar=1"
322
+ );
323
+ } else {
324
+ modalOpen("update", rowProps.data[form.primaryKey]);
325
+ }
326
+ }
327
+ }, []);
328
+
329
+ const onRenderRow = useCallback((rowProps) => {
330
+ // save the original handlers to be called later
331
+ const { onClick } = rowProps;
332
+
333
+ rowProps.onClick = (event) => {
334
+ onRowClick(rowProps, event);
335
+ if (onClick) {
336
+ onClick(event);
337
+ }
338
+ };
339
+ }, []);
340
+
341
+ const renderCreateButton = () => {
342
+ if (form.createDisable && form.createDisable === true) {
343
+ return null;
344
+ }
345
+
346
+ return (
347
+ <div className="filterAction--alt">
348
+ <button
349
+ className="btn"
350
+ onClick={() => {
351
+ if (form.create) {
352
+ modalOpen("create", 0);
353
+ } else {
354
+ if (form.url) {
355
+ window.open(
356
+ form.url,
357
+ "",
358
+ "width=1440,height=1024,menubar=1,resizable=1,status=1,titlebar=1,toolbar=1"
359
+ );
360
+ }
361
+ }
362
+ }}
363
+ >
364
+ <div className="icon-plus"></div> Create
365
+ </button>
366
+ </div>
367
+ );
368
+ };
369
+
370
+ const renderSearch = () => {
371
+ const searchEnable = !(
372
+ form.searchDisable && form.searchDisable === true
373
+ );
374
+
375
+ return searchEnable ? (
376
+ <div className="filterInput--alt">
377
+ <div className="icon-search">
378
+ <Search strokeWidth={2} size={18} />
379
+ </div>
380
+ <input
381
+ type="text"
382
+ placeholder="Search"
383
+ value={search}
384
+ onChange={handleChangeSearch}
385
+ />
386
+ </div>
387
+ ) : null;
388
+ };
389
+
390
+ const handleCoding = (c, data) => {
391
+ const icons = [];
392
+ const assetPath =
393
+ import.meta.env.REACT_APP_ASSET_PATH !== undefined
394
+ ? import.meta.env.REACT_APP_ASSET_PATH
395
+ : "";
396
+
397
+ const addIcon = (src, icon, alt, tooltipContent, key) => {
398
+ if (src && src !== "") {
399
+ icons.push(
400
+ <img
401
+ src={`${assetPath}${src}`}
402
+ alt={alt}
403
+ data-tooltip-id="system-tooltip"
404
+ data-tooltip-content={tooltipContent}
405
+ key={key}
406
+ />
407
+ );
408
+ } else {
409
+ let IconComponent = null;
410
+
411
+ if (icon === "archive") {
412
+ IconComponent = ShippingBoxV1;
413
+ } else if (icon === "folder") {
414
+ IconComponent = Folder;
415
+ } else if (icon === "inbox") {
416
+ IconComponent = Inbox;
417
+ } else if (icon === "network") {
418
+ IconComponent = Network;
419
+ } else if (icon === "urgent") {
420
+ IconComponent = TriangleAlert;
421
+ }
422
+
423
+ if (IconComponent) {
424
+ icons.push(
425
+ <IconComponent
426
+ data-tooltip-id="system-tooltip"
427
+ data-tooltip-content={tooltipContent}
428
+ strokeWidth={2}
429
+ size={18}
430
+ className="tdaction"
431
+ key={key}
432
+ />
433
+ );
434
+ }
435
+ }
436
+ };
437
+
438
+ c.config.forEach((config, configKey) => {
439
+ const fieldValue = config.id.reduce((acc, id) => {
440
+ if (acc === "") {
441
+ return data[id];
442
+ } else {
443
+ return acc[id];
444
+ }
445
+ }, "");
446
+
447
+ const iconData = config.icons.find(
448
+ (icon) => icon.value === fieldValue
449
+ );
450
+
451
+ if (
452
+ iconData &&
453
+ (iconData.src || iconData.icon) &&
454
+ iconData.alt &&
455
+ iconData.tooltipContent
456
+ ) {
457
+ addIcon(
458
+ iconData.src ? iconData.src : null,
459
+ iconData.icon ? iconData.icon : null,
460
+ iconData.alt,
461
+ iconData.tooltipContent,
462
+ `coding-${config.id[0]}-${configKey}`
463
+ );
464
+ }
465
+ });
466
+
467
+ return icons;
468
+ };
469
+
470
+ /** Table Hooks */
471
+ useEffect(() => {
472
+ const delayedSetDSearch = debounce(() => {
473
+ setDSearch(search);
474
+ }, 300); // Adjust the delay (in milliseconds) according to your needs
475
+
476
+ delayedSetDSearch();
477
+
478
+ return delayedSetDSearch.cancel; // Cleanup the debounce timer on unmount
479
+ }, [search, setDSearch]);
480
+
481
+ const renderSetting = (s, d) => {
482
+ let iconStyle = {};
483
+ let allow = true;
484
+
485
+ if (s.active) {
486
+ if (
487
+ d.hasOwnProperty(s.active.id) &&
488
+ d[s.active.id] === s.active.value
489
+ ) {
490
+ iconStyle = {
491
+ color: s.active.colour,
492
+ };
493
+ } else if (
494
+ s.active.type === "greater" &&
495
+ d[s.active.id] > s.active.value
496
+ ) {
497
+ iconStyle = {
498
+ color: s.active.colour,
499
+ };
500
+ } else if (
501
+ s.active.type === "not null" &&
502
+ d[s.active.id] !== null
503
+ ) {
504
+ iconStyle = {
505
+ color: s.active.colour,
506
+ };
507
+ }
508
+ }
509
+
510
+ const handleIconClick = (e) => {
511
+ e.preventDefault();
512
+ e.stopPropagation();
513
+ handleSettingClick(s, d);
514
+ };
515
+
516
+ const tooltipContent =
517
+ iconStyle && iconStyle.color ? s.active.title : s.title;
518
+
519
+ if (s.hide && s.hide.id && s.hide.value > 0) {
520
+ if (
521
+ d.hasOwnProperty(s.hide.id) &&
522
+ d[s.hide.id] === s.hide.value
523
+ ) {
524
+ allow = false;
525
+ }
526
+ }
527
+
528
+ if (allow) {
529
+ switch (s.id) {
530
+ case "archive":
531
+ return getIconComponent(ShippingBoxV1);
532
+ case "complete":
533
+ return getIconComponent(Check);
534
+ case "copy":
535
+ return getIconComponent(Copy);
536
+ case "delete":
537
+ return getIconComponent(Backspace);
538
+ case "family":
539
+ return getIconComponent(Network);
540
+ case "primary":
541
+ return getIconComponent(Ribbon);
542
+ case "ssa":
543
+ return getIconComponent(Clock);
544
+ case "update":
545
+ return getIconComponent(Edit);
546
+ default:
547
+ return null;
548
+ }
549
+ } else {
550
+ return null;
551
+ }
552
+
553
+ function getIconComponent(IconComponent) {
554
+ return (
555
+ <span key={`setting-${s.id}`} onClick={handleIconClick}>
556
+ <IconComponent
557
+ data-tooltip-id="system-tooltip"
558
+ data-tooltip-content={tooltipContent}
559
+ strokeWidth={2}
560
+ size={18}
561
+ className="tdaction"
562
+ style={iconStyle}
563
+ />
564
+ </span>
565
+ );
566
+ }
567
+ };
568
+
569
+ const renderColumnContextMenu = useCallback(
570
+ (menuProps, { cellProps }) => {
571
+ const filteredItems = menuProps.items
572
+ .map((item, key) => {
573
+ if (key >= 6 && key <= 9) {
574
+ return null; // Skip items with keys 6-9 [lock columns setting]
575
+ }
576
+ return item;
577
+ })
578
+ .filter(Boolean); // Filter out any null items
579
+
580
+ menuProps.items = filteredItems;
581
+ },
582
+ []
583
+ );
584
+
585
+ useEffect(() => {
586
+ const renderColumn = (column) => {
587
+ let childValue;
588
+ let date;
589
+ let filterEditor = null;
590
+ let filterEditorProps = null;
591
+ let fileUrl = null;
592
+ let icons = [];
593
+ let relationName;
594
+ let selectedDataSource = null;
595
+ let stage;
596
+ let style;
597
+ let value;
598
+
599
+ const commonProps = {
600
+ header: column.label,
601
+ defaultFlex: 1,
602
+ link: column.link ? column.link : {},
603
+ minWidth:
604
+ column.minWidth && column.minWidth > 0
605
+ ? column.minWidth
606
+ : undefined,
607
+ maxWidth:
608
+ column.maxWidth && column.maxWidth > 0
609
+ ? column.maxWidth
610
+ : undefined,
611
+ };
612
+
613
+ switch (column.type) {
614
+ case "address":
615
+ return {
616
+ ...commonProps,
617
+ name: `${column.type}.${column.id}`,
618
+ render: ({ data }) => {
619
+ value = "";
620
+
621
+ if (data.address1) {
622
+ value += `${data.address1} `;
623
+ }
624
+
625
+ if (data.address2) {
626
+ value += `${data.address2} `;
627
+ }
628
+
629
+ if (data.suburb) {
630
+ value += `${data.suburb} `;
631
+ }
632
+
633
+ if (data.postcode) {
634
+ value += `${data.postcode} `;
635
+ }
636
+
637
+ return <span>{value}</span>;
638
+ },
639
+ };
640
+ case "age":
641
+ if (column.filter && column.filter.type) {
642
+ switch (column.filter.type) {
643
+ case "number":
644
+ filterEditor = NumberFilter;
645
+ break;
646
+ default:
647
+ filterEditor = null;
648
+ break;
649
+ }
650
+ } else {
651
+ filterEditor = null;
652
+ }
653
+
654
+ return {
655
+ ...commonProps,
656
+ type: "number",
657
+ filterEditor: filterEditor,
658
+ name: `${column.type}.${column.id}`,
659
+ render: ({ data }) => {
660
+ value = column.id.reduce((acc, id) => {
661
+ if (acc === "") {
662
+ return data[id];
663
+ } else {
664
+ return acc[id];
665
+ }
666
+ }, "");
667
+
668
+ if (value !== "" && value !== null) {
669
+ date = moment(value);
670
+
671
+ return (
672
+ <span>
673
+ {moment().diff(
674
+ date,
675
+ column.duration
676
+ ? column.duration
677
+ : "years"
678
+ )}
679
+ </span>
680
+ );
681
+ } else {
682
+ return null;
683
+ }
684
+ },
685
+ };
686
+ case "arrayCount":
687
+ return {
688
+ ...commonProps,
689
+ name: `${column.type}.${column.id}`,
690
+ sortable: false,
691
+ render: ({ data }) => {
692
+ if (data && data[column.id]) {
693
+ return data[column.id].length;
694
+ } else {
695
+ return null;
696
+ }
697
+ },
698
+ };
699
+ case "coding":
700
+ return {
701
+ ...commonProps,
702
+ name: `${column.type}.${column.id}`,
703
+ sortable: false,
704
+ render: ({ data }) => {
705
+ if (data) {
706
+ return handleCoding(column, data);
707
+ } else {
708
+ return null;
709
+ }
710
+ },
711
+ };
712
+ case "created_by":
713
+ return {
714
+ ...commonProps,
715
+ name: `${column.type}.${column.id}`,
716
+ sortable: false,
717
+ render: ({ data }) => {
718
+ if (
719
+ data.audits[0] &&
720
+ data.audits[0].user &&
721
+ data.audits[0].user.name
722
+ ) {
723
+ value = data.audits[0].user.name;
724
+
725
+ return <span>{value}</span>;
726
+ } else {
727
+ return null;
728
+ }
729
+ },
730
+ };
731
+ case "currency":
732
+ return {
733
+ ...commonProps,
734
+ name: column.id,
735
+ render: ({ data }) => {
736
+ if (data) {
737
+ data = numeral(data[column.id]).format(
738
+ "$0,0.00"
739
+ );
740
+
741
+ return <span>{data}</span>;
742
+ } else {
743
+ return null;
744
+ }
745
+ },
746
+ };
747
+ case "date":
748
+ if (column.filter && column.filter.type) {
749
+ filterEditor = DateFilter;
750
+ filterEditorProps = (props, { index }) => {
751
+ return {
752
+ dateFormat: "DD-MM-YYYY",
753
+ placeholder: "All Dates",
754
+ };
755
+ };
756
+ } else {
757
+ filterEditor = null;
758
+ filterEditorProps = null;
759
+ }
760
+
761
+ return {
762
+ ...commonProps,
763
+ name: column.id,
764
+ filterEditor: filterEditor,
765
+ filterEditorProps: filterEditorProps,
766
+ render: ({ data }) => {
767
+ if (
768
+ data &&
769
+ data[column.id] &&
770
+ moment(data[column.id]).isValid() &&
771
+ moment(data[column.id]).format(
772
+ "YYYY-MM-DD"
773
+ ) !== "1970-01-01"
774
+ ) {
775
+ const value = moment(
776
+ data[column.id]
777
+ ).format(column.format || "DD-MM-YYYY");
778
+ style = {};
779
+
780
+ if (
781
+ column.active &&
782
+ column.active.id &&
783
+ column.active.value === "expired"
784
+ ) {
785
+ if (
786
+ data[column.active.id] &&
787
+ moment(
788
+ data[column.active.id]
789
+ ).isBefore(moment())
790
+ ) {
791
+ style = {
792
+ color: column.active.colour,
793
+ };
794
+ }
795
+ }
796
+
797
+ return <span style={style}>{value}</span>;
798
+ } else {
799
+ return null;
800
+ }
801
+ },
802
+ };
803
+ case "datetime":
804
+ if (column.filter && column.filter.type) {
805
+ filterEditor = DateFilter;
806
+ filterEditorProps = (props, { index }) => {
807
+ return {
808
+ dateFormat: "DD-MM-YYYY",
809
+ placeholder: "All Dates",
810
+ };
811
+ };
812
+ } else {
813
+ filterEditor = null;
814
+ filterEditorProps = null;
815
+ }
816
+
817
+ return {
818
+ ...commonProps,
819
+ name: column.id,
820
+ filterEditor: filterEditor,
821
+ filterEditorProps: filterEditorProps,
822
+ render: ({ data }) => {
823
+ if (
824
+ data &&
825
+ data[column.id] &&
826
+ moment(data[column.id]).isValid() &&
827
+ moment(data[column.id]).format(
828
+ "YYYY-MM-DD"
829
+ ) !== "1970-01-01"
830
+ ) {
831
+ data = moment(data[column.id]).format(
832
+ column.format
833
+ ? column.format
834
+ : "DD-MM-YYYY hh:mm A"
835
+ );
836
+
837
+ return <span>{data}</span>;
838
+ } else {
839
+ return null;
840
+ }
841
+ },
842
+ };
843
+ case "file":
844
+ return {
845
+ ...commonProps,
846
+ name: column.id,
847
+ sortable: false,
848
+ render: ({ data }) => {
849
+ if (data) {
850
+ return (
851
+ <span>
852
+ <CloudDownload
853
+ data-tooltip-id="system-tooltip"
854
+ data-tooltip-content="Download File"
855
+ strokeWidth={2}
856
+ size={18}
857
+ className="tdaction"
858
+ />
859
+ </span>
860
+ );
861
+ } else {
862
+ return null;
863
+ }
864
+ },
865
+ };
866
+ case "icons":
867
+ return {
868
+ ...commonProps,
869
+ name: column.id,
870
+ sortable: false,
871
+ render: ({ data }) => {
872
+ icons = [];
873
+
874
+ if (column.icons && column.icons.length > 0) {
875
+ column.icons.forEach((icon) => {
876
+ icons.push(
877
+ <img
878
+ src={icon.url}
879
+ alt={icon.name}
880
+ data-tooltip-id="system-tooltip"
881
+ data-tooltip-content={icon.name}
882
+ />
883
+ );
884
+ });
885
+ }
886
+
887
+ if (data) {
888
+ return <span>{icons}</span>;
889
+ } else {
890
+ return null;
891
+ }
892
+ },
893
+ };
894
+ case "image":
895
+ return {
896
+ ...commonProps,
897
+ name: column.id,
898
+ render: ({ data }) => {
899
+ if (data && data[column.id]) {
900
+ const fileUrl =
901
+ data[column.id][0]?.file_url ||
902
+ data[column.id]?.file_url;
903
+
904
+ if (fileUrl) {
905
+ return (
906
+ <span>
907
+ {parse(
908
+ `<img width="150" src="${fileUrl}" />`
909
+ )}
910
+ </span>
911
+ );
912
+ }
913
+ }
914
+
915
+ return null;
916
+ },
917
+ };
918
+ case "json":
919
+ return {
920
+ ...commonProps,
921
+ name: column.id,
922
+ render: ({ data }) => {
923
+ if (
924
+ data &&
925
+ data[column.id] &&
926
+ data[column.id][column.jsonData]
927
+ ) {
928
+ data = data[column.id][column.jsonData];
929
+
930
+ return <span>{data}</span>;
931
+ } else {
932
+ return null;
933
+ }
934
+ },
935
+ };
936
+ case "number":
937
+ return {
938
+ ...commonProps,
939
+ name: column.id,
940
+ type: "number",
941
+ };
942
+ case "option":
943
+ selectedDataSource = filterDataSource.reduce(
944
+ (acc, fds) => {
945
+ if (fds.id === column.id) {
946
+ return fds.dataset;
947
+ }
948
+ return acc;
949
+ },
950
+ null
951
+ );
952
+
953
+ if (
954
+ column.filter &&
955
+ column.filter.type &&
956
+ column.filter.type === "select"
957
+ ) {
958
+ filterEditor = SelectFilter;
959
+ filterEditorProps = {
960
+ placeholder: column.filter.placeholder
961
+ ? column.filter.placeholder
962
+ : "All Options",
963
+ dataSource: selectedDataSource,
964
+ };
965
+ } else {
966
+ filterEditor = null;
967
+ filterEditorProps = null;
968
+ }
969
+
970
+ return {
971
+ ...commonProps,
972
+ name: column.id,
973
+ filterEditor: filterEditor,
974
+ filterEditorProps: filterEditorProps,
975
+ render: ({ data }) => {
976
+ if (
977
+ data &&
978
+ column.options &&
979
+ column.options[data[column.id]]
980
+ ) {
981
+ data = column.options[data[column.id]];
982
+
983
+ return <span>{data}</span>;
984
+ } else {
985
+ return null;
986
+ }
987
+ },
988
+ };
989
+ case "placeholder":
990
+ return {
991
+ ...commonProps,
992
+ name: column.id,
993
+ sortable: false,
994
+ render: ({ data }) => {
995
+ if (data) {
996
+ return <span>{column.placeholder}</span>;
997
+ } else {
998
+ return null;
999
+ }
1000
+ },
1001
+ };
1002
+ case "relation":
1003
+ relationName = column.id.reduce(
1004
+ (acc, id) => acc + `${id}.`,
1005
+ ""
1006
+ );
1007
+ relationName += column.nameFrom;
1008
+
1009
+ selectedDataSource = filterDataSource.reduce(
1010
+ (acc, fds) => {
1011
+ if (fds.id === relationName) {
1012
+ return fds.dataset;
1013
+ }
1014
+ return acc;
1015
+ },
1016
+ null
1017
+ );
1018
+
1019
+ if (
1020
+ column.filter &&
1021
+ column.filter.type &&
1022
+ column.filter.type === "select"
1023
+ ) {
1024
+ filterEditor = SelectFilter;
1025
+ filterEditorProps = {
1026
+ placeholder: column.filter.placeholder
1027
+ ? column.filter.placeholder
1028
+ : "All Options",
1029
+ dataSource: selectedDataSource,
1030
+ };
1031
+ } else {
1032
+ filterEditor = null;
1033
+ filterEditorProps = null;
1034
+ }
1035
+
1036
+ return {
1037
+ ...commonProps,
1038
+ name: relationName,
1039
+ sortable: false,
1040
+ filterEditor: filterEditor,
1041
+ filterEditorProps: filterEditorProps,
1042
+ render: ({ data }) => {
1043
+ value = column.id.reduce((acc, id) => {
1044
+ if (acc === "") {
1045
+ return data[id];
1046
+ } else {
1047
+ if (acc && acc[id]) {
1048
+ return acc[id];
1049
+ } else {
1050
+ return "";
1051
+ }
1052
+ }
1053
+ }, "");
1054
+
1055
+ if (value) {
1056
+ if (Array.isArray(column.nameFrom)) {
1057
+ value = column.nameFrom
1058
+ .map((nf) => value[nf])
1059
+ .join(" ");
1060
+ } else {
1061
+ value = value[column.nameFrom];
1062
+ }
1063
+
1064
+ if (
1065
+ column.placeholder &&
1066
+ column.placeholder !== ""
1067
+ ) {
1068
+ return (
1069
+ <span>{column.placeholder}</span>
1070
+ );
1071
+ } else {
1072
+ return <span>{value}</span>;
1073
+ }
1074
+ } else {
1075
+ return null;
1076
+ }
1077
+ },
1078
+ };
1079
+ case "relationArray":
1080
+ relationName =
1081
+ column.id.reduce((acc, id) => acc + `${id}.`, "") +
1082
+ column.nameFrom;
1083
+
1084
+ selectedDataSource = filterDataSource.reduce(
1085
+ (acc, fds) => {
1086
+ if (fds.id === relationName) {
1087
+ return fds.dataset;
1088
+ }
1089
+ return acc;
1090
+ },
1091
+ null
1092
+ );
1093
+
1094
+ if (
1095
+ column.filter &&
1096
+ column.filter.type &&
1097
+ column.filter.type === "select"
1098
+ ) {
1099
+ filterEditor = SelectFilter;
1100
+ filterEditorProps = {
1101
+ placeholder: column.filter.placeholder
1102
+ ? column.filter.placeholder
1103
+ : "All Options",
1104
+ dataSource: selectedDataSource,
1105
+ };
1106
+ } else {
1107
+ filterEditor = null;
1108
+ filterEditorProps = null;
1109
+ }
1110
+
1111
+ return {
1112
+ ...commonProps,
1113
+ name: relationName,
1114
+ sortable: false,
1115
+ filterEditor: filterEditor,
1116
+ filterEditorProps: filterEditorProps,
1117
+ render: ({ data }) => {
1118
+ const relationValue = column.id.reduce(
1119
+ (acc, id) =>
1120
+ acc === ""
1121
+ ? data[id]
1122
+ : acc && acc[id]
1123
+ ? acc[id]
1124
+ : "",
1125
+ ""
1126
+ );
1127
+
1128
+ if (
1129
+ relationValue &&
1130
+ Array.isArray(relationValue)
1131
+ ) {
1132
+ const nameProperties = Array.isArray(
1133
+ column.nameFrom
1134
+ )
1135
+ ? column.nameFrom
1136
+ : [column.nameFrom];
1137
+
1138
+ const values = relationValue
1139
+ .map((rv) => {
1140
+ if (
1141
+ column.childId &&
1142
+ Array.isArray(column.childId)
1143
+ ) {
1144
+ childValue = rv;
1145
+
1146
+ column.childId.forEach((a) => {
1147
+ if (childValue[a]) {
1148
+ childValue =
1149
+ childValue[a];
1150
+ }
1151
+ });
1152
+
1153
+ if (childValue) {
1154
+ const propertyValues =
1155
+ nameProperties
1156
+ .map(
1157
+ (prop) =>
1158
+ childValue[
1159
+ prop
1160
+ ]
1161
+ )
1162
+ .filter(
1163
+ (value) =>
1164
+ value !==
1165
+ undefined &&
1166
+ value !==
1167
+ null
1168
+ );
1169
+ return propertyValues.join(
1170
+ " "
1171
+ );
1172
+ }
1173
+ } else {
1174
+ const propertyValues =
1175
+ nameProperties
1176
+ .map((prop) => rv[prop])
1177
+ .filter(
1178
+ (value) =>
1179
+ value !==
1180
+ undefined &&
1181
+ value !== null
1182
+ );
1183
+ return propertyValues.join(" ");
1184
+ }
1185
+ })
1186
+ .filter((value) => value !== "");
1187
+
1188
+ if (values.length > 0) {
1189
+ let result = "";
1190
+
1191
+ if (
1192
+ column.relation?.id &&
1193
+ column.relation?.key
1194
+ ) {
1195
+ const relationValue =
1196
+ column.relation.key.reduce(
1197
+ (acc, id) =>
1198
+ acc === ""
1199
+ ? data[id]
1200
+ : acc && acc[id]
1201
+ ? acc[id]
1202
+ : "",
1203
+ ""
1204
+ );
1205
+
1206
+ result = `${
1207
+ relationValue[
1208
+ column.relation.id
1209
+ ]
1210
+ }<br />`;
1211
+ }
1212
+
1213
+ if (
1214
+ column.where &&
1215
+ column.where.id &&
1216
+ column.where.value
1217
+ ) {
1218
+ if (
1219
+ data[column.where.id] ===
1220
+ column.where.value
1221
+ ) {
1222
+ result += values.join("<br />");
1223
+ }
1224
+ } else {
1225
+ result += values.join("<br />");
1226
+ }
1227
+
1228
+ return <span>{parse(result)}</span>;
1229
+ }
1230
+ }
1231
+
1232
+ return null;
1233
+ },
1234
+ };
1235
+ case "richtext":
1236
+ return {
1237
+ ...commonProps,
1238
+ name: column.id,
1239
+ render: ({ data }) => {
1240
+ if (data && data[column.id]) {
1241
+ return (
1242
+ <span>{parse(data[column.id])}</span>
1243
+ );
1244
+ } else {
1245
+ return null;
1246
+ }
1247
+ },
1248
+ };
1249
+ case "stage":
1250
+ return {
1251
+ ...commonProps,
1252
+ name: column.id,
1253
+ sortable: false,
1254
+ render: ({ data }) => {
1255
+ stage = "";
1256
+
1257
+ column.keyIds.forEach((a, b) => {
1258
+ if (data[a] === 1) {
1259
+ stage = column.valueIds[b];
1260
+ }
1261
+ });
1262
+
1263
+ return <span>{stage}</span>;
1264
+ },
1265
+ };
1266
+ case "time":
1267
+ return {
1268
+ ...commonProps,
1269
+ name: column.id,
1270
+ render: ({ data }) => {
1271
+ if (
1272
+ data &&
1273
+ data[column.id] &&
1274
+ moment(data[column.id]).isValid() &&
1275
+ moment(data[column.id]).format(
1276
+ "YYYY-MM-DD"
1277
+ ) !== "1970-01-01"
1278
+ ) {
1279
+ data = moment(data[column.id]).format(
1280
+ column.format
1281
+ ? column.format
1282
+ : "hh:mm A"
1283
+ );
1284
+
1285
+ return <span>{data}</span>;
1286
+ } else {
1287
+ return null;
1288
+ }
1289
+ },
1290
+ };
1291
+ case "url":
1292
+ return {
1293
+ ...commonProps,
1294
+ name: column.id,
1295
+ render: ({ data }) => {
1296
+ if (data && data[column.id]) {
1297
+ return (
1298
+ <span>
1299
+ <a
1300
+ href={data[column.id]}
1301
+ target="_blank"
1302
+ >
1303
+ Link
1304
+ </a>
1305
+ </span>
1306
+ );
1307
+ } else {
1308
+ return null;
1309
+ }
1310
+ },
1311
+ };
1312
+ default:
1313
+ selectedDataSource = filterDataSource.reduce(
1314
+ (acc, fds) => {
1315
+ if (fds.id === column.id) {
1316
+ return fds.dataset;
1317
+ }
1318
+ return acc;
1319
+ },
1320
+ null
1321
+ );
1322
+
1323
+ if (column.filter && column.filter.type) {
1324
+ switch (column.filter.type) {
1325
+ case "select":
1326
+ filterEditor = SelectFilter;
1327
+ filterEditorProps = {
1328
+ placeholder: column.filter.placeholder
1329
+ ? column.filter.placeholder
1330
+ : "All Options",
1331
+ dataSource: selectedDataSource,
1332
+ };
1333
+ break;
1334
+ default:
1335
+ filterEditor = null;
1336
+ filterEditorProps = null;
1337
+ break;
1338
+ }
1339
+ } else {
1340
+ filterEditor = null;
1341
+ filterEditorProps = null;
1342
+ }
1343
+
1344
+ return {
1345
+ ...commonProps,
1346
+ name: column.id,
1347
+ filterEditor: filterEditor,
1348
+ filterEditorProps: filterEditorProps,
1349
+ };
1350
+ }
1351
+ };
1352
+
1353
+ const newColumns = columns.map(renderColumn);
1354
+
1355
+ if (settings.length > 0) {
1356
+ newColumns.push({
1357
+ name: "setting",
1358
+ header: "Action",
1359
+ defaultWidth: 100,
1360
+ textAlign: "center",
1361
+ render: ({ data }) => {
1362
+ return (
1363
+ <div className="tdactions">
1364
+ {settings.map((setting) =>
1365
+ renderSetting(setting, data)
1366
+ )}
1367
+ </div>
1368
+ );
1369
+ },
1370
+ });
1371
+ }
1372
+
1373
+ setGridColumns(newColumns);
1374
+
1375
+ const newFilterValue = columns
1376
+ .filter((column) => column.filter && column.filter.type) // Only consider columns with a filter type
1377
+ .map((column) => {
1378
+ // Map these filtered columns to a new object array
1379
+ const filterName = Array.isArray(column.id)
1380
+ ? column.id.reduce((acc, id) => acc + `${id}.`, "") +
1381
+ column.nameFrom
1382
+ : column.id;
1383
+
1384
+ return {
1385
+ key: column.filter.key ? column.filter.key : null,
1386
+ name: filterName,
1387
+ operator: column.filter.operator,
1388
+ type: column.filter.type,
1389
+ value: column.filter.value ? column.filter.value : "",
1390
+ };
1391
+ });
1392
+
1393
+ setFilterValue(newFilterValue);
1394
+ }, [columns, filterDataSource]);
1395
+
1396
+ useEffect(() => {
1397
+ if (setFilterData) {
1398
+ setFilterData(filterValue);
1399
+ }
1400
+ }, [filterValue]);
1401
+
1402
+ useEffect(() => {
1403
+ columns.forEach((column) => {
1404
+ if (column.filter && column.filter.url) {
1405
+ CustomFetch(
1406
+ column.filter.url,
1407
+ "POST",
1408
+ column.filter.where
1409
+ ? { where: column.filter.where }
1410
+ : {},
1411
+ (res) => {
1412
+ const filterName = Array.isArray(column.id)
1413
+ ? column.id.reduce(
1414
+ (acc, id) => acc + `${id}.`,
1415
+ ""
1416
+ ) + column.nameFrom
1417
+ : column.id;
1418
+
1419
+ const filterIndex = filterDataSource.findIndex(
1420
+ (filter) => filter.id === filterName
1421
+ );
1422
+
1423
+ if (filterIndex >= 0) {
1424
+ const updatedFilter = {
1425
+ ...filterDataSource[filterIndex],
1426
+ dataset: res.data,
1427
+ };
1428
+ const updatedDataSource = [
1429
+ ...filterDataSource.slice(0, filterIndex),
1430
+ updatedFilter,
1431
+ ...filterDataSource.slice(filterIndex + 1),
1432
+ ];
1433
+ setFilterDataSource(updatedDataSource);
1434
+ } else {
1435
+ const newFilter = {
1436
+ id: filterName,
1437
+ dataset: res.data,
1438
+ };
1439
+ setFilterDataSource((prevState) => [
1440
+ ...prevState,
1441
+ newFilter,
1442
+ ]);
1443
+ }
1444
+ }
1445
+ );
1446
+ } else if (column.filter && column.filter.options) {
1447
+ const filterName = Array.isArray(column.id)
1448
+ ? column.id.reduce((acc, id) => acc + `${id}.`, "") +
1449
+ column.nameFrom
1450
+ : column.id;
1451
+
1452
+ const newFilter = {
1453
+ id: filterName,
1454
+ dataset: column.filter.options,
1455
+ };
1456
+
1457
+ setFilterDataSource((prevState) => [
1458
+ ...prevState,
1459
+ newFilter,
1460
+ ]);
1461
+ }
1462
+ });
1463
+ }, []);
1464
+
1465
+ // Data Grid Styling
1466
+ const [windowHeight, setWindowHeight] = useState(window.innerHeight);
1467
+ const gridStyle = {
1468
+ minHeight: windowHeight * 0.7 > 550 ? windowHeight * 0.7 : 550,
1469
+ boxShadow: "none",
1470
+ };
1471
+ const headerProps = {
1472
+ style: style ? style : {},
1473
+ };
1474
+
1475
+ useEffect(() => {
1476
+ const handleResize = () => {
1477
+ setWindowHeight(window.innerHeight);
1478
+ };
1479
+
1480
+ window.addEventListener("resize", handleResize);
1481
+
1482
+ // Clean up the event listener
1483
+ return () => {
1484
+ window.removeEventListener("resize", handleResize);
1485
+ };
1486
+ }, []);
1487
+
1488
+ return (
1489
+ <>
1490
+ {renderSearch()}
1491
+ {renderCreateButton()}
1492
+ <ReactDataGrid
1493
+ columns={gridColumns}
1494
+ dataSource={data}
1495
+ filterValue={filterValue}
1496
+ defaultLimit={limit}
1497
+ defaultSortInfo={sortInfo}
1498
+ enableColumnAutosize={false}
1499
+ enableColumnFilterContextMenu={false}
1500
+ enableSelection={false}
1501
+ headerProps={headerProps}
1502
+ idProperty={`visns-datagrid-${ajaxSetting.url.replace(
1503
+ /\//g,
1504
+ "-"
1505
+ )}`}
1506
+ minRowHeight={40}
1507
+ onFilterValueChange={setFilterValue}
1508
+ onRenderRow={onRenderRow}
1509
+ pagination
1510
+ renderColumnContextMenu={renderColumnContextMenu}
1511
+ rowHeight={null}
1512
+ showZebraRows={true}
1513
+ style={gridStyle}
1514
+ />
1515
+ <Popup
1516
+ open={modalShow}
1517
+ onClose={modalClose}
1518
+ closeOnDocumentClick={false}
1519
+ >
1520
+ <Form
1521
+ closeModal={modalClose}
1522
+ fetchTable={handleReload}
1523
+ formSettings={formData}
1524
+ formType={formType}
1525
+ updateForm={setFormData}
1526
+ columnId={formId}
1527
+ childDropdownCallback={childDropdownCallback}
1528
+ style={style}
1529
+ />
1530
+ </Popup>
1531
+ </>
1532
+ );
1533
+ }
1535
1534
  );
1536
1535
 
1537
1536
  export default DataGrid;