@visns-studio/visns-components 1.2.4 → 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
  ),
@@ -259,6 +269,45 @@ const DataGrid = (props) => {
259
269
  };
260
270
  }, []);
261
271
 
272
+ const renderCreateButton = () => {
273
+ if (form.createDisable && form.createDisable === true) {
274
+ return null;
275
+ }
276
+
277
+ return (
278
+ <div className="filterAction--alt">
279
+ <button
280
+ className="btn"
281
+ onClick={() => {
282
+ modalOpen("create", 0);
283
+ }}
284
+ >
285
+ <div className="icon-plus"></div> Create
286
+ </button>
287
+ </div>
288
+ );
289
+ };
290
+
291
+ const renderSearch = () => {
292
+ const searchEnable = !(
293
+ form.searchDisable && form.searchDisable === true
294
+ );
295
+
296
+ return searchEnable ? (
297
+ <div className="filterInput--alt">
298
+ <div className="icon-search">
299
+ <Search strokeWidth={2} size={18} />
300
+ </div>
301
+ <input
302
+ type="text"
303
+ placeholder="Search"
304
+ value={search}
305
+ onChange={handleChangeSearch}
306
+ />
307
+ </div>
308
+ ) : null;
309
+ };
310
+
262
311
  /** Table Hooks */
263
312
  useEffect(() => {
264
313
  const delayedSetDSearch = debounce(() => {
@@ -350,28 +399,41 @@ const DataGrid = (props) => {
350
399
 
351
400
  useEffect(() => {
352
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
+
353
424
  switch (column.type) {
354
425
  case "created_by":
355
426
  return {
427
+ ...commonProps,
356
428
  name: "audits.name",
357
- header: column.label,
358
- defaultFlex: 1,
359
- link: column.link ? column.link : {},
360
- minWidth:
361
- column.minWidth && column.minWidth > 0
362
- ? column.minWidth
363
- : undefined,
364
- maxWidth:
365
- column.maxWidth && column.maxWidth > 0
366
- ? column.maxWidth
367
- : undefined,
429
+ sortable: false,
368
430
  render: ({ data }) => {
369
431
  if (
370
432
  data.audits[0] &&
371
433
  data.audits[0].user &&
372
434
  data.audits[0].user.name
373
435
  ) {
374
- let value = data.audits[0].user.name;
436
+ value = data.audits[0].user.name;
375
437
 
376
438
  return <span>{value}</span>;
377
439
  } else {
@@ -381,18 +443,8 @@ const DataGrid = (props) => {
381
443
  };
382
444
  case "currency":
383
445
  return {
446
+ ...commonProps,
384
447
  name: column.id,
385
- header: column.label,
386
- defaultFlex: 1,
387
- minWidth:
388
- column.minWidth && column.minWidth > 0
389
- ? column.minWidth
390
- : undefined,
391
- maxWidth:
392
- column.maxWidth && column.maxWidth > 0
393
- ? column.maxWidth
394
- : undefined,
395
- link: column.link ? column.link : {},
396
448
  render: ({ data }) => {
397
449
  if (data) {
398
450
  data = numeral(data[column.id]).format(
@@ -406,19 +458,24 @@ const DataGrid = (props) => {
406
458
  },
407
459
  };
408
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
+
409
474
  return {
475
+ ...commonProps,
410
476
  name: column.id,
411
- header: column.label,
412
- defaultFlex: 1,
413
- minWidth:
414
- column.minWidth && column.minWidth > 0
415
- ? column.minWidth
416
- : undefined,
417
- maxWidth:
418
- column.maxWidth && column.maxWidth > 0
419
- ? column.maxWidth
420
- : undefined,
421
- link: column.link ? column.link : {},
477
+ filterEditor: filterEditor,
478
+ filterEditorProps: filterEditorProps,
422
479
  render: ({ data }) => {
423
480
  if (data && data[column.id]) {
424
481
  data = moment(data[column.id]).format(
@@ -432,19 +489,24 @@ const DataGrid = (props) => {
432
489
  },
433
490
  };
434
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
+
435
505
  return {
506
+ ...commonProps,
436
507
  name: column.id,
437
- header: column.label,
438
- defaultFlex: 1,
439
- minWidth:
440
- column.minWidth && column.minWidth > 0
441
- ? column.minWidth
442
- : undefined,
443
- maxWidth:
444
- column.maxWidth && column.maxWidth > 0
445
- ? column.maxWidth
446
- : undefined,
447
- link: column.link ? column.link : {},
508
+ filterEditor: filterEditor,
509
+ filterEditorProps: filterEditorProps,
448
510
  render: ({ data }) => {
449
511
  if (data && data[column.id]) {
450
512
  data = moment(data[column.id]).format(
@@ -461,18 +523,9 @@ const DataGrid = (props) => {
461
523
  };
462
524
  case "file":
463
525
  return {
526
+ ...commonProps,
464
527
  name: column.id,
465
- header: column.label,
466
- defaultFlex: 1,
467
- minWidth:
468
- column.minWidth && column.minWidth > 0
469
- ? column.minWidth
470
- : undefined,
471
- maxWidth:
472
- column.maxWidth && column.maxWidth > 0
473
- ? column.maxWidth
474
- : undefined,
475
- link: column.link ? column.link : {},
528
+ sortable: false,
476
529
  render: ({ data }) => {
477
530
  if (data) {
478
531
  return (
@@ -493,20 +546,11 @@ const DataGrid = (props) => {
493
546
  };
494
547
  case "icons":
495
548
  return {
549
+ ...commonProps,
496
550
  name: column.id,
497
- header: column.label,
498
- defaultFlex: 1,
499
- minWidth:
500
- column.minWidth && column.minWidth > 0
501
- ? column.minWidth
502
- : undefined,
503
- maxWidth:
504
- column.maxWidth && column.maxWidth > 0
505
- ? column.maxWidth
506
- : undefined,
507
- link: column.link ? column.link : {},
551
+ sortable: false,
508
552
  render: ({ data }) => {
509
- let icons = [];
553
+ icons = [];
510
554
 
511
555
  if (column.icons && column.icons.length > 0) {
512
556
  column.icons.forEach((icon) => {
@@ -521,23 +565,17 @@ const DataGrid = (props) => {
521
565
  });
522
566
  }
523
567
 
524
- return <span>{icons}</span>;
568
+ if (data) {
569
+ return <span>{icons}</span>;
570
+ } else {
571
+ return null;
572
+ }
525
573
  },
526
574
  };
527
575
  case "json":
528
576
  return {
577
+ ...commonProps,
529
578
  name: column.id,
530
- header: column.label,
531
- defaultFlex: 1,
532
- minWidth:
533
- column.minWidth && column.minWidth > 0
534
- ? column.minWidth
535
- : undefined,
536
- maxWidth:
537
- column.maxWidth && column.maxWidth > 0
538
- ? column.maxWidth
539
- : undefined,
540
- link: column.link ? column.link : {},
541
579
  render: ({ data }) => {
542
580
  if (
543
581
  data &&
@@ -554,34 +592,14 @@ const DataGrid = (props) => {
554
592
  };
555
593
  case "number":
556
594
  return {
595
+ ...commonProps,
557
596
  name: column.id,
558
- header: column.label,
559
- defaultFlex: 1,
560
- minWidth:
561
- column.minWidth && column.minWidth > 0
562
- ? column.minWidth
563
- : undefined,
564
- maxWidth:
565
- column.maxWidth && column.maxWidth > 0
566
- ? column.maxWidth
567
- : undefined,
568
- link: column.link ? column.link : {},
569
597
  type: "number",
570
598
  };
571
599
  case "option":
572
600
  return {
601
+ ...commonProps,
573
602
  name: column.id,
574
- header: column.label,
575
- defaultFlex: 1,
576
- minWidth:
577
- column.minWidth && column.minWidth > 0
578
- ? column.minWidth
579
- : undefined,
580
- maxWidth:
581
- column.maxWidth && column.maxWidth > 0
582
- ? column.maxWidth
583
- : undefined,
584
- link: column.link ? column.link : {},
585
603
  render: ({ data }) => {
586
604
  if (
587
605
  data &&
@@ -598,44 +616,50 @@ const DataGrid = (props) => {
598
616
  };
599
617
  case "placeholder":
600
618
  return {
619
+ ...commonProps,
601
620
  name: column.id,
602
- header: column.label,
603
- defaultFlex: 1,
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
- link: column.link ? column.link : {},
621
+ sortable: false,
613
622
  render: ({ data }) => {
614
- return <span>{column.placeholder}</span>;
623
+ if (data) {
624
+ return <span>{column.placeholder}</span>;
625
+ } else {
626
+ return null;
627
+ }
615
628
  },
616
629
  };
617
630
  case "relation":
618
- let relationName = column.id.reduce(
631
+ relationName = column.id.reduce(
619
632
  (acc, id) => acc + `${id}.`,
620
633
  ""
621
634
  );
622
635
  relationName += column.nameFrom;
623
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
+
624
655
  return {
656
+ ...commonProps,
625
657
  name: relationName,
626
- header: column.label,
627
- defaultFlex: 1,
628
- minWidth:
629
- column.minWidth && column.minWidth > 0
630
- ? column.minWidth
631
- : undefined,
632
- maxWidth:
633
- column.maxWidth && column.maxWidth > 0
634
- ? column.maxWidth
635
- : undefined,
636
- link: column.link ? column.link : {},
658
+ sortable: false,
659
+ filterEditor: filterEditor,
660
+ filterEditorProps: filterEditorProps,
637
661
  render: ({ data }) => {
638
- let value = column.id.reduce((acc, id) => {
662
+ value = column.id.reduce((acc, id) => {
639
663
  if (acc === "") {
640
664
  return data[id];
641
665
  } else {
@@ -666,23 +690,34 @@ const DataGrid = (props) => {
666
690
  },
667
691
  };
668
692
  case "relationArray":
669
- const relationArrayName =
693
+ relationName =
670
694
  column.id.reduce((acc, id) => acc + `${id}.`, "") +
671
695
  column.nameFrom;
672
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
+
673
715
  return {
674
- name: relationArrayName,
675
- header: column.label,
676
- defaultFlex: 1,
677
- minWidth:
678
- column.minWidth && column.minWidth > 0
679
- ? column.minWidth
680
- : undefined,
681
- maxWidth:
682
- column.maxWidth && column.maxWidth > 0
683
- ? column.maxWidth
684
- : undefined,
685
- link: column.link ? column.link : {},
716
+ ...commonProps,
717
+ name: relationName,
718
+ sortable: false,
719
+ filterEditor: filterEditor,
720
+ filterEditorProps: filterEditorProps,
686
721
  render: ({ data }) => {
687
722
  const relationValue = column.id.reduce(
688
723
  (acc, id) => (acc === "" ? data[id] : acc[id]),
@@ -702,20 +737,11 @@ const DataGrid = (props) => {
702
737
 
703
738
  case "stage":
704
739
  return {
740
+ ...commonProps,
705
741
  name: column.id,
706
- header: column.label,
707
- defaultFlex: 1,
708
- minWidth:
709
- column.minWidth && column.minWidth > 0
710
- ? column.minWidth
711
- : undefined,
712
- maxWidth:
713
- column.maxWidth && column.maxWidth > 0
714
- ? column.maxWidth
715
- : undefined,
716
- link: column.link ? column.link : {},
742
+ sortable: false,
717
743
  render: ({ data }) => {
718
- let stage = "";
744
+ stage = "";
719
745
 
720
746
  column.keyIds.forEach((a, b) => {
721
747
  if (data[a] === 1) {
@@ -728,18 +754,8 @@ const DataGrid = (props) => {
728
754
  };
729
755
  case "time":
730
756
  return {
757
+ ...commonProps,
731
758
  name: column.id,
732
- header: column.label,
733
- defaultFlex: 1,
734
- minWidth:
735
- column.minWidth && column.minWidth > 0
736
- ? column.minWidth
737
- : undefined,
738
- maxWidth:
739
- column.maxWidth && column.maxWidth > 0
740
- ? column.maxWidth
741
- : undefined,
742
- link: column.link ? column.link : {},
743
759
  render: ({ data }) => {
744
760
  if (data && data[column.id]) {
745
761
  data = moment(data[column.id]).format(
@@ -754,18 +770,8 @@ const DataGrid = (props) => {
754
770
  };
755
771
  case "url":
756
772
  return {
773
+ ...commonProps,
757
774
  name: column.id,
758
- header: column.label,
759
- defaultFlex: 1,
760
- minWidth:
761
- column.minWidth && column.minWidth > 0
762
- ? column.minWidth
763
- : undefined,
764
- maxWidth:
765
- column.maxWidth && column.maxWidth > 0
766
- ? column.maxWidth
767
- : undefined,
768
- link: column.link ? column.link : {},
769
775
  render: ({ data }) => {
770
776
  if (data) {
771
777
  return (
@@ -782,18 +788,8 @@ const DataGrid = (props) => {
782
788
  };
783
789
  default:
784
790
  return {
791
+ ...commonProps,
785
792
  name: column.id,
786
- header: column.label,
787
- defaultFlex: 1,
788
- minWidth:
789
- column.minWidth && column.minWidth > 0
790
- ? column.minWidth
791
- : undefined,
792
- maxWidth:
793
- column.maxWidth && column.maxWidth > 0
794
- ? column.maxWidth
795
- : undefined,
796
- link: column.link ? column.link : {},
797
793
  };
798
794
  }
799
795
  };
@@ -828,7 +824,62 @@ const DataGrid = (props) => {
828
824
  }
829
825
 
830
826
  setGridColumns(newColumns);
831
- }, [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
+ }, []);
832
883
 
833
884
  // Data Grid Styling
834
885
  const [windowHeight, setWindowHeight] = useState(window.innerHeight);
@@ -854,33 +905,16 @@ const DataGrid = (props) => {
854
905
 
855
906
  return (
856
907
  <>
857
- <div className="filterInput--alt">
858
- <div className="icon-search">
859
- <Search strokeWidth={2} size={18} />
860
- </div>
861
- <input
862
- type="text"
863
- placeholder="Search"
864
- value={search}
865
- onChange={handleChangeSearch}
866
- />
867
- </div>
868
- <div className="filterAction--alt">
869
- <button
870
- className="btn"
871
- onClick={() => {
872
- modalOpen("create", 0);
873
- }}
874
- >
875
- <div className="icon-plus"></div> Create
876
- </button>
877
- </div>
908
+ {renderSearch()}
909
+ {renderCreateButton()}
878
910
  <ReactDataGrid
879
911
  columns={gridColumns}
880
912
  dataSource={data}
913
+ filterValue={filterValue}
881
914
  defaultLimit={limit}
882
915
  defaultSortInfo={sortInfo}
883
916
  enableColumnAutosize={false}
917
+ enableColumnFilterContextMenu={false}
884
918
  headerProps={headerProps}
885
919
  idProperty={`visns-datagrid-${ajaxSetting.url.replace(
886
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.4",
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": {},