@visns-studio/visns-components 1.2.5 → 1.2.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.
@@ -5,6 +5,10 @@ import debounce from "lodash.debounce";
5
5
  import moment from "moment";
6
6
  import numeral from "numeral";
7
7
  import Popup from "reactjs-popup";
8
+ import parse from "html-react-parser";
9
+ import NumberFilter from "@inovua/reactdatagrid-community/NumberFilter";
10
+ import SelectFilter from "@inovua/reactdatagrid-community/SelectFilter";
11
+ import DateFilter from "@inovua/reactdatagrid-community/DateFilter";
8
12
  import ReactDataGrid from "@inovua/reactdatagrid-community";
9
13
  import { confirmAlert } from "react-confirm-alert";
10
14
  import { toast } from "react-toastify";
@@ -105,6 +109,8 @@ const DataGrid = (props) => {
105
109
  (params) => loadData(params, dSearch, ajaxSetting),
106
110
  [ajaxSetting, dataReload, dSearch]
107
111
  );
112
+ const [filterDataSource, setFilterDataSource] = useState([]);
113
+ const [filterValue, setFilterValue] = useState([]);
108
114
  const [gridColumns, setGridColumns] = useState([]);
109
115
  const limit = ajaxSetting.take || 10;
110
116
  const [search, setSearch] = useState("");
@@ -157,7 +163,9 @@ const DataGrid = (props) => {
157
163
  `The selected item has been deleted.`
158
164
  );
159
165
  } else {
160
- toast.error(String(result.error));
166
+ toast.error(
167
+ <div>{parse(result.error)}</div>
168
+ );
161
169
  }
162
170
  }
163
171
  ),
@@ -195,7 +203,9 @@ const DataGrid = (props) => {
195
203
 
196
204
  toast.success(String(s.title));
197
205
  } else {
198
- toast.error(String(result.error));
206
+ toast.error(
207
+ <div>{parse(result.error)}</div>
208
+ );
199
209
  }
200
210
  }
201
211
  ),
@@ -389,28 +399,41 @@ const DataGrid = (props) => {
389
399
 
390
400
  useEffect(() => {
391
401
  const renderColumn = (column) => {
402
+ let filterEditor = null;
403
+ let filterEditorProps = null;
404
+ let selectedDataSource = null;
405
+ let icons = [];
406
+ let relationName;
407
+ let stage;
408
+ let value;
409
+
410
+ const commonProps = {
411
+ header: column.label,
412
+ defaultFlex: 1,
413
+ link: column.link ? column.link : {},
414
+ minWidth:
415
+ column.minWidth && column.minWidth > 0
416
+ ? column.minWidth
417
+ : undefined,
418
+ maxWidth:
419
+ column.maxWidth && column.maxWidth > 0
420
+ ? column.maxWidth
421
+ : undefined,
422
+ };
423
+
392
424
  switch (column.type) {
393
425
  case "created_by":
394
426
  return {
427
+ ...commonProps,
395
428
  name: "audits.name",
396
- header: column.label,
397
- defaultFlex: 1,
398
- link: column.link ? column.link : {},
399
- minWidth:
400
- column.minWidth && column.minWidth > 0
401
- ? column.minWidth
402
- : undefined,
403
- maxWidth:
404
- column.maxWidth && column.maxWidth > 0
405
- ? column.maxWidth
406
- : undefined,
429
+ sortable: false,
407
430
  render: ({ data }) => {
408
431
  if (
409
432
  data.audits[0] &&
410
433
  data.audits[0].user &&
411
434
  data.audits[0].user.name
412
435
  ) {
413
- let value = data.audits[0].user.name;
436
+ value = data.audits[0].user.name;
414
437
 
415
438
  return <span>{value}</span>;
416
439
  } else {
@@ -420,18 +443,8 @@ const DataGrid = (props) => {
420
443
  };
421
444
  case "currency":
422
445
  return {
446
+ ...commonProps,
423
447
  name: column.id,
424
- header: column.label,
425
- defaultFlex: 1,
426
- minWidth:
427
- column.minWidth && column.minWidth > 0
428
- ? column.minWidth
429
- : undefined,
430
- maxWidth:
431
- column.maxWidth && column.maxWidth > 0
432
- ? column.maxWidth
433
- : undefined,
434
- link: column.link ? column.link : {},
435
448
  render: ({ data }) => {
436
449
  if (data) {
437
450
  data = numeral(data[column.id]).format(
@@ -445,19 +458,24 @@ const DataGrid = (props) => {
445
458
  },
446
459
  };
447
460
  case "date":
461
+ if (column.filter && column.filter.type) {
462
+ filterEditor = DateFilter;
463
+ filterEditorProps = (props, { index }) => {
464
+ return {
465
+ dateFormat: "DD-MM-YYYY",
466
+ placeholder: "All Dates",
467
+ };
468
+ };
469
+ } else {
470
+ filterEditor = null;
471
+ filterEditorProps = null;
472
+ }
473
+
448
474
  return {
475
+ ...commonProps,
449
476
  name: column.id,
450
- header: column.label,
451
- defaultFlex: 1,
452
- minWidth:
453
- column.minWidth && column.minWidth > 0
454
- ? column.minWidth
455
- : undefined,
456
- maxWidth:
457
- column.maxWidth && column.maxWidth > 0
458
- ? column.maxWidth
459
- : undefined,
460
- link: column.link ? column.link : {},
477
+ filterEditor: filterEditor,
478
+ filterEditorProps: filterEditorProps,
461
479
  render: ({ data }) => {
462
480
  if (data && data[column.id]) {
463
481
  data = moment(data[column.id]).format(
@@ -471,19 +489,24 @@ const DataGrid = (props) => {
471
489
  },
472
490
  };
473
491
  case "datetime":
492
+ if (column.filter && column.filter.type) {
493
+ filterEditor = DateFilter;
494
+ filterEditorProps = (props, { index }) => {
495
+ return {
496
+ dateFormat: "DD-MM-YYYY",
497
+ placeholder: "All Dates",
498
+ };
499
+ };
500
+ } else {
501
+ filterEditor = null;
502
+ filterEditorProps = null;
503
+ }
504
+
474
505
  return {
506
+ ...commonProps,
475
507
  name: column.id,
476
- header: column.label,
477
- defaultFlex: 1,
478
- minWidth:
479
- column.minWidth && column.minWidth > 0
480
- ? column.minWidth
481
- : undefined,
482
- maxWidth:
483
- column.maxWidth && column.maxWidth > 0
484
- ? column.maxWidth
485
- : undefined,
486
- link: column.link ? column.link : {},
508
+ filterEditor: filterEditor,
509
+ filterEditorProps: filterEditorProps,
487
510
  render: ({ data }) => {
488
511
  if (data && data[column.id]) {
489
512
  data = moment(data[column.id]).format(
@@ -500,18 +523,9 @@ const DataGrid = (props) => {
500
523
  };
501
524
  case "file":
502
525
  return {
526
+ ...commonProps,
503
527
  name: column.id,
504
- header: column.label,
505
- defaultFlex: 1,
506
- minWidth:
507
- column.minWidth && column.minWidth > 0
508
- ? column.minWidth
509
- : undefined,
510
- maxWidth:
511
- column.maxWidth && column.maxWidth > 0
512
- ? column.maxWidth
513
- : undefined,
514
- link: column.link ? column.link : {},
528
+ sortable: false,
515
529
  render: ({ data }) => {
516
530
  if (data) {
517
531
  return (
@@ -532,20 +546,11 @@ const DataGrid = (props) => {
532
546
  };
533
547
  case "icons":
534
548
  return {
549
+ ...commonProps,
535
550
  name: column.id,
536
- header: column.label,
537
- defaultFlex: 1,
538
- minWidth:
539
- column.minWidth && column.minWidth > 0
540
- ? column.minWidth
541
- : undefined,
542
- maxWidth:
543
- column.maxWidth && column.maxWidth > 0
544
- ? column.maxWidth
545
- : undefined,
546
- link: column.link ? column.link : {},
551
+ sortable: false,
547
552
  render: ({ data }) => {
548
- let icons = [];
553
+ icons = [];
549
554
 
550
555
  if (column.icons && column.icons.length > 0) {
551
556
  column.icons.forEach((icon) => {
@@ -560,23 +565,17 @@ const DataGrid = (props) => {
560
565
  });
561
566
  }
562
567
 
563
- return <span>{icons}</span>;
568
+ if (data) {
569
+ return <span>{icons}</span>;
570
+ } else {
571
+ return null;
572
+ }
564
573
  },
565
574
  };
566
575
  case "json":
567
576
  return {
577
+ ...commonProps,
568
578
  name: column.id,
569
- header: column.label,
570
- defaultFlex: 1,
571
- minWidth:
572
- column.minWidth && column.minWidth > 0
573
- ? column.minWidth
574
- : undefined,
575
- maxWidth:
576
- column.maxWidth && column.maxWidth > 0
577
- ? column.maxWidth
578
- : undefined,
579
- link: column.link ? column.link : {},
580
579
  render: ({ data }) => {
581
580
  if (
582
581
  data &&
@@ -593,34 +592,14 @@ const DataGrid = (props) => {
593
592
  };
594
593
  case "number":
595
594
  return {
595
+ ...commonProps,
596
596
  name: column.id,
597
- header: column.label,
598
- defaultFlex: 1,
599
- minWidth:
600
- column.minWidth && column.minWidth > 0
601
- ? column.minWidth
602
- : undefined,
603
- maxWidth:
604
- column.maxWidth && column.maxWidth > 0
605
- ? column.maxWidth
606
- : undefined,
607
- link: column.link ? column.link : {},
608
597
  type: "number",
609
598
  };
610
599
  case "option":
611
600
  return {
601
+ ...commonProps,
612
602
  name: column.id,
613
- header: column.label,
614
- defaultFlex: 1,
615
- minWidth:
616
- column.minWidth && column.minWidth > 0
617
- ? column.minWidth
618
- : undefined,
619
- maxWidth:
620
- column.maxWidth && column.maxWidth > 0
621
- ? column.maxWidth
622
- : undefined,
623
- link: column.link ? column.link : {},
624
603
  render: ({ data }) => {
625
604
  if (
626
605
  data &&
@@ -637,44 +616,50 @@ const DataGrid = (props) => {
637
616
  };
638
617
  case "placeholder":
639
618
  return {
619
+ ...commonProps,
640
620
  name: column.id,
641
- header: column.label,
642
- defaultFlex: 1,
643
- minWidth:
644
- column.minWidth && column.minWidth > 0
645
- ? column.minWidth
646
- : undefined,
647
- maxWidth:
648
- column.maxWidth && column.maxWidth > 0
649
- ? column.maxWidth
650
- : undefined,
651
- link: column.link ? column.link : {},
621
+ sortable: false,
652
622
  render: ({ data }) => {
653
- return <span>{column.placeholder}</span>;
623
+ if (data) {
624
+ return <span>{column.placeholder}</span>;
625
+ } else {
626
+ return null;
627
+ }
654
628
  },
655
629
  };
656
630
  case "relation":
657
- let relationName = column.id.reduce(
631
+ relationName = column.id.reduce(
658
632
  (acc, id) => acc + `${id}.`,
659
633
  ""
660
634
  );
661
635
  relationName += column.nameFrom;
662
636
 
637
+ selectedDataSource = filterDataSource.reduce((acc, fds) => {
638
+ if (fds.id === relationName) {
639
+ return fds.dataset;
640
+ }
641
+ return acc;
642
+ }, null);
643
+
644
+ if (column.filter && column.filter.type) {
645
+ filterEditor = SelectFilter;
646
+ filterEditorProps = {
647
+ placeholder: "All Options",
648
+ dataSource: selectedDataSource,
649
+ };
650
+ } else {
651
+ filterEditor = null;
652
+ filterEditorProps = null;
653
+ }
654
+
663
655
  return {
656
+ ...commonProps,
664
657
  name: relationName,
665
- header: column.label,
666
- defaultFlex: 1,
667
- minWidth:
668
- column.minWidth && column.minWidth > 0
669
- ? column.minWidth
670
- : undefined,
671
- maxWidth:
672
- column.maxWidth && column.maxWidth > 0
673
- ? column.maxWidth
674
- : undefined,
675
- link: column.link ? column.link : {},
658
+ sortable: false,
659
+ filterEditor: filterEditor,
660
+ filterEditorProps: filterEditorProps,
676
661
  render: ({ data }) => {
677
- let value = column.id.reduce((acc, id) => {
662
+ value = column.id.reduce((acc, id) => {
678
663
  if (acc === "") {
679
664
  return data[id];
680
665
  } else {
@@ -705,23 +690,34 @@ const DataGrid = (props) => {
705
690
  },
706
691
  };
707
692
  case "relationArray":
708
- const relationArrayName =
693
+ relationName =
709
694
  column.id.reduce((acc, id) => acc + `${id}.`, "") +
710
695
  column.nameFrom;
711
696
 
697
+ selectedDataSource = filterDataSource.reduce((acc, fds) => {
698
+ if (fds.id === relationName) {
699
+ return fds.dataset;
700
+ }
701
+ return acc;
702
+ }, null);
703
+
704
+ if (column.filter && column.filter.type) {
705
+ filterEditor = SelectFilter;
706
+ filterEditorProps = {
707
+ placeholder: "All Options",
708
+ dataSource: selectedDataSource,
709
+ };
710
+ } else {
711
+ filterEditor = null;
712
+ filterEditorProps = null;
713
+ }
714
+
712
715
  return {
713
- name: relationArrayName,
714
- header: column.label,
715
- defaultFlex: 1,
716
- minWidth:
717
- column.minWidth && column.minWidth > 0
718
- ? column.minWidth
719
- : undefined,
720
- maxWidth:
721
- column.maxWidth && column.maxWidth > 0
722
- ? column.maxWidth
723
- : undefined,
724
- link: column.link ? column.link : {},
716
+ ...commonProps,
717
+ name: relationName,
718
+ sortable: false,
719
+ filterEditor: filterEditor,
720
+ filterEditorProps: filterEditorProps,
725
721
  render: ({ data }) => {
726
722
  const relationValue = column.id.reduce(
727
723
  (acc, id) => (acc === "" ? data[id] : acc[id]),
@@ -741,20 +737,11 @@ const DataGrid = (props) => {
741
737
 
742
738
  case "stage":
743
739
  return {
740
+ ...commonProps,
744
741
  name: column.id,
745
- header: column.label,
746
- defaultFlex: 1,
747
- minWidth:
748
- column.minWidth && column.minWidth > 0
749
- ? column.minWidth
750
- : undefined,
751
- maxWidth:
752
- column.maxWidth && column.maxWidth > 0
753
- ? column.maxWidth
754
- : undefined,
755
- link: column.link ? column.link : {},
742
+ sortable: false,
756
743
  render: ({ data }) => {
757
- let stage = "";
744
+ stage = "";
758
745
 
759
746
  column.keyIds.forEach((a, b) => {
760
747
  if (data[a] === 1) {
@@ -767,18 +754,8 @@ const DataGrid = (props) => {
767
754
  };
768
755
  case "time":
769
756
  return {
757
+ ...commonProps,
770
758
  name: column.id,
771
- header: column.label,
772
- defaultFlex: 1,
773
- minWidth:
774
- column.minWidth && column.minWidth > 0
775
- ? column.minWidth
776
- : undefined,
777
- maxWidth:
778
- column.maxWidth && column.maxWidth > 0
779
- ? column.maxWidth
780
- : undefined,
781
- link: column.link ? column.link : {},
782
759
  render: ({ data }) => {
783
760
  if (data && data[column.id]) {
784
761
  data = moment(data[column.id]).format(
@@ -793,18 +770,8 @@ const DataGrid = (props) => {
793
770
  };
794
771
  case "url":
795
772
  return {
773
+ ...commonProps,
796
774
  name: column.id,
797
- header: column.label,
798
- defaultFlex: 1,
799
- minWidth:
800
- column.minWidth && column.minWidth > 0
801
- ? column.minWidth
802
- : undefined,
803
- maxWidth:
804
- column.maxWidth && column.maxWidth > 0
805
- ? column.maxWidth
806
- : undefined,
807
- link: column.link ? column.link : {},
808
775
  render: ({ data }) => {
809
776
  if (data) {
810
777
  return (
@@ -821,18 +788,8 @@ const DataGrid = (props) => {
821
788
  };
822
789
  default:
823
790
  return {
791
+ ...commonProps,
824
792
  name: column.id,
825
- header: column.label,
826
- defaultFlex: 1,
827
- minWidth:
828
- column.minWidth && column.minWidth > 0
829
- ? column.minWidth
830
- : undefined,
831
- maxWidth:
832
- column.maxWidth && column.maxWidth > 0
833
- ? column.maxWidth
834
- : undefined,
835
- link: column.link ? column.link : {},
836
793
  };
837
794
  }
838
795
  };
@@ -867,7 +824,62 @@ const DataGrid = (props) => {
867
824
  }
868
825
 
869
826
  setGridColumns(newColumns);
870
- }, [columns]);
827
+
828
+ const newFilterValue = columns
829
+ .filter((column) => column.filter && column.filter.type) // Only consider columns with a filter type
830
+ .map((column) => {
831
+ // Map these filtered columns to a new object array
832
+ const filterName = Array.isArray(column.id)
833
+ ? column.id.reduce((acc, id) => acc + `${id}.`, "") +
834
+ column.nameFrom
835
+ : column.id;
836
+
837
+ return {
838
+ name: filterName,
839
+ operator: column.filter.operator,
840
+ type: column.filter.type,
841
+ value: "",
842
+ };
843
+ });
844
+
845
+ setFilterValue(newFilterValue);
846
+ }, [columns, filterDataSource]);
847
+
848
+ useEffect(() => {
849
+ columns.forEach((column) => {
850
+ if (column.filter && column.filter.url) {
851
+ CustomFetch(column.filter.url, "POST", {}, (res) => {
852
+ const filterName = Array.isArray(column.id)
853
+ ? column.id.reduce((acc, id) => acc + `${id}.`, "") +
854
+ column.nameFrom
855
+ : column.id;
856
+
857
+ const filterIndex = filterDataSource.findIndex(
858
+ (filter) => filter.id === filterName
859
+ );
860
+
861
+ if (filterIndex >= 0) {
862
+ const updatedFilter = {
863
+ ...filterDataSource[filterIndex],
864
+ dataset: res.data,
865
+ };
866
+ const updatedDataSource = [
867
+ ...filterDataSource.slice(0, filterIndex),
868
+ updatedFilter,
869
+ ...filterDataSource.slice(filterIndex + 1),
870
+ ];
871
+ setFilterDataSource(updatedDataSource);
872
+ } else {
873
+ const newFilter = { id: filterName, dataset: res.data };
874
+ setFilterDataSource((prevState) => [
875
+ ...prevState,
876
+ newFilter,
877
+ ]);
878
+ }
879
+ });
880
+ }
881
+ });
882
+ }, []);
871
883
 
872
884
  // Data Grid Styling
873
885
  const [windowHeight, setWindowHeight] = useState(window.innerHeight);
@@ -898,9 +910,11 @@ const DataGrid = (props) => {
898
910
  <ReactDataGrid
899
911
  columns={gridColumns}
900
912
  dataSource={data}
913
+ filterValue={filterValue}
901
914
  defaultLimit={limit}
902
915
  defaultSortInfo={sortInfo}
903
916
  enableColumnAutosize={false}
917
+ enableColumnFilterContextMenu={false}
904
918
  headerProps={headerProps}
905
919
  idProperty={`visns-datagrid-${ajaxSetting.url.replace(
906
920
  /\//g,
@@ -1,102 +1,65 @@
1
- import _ from 'lodash';
2
- import axios from 'axios';
3
- import { trackPromise } from 'react-promise-tracker';
1
+ import axios from "axios";
2
+ import { trackPromise } from "react-promise-tracker";
4
3
 
5
- const CustomDownload = (
6
- url,
7
- method,
8
- formData,
9
- successCallback,
10
- errorCallback
4
+ const CustomDownload = async (
5
+ url,
6
+ method,
7
+ formData,
8
+ successCallback,
9
+ errorCallback
11
10
  ) => {
12
- let baseUrl = '';
13
- let fileUpload = false;
14
- let body;
15
- let headers = {};
16
- let options = {};
11
+ let headers = {};
12
+ let options = {};
17
13
 
18
- if (!_.isEmpty(formData)) {
19
- Object.keys(formData).forEach((item) => {
20
- if (formData[item] !== undefined) {
21
- if (formData[item] instanceof File && formData[item] !== null) {
22
- fileUpload = true;
23
- }
24
- }
25
- });
26
- }
14
+ if (method.toUpperCase() === "PUT" || method.toUpperCase() === "PATCH") {
15
+ formData = { ...formData, _method: method };
16
+ }
27
17
 
28
- if (method.toUpperCase() === 'PUT' || method.toUpperCase() === 'PATCH') {
29
- if (fileUpload === false) {
30
- body = {
31
- ...body,
32
- _method: method,
33
- };
34
- }
35
- }
18
+ if (!(formData instanceof FormData)) {
19
+ headers["Content-Type"] = "application/json";
20
+ formData = JSON.stringify(formData);
21
+ } else {
22
+ headers["Content-Type"] = "multipart/form-data";
23
+ headers.contentType = false;
24
+ headers.processData = false;
36
25
 
37
- if (fileUpload === false) {
38
- body = JSON.stringify(formData);
39
- headers = {
40
- ...headers,
41
- 'Content-Type': 'application/json',
42
- };
43
- } else {
44
- body = new FormData();
26
+ if (
27
+ method.toUpperCase() === "PUT" ||
28
+ method.toUpperCase() === "PATCH"
29
+ ) {
30
+ formData.append("_method", method);
31
+ method = "POST";
32
+ }
33
+ }
45
34
 
46
- headers = {
47
- ...headers,
48
- 'Content-Type': 'multipart/form-data',
49
- contentType: false,
50
- processData: false,
51
- };
35
+ options = {
36
+ method: method,
37
+ data: formData,
38
+ headers: headers,
39
+ url: url,
40
+ responseType: "blob",
41
+ };
52
42
 
53
- if (!_.isEmpty(formData)) {
54
- Object.keys(formData).forEach((item) => {
55
- body.append(item, formData[item]);
56
- });
57
- }
43
+ try {
44
+ const response = await trackPromise(axios(options));
45
+ successCallback(response.data);
46
+ } catch (error) {
47
+ if (
48
+ error.response &&
49
+ error.response.data &&
50
+ error.response.data.message
51
+ ) {
52
+ const errorMessage = error.response.data.message;
58
53
 
59
- if (
60
- method.toUpperCase() === 'PUT' ||
61
- method.toUpperCase() === 'PATCH'
62
- ) {
63
- if (fileUpload === false) {
64
- } else {
65
- body.append('_method', method);
66
- method = 'POST';
67
- }
68
- }
69
- }
54
+ if (errorMessage === "Unauthenticated.") {
55
+ localStorage.clear();
56
+ }
70
57
 
71
- options = {
72
- method: method,
73
- data: body,
74
- headers: headers,
75
- url: baseUrl + url,
76
- responseType: 'blob',
77
- };
78
-
79
- trackPromise(
80
- axios(options).then(
81
- (result) => {
82
- successCallback(result.data);
83
- },
84
- (error) => {
85
- if (
86
- error.response.hasOwnProperty('data') &&
87
- error.response.data.hasOwnProperty('message') &&
88
- error.response.data.message !== ''
89
- ) {
90
- if (error.response.data.message === 'Unauthenticated.') {
91
- localStorage.clear();
92
- }
93
- errorCallback(error.response.data.message);
94
- } else {
95
- errorCallback(error);
96
- }
97
- }
98
- )
99
- );
58
+ errorCallback(errorMessage);
59
+ } else {
60
+ errorCallback(error);
61
+ }
62
+ }
100
63
  };
101
64
 
102
65
  export default CustomDownload;
@@ -1,101 +1,79 @@
1
- import _ from 'lodash';
2
- import axios from 'axios';
3
- import { trackPromise } from 'react-promise-tracker';
4
- import { toast } from 'react-toastify';
5
- import parse from 'html-react-parser';
1
+ import axios from "axios";
2
+ import { trackPromise } from "react-promise-tracker";
3
+ import { toast } from "react-toastify";
4
+ import parse from "html-react-parser";
6
5
 
7
- const CustomFetch = (
8
- url,
9
- method,
10
- formData,
11
- successCallback = null,
12
- errorCallback = null
6
+ const CustomFetch = async (
7
+ url,
8
+ method,
9
+ formData,
10
+ successCallback = null,
11
+ errorCallback = null
13
12
  ) => {
14
- let baseUrl = '';
15
- let body;
16
- let headers = {};
17
- let options = {};
13
+ const baseUrl = "";
14
+ const headers = {
15
+ "Content-Type": "application/json",
16
+ };
18
17
 
19
- if (method.toUpperCase() === 'PUT' || method.toUpperCase() === 'PATCH') {
20
- body = {
21
- ...body,
22
- _method: method,
23
- };
24
- }
18
+ if (method.toUpperCase() === "PUT" || method.toUpperCase() === "PATCH") {
19
+ formData = {
20
+ ...formData,
21
+ _method: method,
22
+ };
23
+ }
25
24
 
26
- body = JSON.stringify(formData);
27
- headers = {
28
- ...headers,
29
- 'Content-Type': 'application/json',
30
- };
25
+ const options = {
26
+ method: method,
27
+ data: formData,
28
+ headers: headers,
29
+ url: baseUrl + url,
30
+ };
31
31
 
32
- options = {
33
- method: method,
34
- data: body,
35
- headers: headers,
36
- url: baseUrl + url,
37
- };
32
+ try {
33
+ const response = await trackPromise(axios(options));
38
34
 
39
- trackPromise(
40
- axios(options).then(
41
- (result) => {
42
- if (successCallback) {
43
- successCallback(result.data);
44
- } else {
45
- if (result.data.error === '') {
46
- toast.success('Data has been updated successfully.');
47
- } else {
48
- toast.error(result.data.error);
49
- }
50
- }
51
- },
52
- (error) => {
53
- if (
54
- error.response.hasOwnProperty('data') &&
55
- error.response.data.hasOwnProperty('message') &&
56
- error.response.data.message !== ''
57
- ) {
58
- if (error.response.data.hasOwnProperty('errors')) {
59
- let errorMessage = '';
60
- let errors = error.response.data.errors;
35
+ if (successCallback) {
36
+ successCallback(response.data);
37
+ } else {
38
+ const error =
39
+ response.data.error || "Data has been updated successfully.";
40
+ toast.error(<div>{parse(error)}</div>);
41
+ }
42
+ } catch (error) {
43
+ let errorMessage = "";
61
44
 
62
- Object.keys(errors).forEach((a) => {
63
- errors[a].forEach((b) => {
64
- errorMessage += b + '<br />';
65
- });
66
- });
45
+ if (
46
+ error.response &&
47
+ error.response.data &&
48
+ error.response.data.message
49
+ ) {
50
+ const { data } = error.response;
67
51
 
68
- if (errorCallback) {
69
- errorCallback(errorMessage);
70
- } else {
71
- toast.error(`<div>${parse(errorMessage)}</div>`);
72
- }
73
- } else {
74
- if (
75
- error.response.data.message ===
76
- 'CSRF token mismatch.'
77
- ) {
78
- window.location.replace('/login');
79
- } else {
80
- if (errorCallback) {
81
- errorCallback(error.response.data.message);
82
- } else {
83
- toast.error(
84
- `<div>${parse(errorMessage)}</div>`
85
- );
86
- }
87
- }
88
- }
89
- } else {
90
- if (errorCallback) {
91
- errorCallback(error);
92
- } else {
93
- toast.error(`<div>${parse(errorMessage)}</div>`);
94
- }
95
- }
96
- }
97
- )
98
- );
52
+ if (data.errors) {
53
+ const { errors } = data;
54
+
55
+ Object.values(errors).forEach((errorArray) => {
56
+ errorArray.forEach((errorMsg) => {
57
+ errorMessage += `${errorMsg}<br />`;
58
+ });
59
+ });
60
+ } else {
61
+ if (data.message === "CSRF token mismatch.") {
62
+ window.location.replace("/login");
63
+ } else {
64
+ errorMessage = data.message;
65
+ }
66
+ }
67
+ } else {
68
+ errorMessage = error.message;
69
+ }
70
+
71
+ if (errorCallback) {
72
+ errorCallback(errorMessage);
73
+ } else {
74
+ toast.error(<div>{parse(errorMessage)}</div>);
75
+ }
76
+ }
99
77
  };
100
78
 
101
79
  export default CustomFetch;
package/package.json CHANGED
@@ -12,6 +12,7 @@
12
12
  "numeral": "^2.0.6",
13
13
  "react-confirm-alert": "^3.0.6",
14
14
  "react-datepicker": "^4.14.0",
15
+ "react-dropzone": "^14.2.3",
15
16
  "react-number-format": "^5.2.2",
16
17
  "react-promise-tracker": "^2.1.1",
17
18
  "react-quill": "^2.0.0",
@@ -29,11 +30,11 @@
29
30
  "react-dom": "^18.2.0"
30
31
  },
31
32
  "peerDependencies": {
32
- "react": ">=18.2.0",
33
- "react-dom": ">=18.2.0"
33
+ "react": "^17.0.1",
34
+ "react-dom": "^17.0.1"
34
35
  },
35
36
  "name": "@visns-studio/visns-components",
36
- "version": "1.2.5",
37
+ "version": "1.2.6",
37
38
  "description": "Various packages to assist in the development of our Custom Applications.",
38
39
  "main": "index.js",
39
40
  "devDependencies": {},