@visns-studio/visns-components 1.2.5 → 1.2.7

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";
@@ -24,6 +28,7 @@ import "react-confirm-alert/src/react-confirm-alert.css";
24
28
 
25
29
  import CustomFetch from "./visns-fetch";
26
30
  import Form from "./visns-form";
31
+ import { NonceProvider } from "react-select";
27
32
 
28
33
  const loadData = async (
29
34
  { skip, limit, sortInfo, currentData, filterValue, groupBy },
@@ -31,9 +36,8 @@ const loadData = async (
31
36
  ajaxSetting
32
37
  ) => {
33
38
  const url = ajaxSetting.url;
34
-
35
- let page = Math.floor(skip / limit) + 1;
36
- let params = { page, sortInfo, take: limit, search: search };
39
+ const page = Math.floor(skip / limit) + 1;
40
+ const params = { page, sortInfo, take: limit, search: search, where: [] };
37
41
 
38
42
  if (sortInfo) {
39
43
  params.sortBy = sortInfo.name;
@@ -44,6 +48,16 @@ const loadData = async (
44
48
  params.where = ajaxSetting.where;
45
49
  }
46
50
 
51
+ filterValue.forEach((fv) => {
52
+ const identifier = fv.key ? fv.key : fv.name;
53
+
54
+ if (fv.value !== null && fv.value !== "") {
55
+ params.where.push({ id: identifier, value: fv.value });
56
+ } else {
57
+ params.where = params.where.filter((w) => w.id !== identifier);
58
+ }
59
+ });
60
+
47
61
  try {
48
62
  const response = await axios.post(url, params);
49
63
  const { data, total } = response.data;
@@ -105,6 +119,8 @@ const DataGrid = (props) => {
105
119
  (params) => loadData(params, dSearch, ajaxSetting),
106
120
  [ajaxSetting, dataReload, dSearch]
107
121
  );
122
+ const [filterDataSource, setFilterDataSource] = useState([]);
123
+ const [filterValue, setFilterValue] = useState([]);
108
124
  const [gridColumns, setGridColumns] = useState([]);
109
125
  const limit = ajaxSetting.take || 10;
110
126
  const [search, setSearch] = useState("");
@@ -157,7 +173,9 @@ const DataGrid = (props) => {
157
173
  `The selected item has been deleted.`
158
174
  );
159
175
  } else {
160
- toast.error(String(result.error));
176
+ toast.error(
177
+ <div>{parse(result.error)}</div>
178
+ );
161
179
  }
162
180
  }
163
181
  ),
@@ -195,7 +213,9 @@ const DataGrid = (props) => {
195
213
 
196
214
  toast.success(String(s.title));
197
215
  } else {
198
- toast.error(String(result.error));
216
+ toast.error(
217
+ <div>{parse(result.error)}</div>
218
+ );
199
219
  }
200
220
  }
201
221
  ),
@@ -389,28 +409,41 @@ const DataGrid = (props) => {
389
409
 
390
410
  useEffect(() => {
391
411
  const renderColumn = (column) => {
412
+ let filterEditor = null;
413
+ let filterEditorProps = null;
414
+ let selectedDataSource = null;
415
+ let icons = [];
416
+ let relationName;
417
+ let stage;
418
+ let value;
419
+
420
+ const commonProps = {
421
+ header: column.label,
422
+ defaultFlex: 1,
423
+ link: column.link ? column.link : {},
424
+ minWidth:
425
+ column.minWidth && column.minWidth > 0
426
+ ? column.minWidth
427
+ : undefined,
428
+ maxWidth:
429
+ column.maxWidth && column.maxWidth > 0
430
+ ? column.maxWidth
431
+ : undefined,
432
+ };
433
+
392
434
  switch (column.type) {
393
435
  case "created_by":
394
436
  return {
437
+ ...commonProps,
395
438
  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,
439
+ sortable: false,
407
440
  render: ({ data }) => {
408
441
  if (
409
442
  data.audits[0] &&
410
443
  data.audits[0].user &&
411
444
  data.audits[0].user.name
412
445
  ) {
413
- let value = data.audits[0].user.name;
446
+ value = data.audits[0].user.name;
414
447
 
415
448
  return <span>{value}</span>;
416
449
  } else {
@@ -420,18 +453,8 @@ const DataGrid = (props) => {
420
453
  };
421
454
  case "currency":
422
455
  return {
456
+ ...commonProps,
423
457
  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
458
  render: ({ data }) => {
436
459
  if (data) {
437
460
  data = numeral(data[column.id]).format(
@@ -445,19 +468,24 @@ const DataGrid = (props) => {
445
468
  },
446
469
  };
447
470
  case "date":
471
+ if (column.filter && column.filter.type) {
472
+ filterEditor = DateFilter;
473
+ filterEditorProps = (props, { index }) => {
474
+ return {
475
+ dateFormat: "DD-MM-YYYY",
476
+ placeholder: "All Dates",
477
+ };
478
+ };
479
+ } else {
480
+ filterEditor = null;
481
+ filterEditorProps = null;
482
+ }
483
+
448
484
  return {
485
+ ...commonProps,
449
486
  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 : {},
487
+ filterEditor: filterEditor,
488
+ filterEditorProps: filterEditorProps,
461
489
  render: ({ data }) => {
462
490
  if (data && data[column.id]) {
463
491
  data = moment(data[column.id]).format(
@@ -471,19 +499,24 @@ const DataGrid = (props) => {
471
499
  },
472
500
  };
473
501
  case "datetime":
502
+ if (column.filter && column.filter.type) {
503
+ filterEditor = DateFilter;
504
+ filterEditorProps = (props, { index }) => {
505
+ return {
506
+ dateFormat: "DD-MM-YYYY",
507
+ placeholder: "All Dates",
508
+ };
509
+ };
510
+ } else {
511
+ filterEditor = null;
512
+ filterEditorProps = null;
513
+ }
514
+
474
515
  return {
516
+ ...commonProps,
475
517
  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 : {},
518
+ filterEditor: filterEditor,
519
+ filterEditorProps: filterEditorProps,
487
520
  render: ({ data }) => {
488
521
  if (data && data[column.id]) {
489
522
  data = moment(data[column.id]).format(
@@ -500,18 +533,9 @@ const DataGrid = (props) => {
500
533
  };
501
534
  case "file":
502
535
  return {
536
+ ...commonProps,
503
537
  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 : {},
538
+ sortable: false,
515
539
  render: ({ data }) => {
516
540
  if (data) {
517
541
  return (
@@ -532,20 +556,11 @@ const DataGrid = (props) => {
532
556
  };
533
557
  case "icons":
534
558
  return {
559
+ ...commonProps,
535
560
  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 : {},
561
+ sortable: false,
547
562
  render: ({ data }) => {
548
- let icons = [];
563
+ icons = [];
549
564
 
550
565
  if (column.icons && column.icons.length > 0) {
551
566
  column.icons.forEach((icon) => {
@@ -560,23 +575,17 @@ const DataGrid = (props) => {
560
575
  });
561
576
  }
562
577
 
563
- return <span>{icons}</span>;
578
+ if (data) {
579
+ return <span>{icons}</span>;
580
+ } else {
581
+ return null;
582
+ }
564
583
  },
565
584
  };
566
585
  case "json":
567
586
  return {
587
+ ...commonProps,
568
588
  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
589
  render: ({ data }) => {
581
590
  if (
582
591
  data &&
@@ -593,34 +602,14 @@ const DataGrid = (props) => {
593
602
  };
594
603
  case "number":
595
604
  return {
605
+ ...commonProps,
596
606
  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
607
  type: "number",
609
608
  };
610
609
  case "option":
611
610
  return {
611
+ ...commonProps,
612
612
  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
613
  render: ({ data }) => {
625
614
  if (
626
615
  data &&
@@ -637,44 +626,50 @@ const DataGrid = (props) => {
637
626
  };
638
627
  case "placeholder":
639
628
  return {
629
+ ...commonProps,
640
630
  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 : {},
631
+ sortable: false,
652
632
  render: ({ data }) => {
653
- return <span>{column.placeholder}</span>;
633
+ if (data) {
634
+ return <span>{column.placeholder}</span>;
635
+ } else {
636
+ return null;
637
+ }
654
638
  },
655
639
  };
656
640
  case "relation":
657
- let relationName = column.id.reduce(
641
+ relationName = column.id.reduce(
658
642
  (acc, id) => acc + `${id}.`,
659
643
  ""
660
644
  );
661
645
  relationName += column.nameFrom;
662
646
 
647
+ selectedDataSource = filterDataSource.reduce((acc, fds) => {
648
+ if (fds.id === relationName) {
649
+ return fds.dataset;
650
+ }
651
+ return acc;
652
+ }, null);
653
+
654
+ if (column.filter && column.filter.type) {
655
+ filterEditor = SelectFilter;
656
+ filterEditorProps = {
657
+ placeholder: "All Options",
658
+ dataSource: selectedDataSource,
659
+ };
660
+ } else {
661
+ filterEditor = null;
662
+ filterEditorProps = null;
663
+ }
664
+
663
665
  return {
666
+ ...commonProps,
664
667
  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 : {},
668
+ sortable: false,
669
+ filterEditor: filterEditor,
670
+ filterEditorProps: filterEditorProps,
676
671
  render: ({ data }) => {
677
- let value = column.id.reduce((acc, id) => {
672
+ value = column.id.reduce((acc, id) => {
678
673
  if (acc === "") {
679
674
  return data[id];
680
675
  } else {
@@ -705,23 +700,34 @@ const DataGrid = (props) => {
705
700
  },
706
701
  };
707
702
  case "relationArray":
708
- const relationArrayName =
703
+ relationName =
709
704
  column.id.reduce((acc, id) => acc + `${id}.`, "") +
710
705
  column.nameFrom;
711
706
 
707
+ selectedDataSource = filterDataSource.reduce((acc, fds) => {
708
+ if (fds.id === relationName) {
709
+ return fds.dataset;
710
+ }
711
+ return acc;
712
+ }, null);
713
+
714
+ if (column.filter && column.filter.type) {
715
+ filterEditor = SelectFilter;
716
+ filterEditorProps = {
717
+ placeholder: "All Options",
718
+ dataSource: selectedDataSource,
719
+ };
720
+ } else {
721
+ filterEditor = null;
722
+ filterEditorProps = null;
723
+ }
724
+
712
725
  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 : {},
726
+ ...commonProps,
727
+ name: relationName,
728
+ sortable: false,
729
+ filterEditor: filterEditor,
730
+ filterEditorProps: filterEditorProps,
725
731
  render: ({ data }) => {
726
732
  const relationValue = column.id.reduce(
727
733
  (acc, id) => (acc === "" ? data[id] : acc[id]),
@@ -741,20 +747,11 @@ const DataGrid = (props) => {
741
747
 
742
748
  case "stage":
743
749
  return {
750
+ ...commonProps,
744
751
  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 : {},
752
+ sortable: false,
756
753
  render: ({ data }) => {
757
- let stage = "";
754
+ stage = "";
758
755
 
759
756
  column.keyIds.forEach((a, b) => {
760
757
  if (data[a] === 1) {
@@ -767,18 +764,8 @@ const DataGrid = (props) => {
767
764
  };
768
765
  case "time":
769
766
  return {
767
+ ...commonProps,
770
768
  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
769
  render: ({ data }) => {
783
770
  if (data && data[column.id]) {
784
771
  data = moment(data[column.id]).format(
@@ -793,18 +780,8 @@ const DataGrid = (props) => {
793
780
  };
794
781
  case "url":
795
782
  return {
783
+ ...commonProps,
796
784
  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
785
  render: ({ data }) => {
809
786
  if (data) {
810
787
  return (
@@ -821,18 +798,8 @@ const DataGrid = (props) => {
821
798
  };
822
799
  default:
823
800
  return {
801
+ ...commonProps,
824
802
  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
803
  };
837
804
  }
838
805
  };
@@ -867,12 +834,69 @@ const DataGrid = (props) => {
867
834
  }
868
835
 
869
836
  setGridColumns(newColumns);
870
- }, [columns]);
837
+
838
+ const newFilterValue = columns
839
+ .filter((column) => column.filter && column.filter.type) // Only consider columns with a filter type
840
+ .map((column) => {
841
+ // Map these filtered columns to a new object array
842
+ const filterName = Array.isArray(column.id)
843
+ ? column.id.reduce((acc, id) => acc + `${id}.`, "") +
844
+ column.nameFrom
845
+ : column.id;
846
+
847
+ return {
848
+ key: column.filter.key ? column.filter.key : null,
849
+ name: filterName,
850
+ operator: column.filter.operator,
851
+ type: column.filter.type,
852
+ value: "",
853
+ };
854
+ });
855
+
856
+ setFilterValue(newFilterValue);
857
+ }, [columns, filterDataSource]);
858
+
859
+ useEffect(() => {
860
+ columns.forEach((column) => {
861
+ if (column.filter && column.filter.url) {
862
+ CustomFetch(column.filter.url, "POST", {}, (res) => {
863
+ const filterName = Array.isArray(column.id)
864
+ ? column.id.reduce((acc, id) => acc + `${id}.`, "") +
865
+ column.nameFrom
866
+ : column.id;
867
+
868
+ const filterIndex = filterDataSource.findIndex(
869
+ (filter) => filter.id === filterName
870
+ );
871
+
872
+ if (filterIndex >= 0) {
873
+ const updatedFilter = {
874
+ ...filterDataSource[filterIndex],
875
+ dataset: res.data,
876
+ };
877
+ const updatedDataSource = [
878
+ ...filterDataSource.slice(0, filterIndex),
879
+ updatedFilter,
880
+ ...filterDataSource.slice(filterIndex + 1),
881
+ ];
882
+ setFilterDataSource(updatedDataSource);
883
+ } else {
884
+ const newFilter = { id: filterName, dataset: res.data };
885
+ setFilterDataSource((prevState) => [
886
+ ...prevState,
887
+ newFilter,
888
+ ]);
889
+ }
890
+ });
891
+ }
892
+ });
893
+ }, []);
871
894
 
872
895
  // Data Grid Styling
873
896
  const [windowHeight, setWindowHeight] = useState(window.innerHeight);
874
897
  const gridStyle = {
875
- minHeight: windowHeight * 0.6 > 550 ? windowHeight * 0.6 : 550,
898
+ minHeight: windowHeight * 0.7 > 550 ? windowHeight * 0.7 : 550,
899
+ boxShadow: "none",
876
900
  };
877
901
  const headerProps = {
878
902
  style: style ? style : {},
@@ -898,15 +922,18 @@ const DataGrid = (props) => {
898
922
  <ReactDataGrid
899
923
  columns={gridColumns}
900
924
  dataSource={data}
925
+ filterValue={filterValue}
901
926
  defaultLimit={limit}
902
927
  defaultSortInfo={sortInfo}
903
928
  enableColumnAutosize={false}
929
+ enableColumnFilterContextMenu={false}
904
930
  headerProps={headerProps}
905
931
  idProperty={`visns-datagrid-${ajaxSetting.url.replace(
906
932
  /\//g,
907
933
  "-"
908
934
  )}`}
909
935
  style={gridStyle}
936
+ onFilterValueChange={setFilterValue}
910
937
  onRenderRow={onRenderRow}
911
938
  pagination
912
939
  renderColumnContextMenu={renderColumnContextMenu}
@@ -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.7",
37
38
  "description": "Various packages to assist in the development of our Custom Applications.",
38
39
  "main": "index.js",
39
40
  "devDependencies": {},