datastake-daf 0.6.845 → 0.6.847

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.
@@ -2294,6 +2294,7 @@ var DashboardService$1 = createLazyService(DashboardService);
2294
2294
  // url: string
2295
2295
  // basePath: string,
2296
2296
  // }
2297
+
2297
2298
  const useWidgetFetch = ({
2298
2299
  config,
2299
2300
  getData = DashboardService$1.getWidget,
@@ -2306,15 +2307,12 @@ const useWidgetFetch = ({
2306
2307
  } = config;
2307
2308
  const [data, setData] = React.useState(defaultData || {});
2308
2309
  const [loading, setLoading] = React.useState(false);
2309
- const [initFetchDone, setInitFetchDone] = React.useState(false);
2310
- const isMounted = React.useRef(true);
2311
2310
  const fetchData = async () => {
2312
2311
  setLoading(true);
2313
2312
  try {
2314
2313
  const {
2315
2314
  data
2316
2315
  } = await getData(rest);
2317
- if (!isMounted.current) return;
2318
2316
  setData(data || defaultData);
2319
2317
  if (isErrorResponse(data)) {
2320
2318
  const errorMessage = getErrorMessage(data);
@@ -2322,20 +2320,11 @@ const useWidgetFetch = ({
2322
2320
  return;
2323
2321
  }
2324
2322
  onFetch();
2325
- setInitFetchDone(true);
2326
2323
  } catch (err) {
2327
2324
  console.log(err);
2328
2325
  }
2329
- if (isMounted.current) {
2330
- setLoading(false);
2331
- }
2326
+ setLoading(false);
2332
2327
  };
2333
- React.useEffect(() => {
2334
- isMounted.current = true;
2335
- return () => {
2336
- isMounted.current = false;
2337
- };
2338
- }, []);
2339
2328
  React.useEffect(() => {
2340
2329
  if (!stop) {
2341
2330
  fetchData();
@@ -2344,8 +2333,7 @@ const useWidgetFetch = ({
2344
2333
  return {
2345
2334
  data,
2346
2335
  loading,
2347
- setData,
2348
- initFetchDone
2336
+ setData
2349
2337
  };
2350
2338
  };
2351
2339
 
@@ -7500,6 +7500,7 @@ var DashboardService$1 = createLazyService(DashboardService);
7500
7500
  // url: string
7501
7501
  // basePath: string,
7502
7502
  // }
7503
+
7503
7504
  const useWidgetFetch = ({
7504
7505
  config,
7505
7506
  getData = DashboardService$1.getWidget,
@@ -7512,15 +7513,12 @@ const useWidgetFetch = ({
7512
7513
  } = config;
7513
7514
  const [data, setData] = React.useState(defaultData || {});
7514
7515
  const [loading, setLoading] = React.useState(false);
7515
- const [initFetchDone, setInitFetchDone] = React.useState(false);
7516
- const isMounted = React.useRef(true);
7517
7516
  const fetchData = async () => {
7518
7517
  setLoading(true);
7519
7518
  try {
7520
7519
  const {
7521
7520
  data
7522
7521
  } = await getData(rest);
7523
- if (!isMounted.current) return;
7524
7522
  setData(data || defaultData);
7525
7523
  if (isErrorResponse(data)) {
7526
7524
  const errorMessage = getErrorMessage(data);
@@ -7528,20 +7526,11 @@ const useWidgetFetch = ({
7528
7526
  return;
7529
7527
  }
7530
7528
  onFetch();
7531
- setInitFetchDone(true);
7532
7529
  } catch (err) {
7533
7530
  console.log(err);
7534
7531
  }
7535
- if (isMounted.current) {
7536
- setLoading(false);
7537
- }
7532
+ setLoading(false);
7538
7533
  };
7539
- React.useEffect(() => {
7540
- isMounted.current = true;
7541
- return () => {
7542
- isMounted.current = false;
7543
- };
7544
- }, []);
7545
7534
  React.useEffect(() => {
7546
7535
  if (!stop) {
7547
7536
  fetchData();
@@ -7550,8 +7539,7 @@ const useWidgetFetch = ({
7550
7539
  return {
7551
7540
  data,
7552
7541
  loading,
7553
- setData,
7554
- initFetchDone
7542
+ setData
7555
7543
  };
7556
7544
  };
7557
7545
 
@@ -55752,13 +55740,6 @@ const PlantingLocations = ({
55752
55740
  });
55753
55741
  };
55754
55742
 
55755
- /**
55756
- * Formats a date based on the time filter
55757
- * @param {dayjs.Dayjs} date - The date to format
55758
- * @param {boolean} breakLine - Whether to add a line break (for tooltips)
55759
- * @param {string} timeFilter - The time filter ('daily', 'weekly', 'monthly', 'yearly')
55760
- * @returns {string} Formatted date string
55761
- */
55762
55743
  const getFormatDate = (date, breakLine = false, timeFilter = 'monthly') => {
55763
55744
  switch (timeFilter) {
55764
55745
  case "daily":
@@ -55772,12 +55753,6 @@ const getFormatDate = (date, breakLine = false, timeFilter = 'monthly') => {
55772
55753
  return breakLine ? `${capitalize(date.format("MMM"))}\n${date.format("YY")}` : `${capitalize(date.format("MMM"))} ${date.format("YY")}`;
55773
55754
  }
55774
55755
  };
55775
-
55776
- /**
55777
- * Gets the time quantity string for dayjs operations
55778
- * @param {string} timeFilter - The time filter ('daily', 'weekly', 'monthly', 'yearly')
55779
- * @returns {string} Time quantity string ('days', 'weeks', 'months', 'years')
55780
- */
55781
55756
  const getTimeQuantity = (timeFilter = 'monthly') => {
55782
55757
  switch (timeFilter) {
55783
55758
  case "daily":
@@ -55790,15 +55765,6 @@ const getTimeQuantity = (timeFilter = 'monthly') => {
55790
55765
  return "months";
55791
55766
  }
55792
55767
  };
55793
-
55794
- /**
55795
- * Gets previous cumulative score from data before start date
55796
- * @param {Array} dates - Array of data objects with date field
55797
- * @param {dayjs.Dayjs} startDate - The start date
55798
- * @param {string} timeFilter - The time filter
55799
- * @param {string} valueField - The field name to extract value from (default: 'total')
55800
- * @returns {Object} Object with hasPreviousData, previousCumulativeScore, previousMaxScore
55801
- */
55802
55768
  const getPreviousGraphData = (dates, startDate, timeFilter, valueField = 'total') => {
55803
55769
  let previousCumulativeScore = 0;
55804
55770
  let previousMaxScore = 0;
@@ -55834,17 +55800,6 @@ const getPreviousGraphData = (dates, startDate, timeFilter, valueField = 'total'
55834
55800
  previousMaxScore
55835
55801
  };
55836
55802
  };
55837
-
55838
- /**
55839
- * Processes chart data with time filtering support
55840
- * @param {Object} params - Parameters object
55841
- * @param {Array} params.mainData - Array of data objects with date and value fields
55842
- * @param {string} params.timeFilter - Time filter ('daily', 'weekly', 'monthly', 'yearly')
55843
- * @param {Object} params.filters - Optional filters object with timeframe
55844
- * @param {boolean} params.isCumulative - Whether to calculate cumulative values (default: false)
55845
- * @param {string} params.valueField - Field name to extract value from (default: 'total', also checks 'count', 'jobs', 'value')
55846
- * @returns {Array} Processed chart data array
55847
- */
55848
55803
  const processChartDateData = ({
55849
55804
  mainData,
55850
55805
  timeFilter: filter,
@@ -55856,8 +55811,35 @@ const processChartDateData = ({
55856
55811
  const dates = mainData || [];
55857
55812
  const isEmpty = !mainData || !Array.isArray(mainData) || mainData.length === 0;
55858
55813
  const _data = [];
55859
- let end = filters?.timeframe?.endDate || dayjs__default["default"]();
55860
- let start = filters?.timeframe?.startDate || dayjs__default["default"]().add(-12, timeQuantity);
55814
+
55815
+ // Derive date range from actual data if no filters provided and data exists
55816
+ let end, start;
55817
+ if (filters?.timeframe?.endDate) {
55818
+ end = filters.timeframe.endDate;
55819
+ } else if (!isEmpty) {
55820
+ // Find the latest date in the data
55821
+ const sortedDates = dates.map(d => dayjs__default["default"](d.date, "YYYY-MM-DD")).filter(d => d.isValid()).sort((a, b) => b.valueOf() - a.valueOf());
55822
+ end = sortedDates.length > 0 ? sortedDates[0] : dayjs__default["default"]();
55823
+ } else {
55824
+ end = dayjs__default["default"]();
55825
+ }
55826
+ if (filters?.timeframe?.startDate) {
55827
+ start = filters.timeframe.startDate;
55828
+ } else if (!isEmpty) {
55829
+ // Find the earliest date in the data
55830
+ const sortedDates = dates.map(d => dayjs__default["default"](d.date, "YYYY-MM-DD")).filter(d => d.isValid()).sort((a, b) => a.valueOf() - b.valueOf());
55831
+ start = sortedDates.length > 0 ? sortedDates[0] : dayjs__default["default"]().add(-12, timeQuantity);
55832
+
55833
+ // For daily filter, limit to last 14 days to avoid overcrowded x-axis
55834
+ if (filter === "daily" && end) {
55835
+ const maxDailyStart = end.subtract(13, 'days'); // 14 days total including end date
55836
+ if (start.isBefore(maxDailyStart)) {
55837
+ start = maxDailyStart;
55838
+ }
55839
+ }
55840
+ } else {
55841
+ start = dayjs__default["default"]().add(-12, timeQuantity);
55842
+ }
55861
55843
 
55862
55844
  // Normalize start and end to period boundaries
55863
55845
  if (filter === "daily") {
@@ -55873,6 +55855,30 @@ const processChartDateData = ({
55873
55855
  start = start.startOf('month');
55874
55856
  end = end.startOf('month');
55875
55857
  }
55858
+
55859
+ // Ensure minimum number of periods to fill the x-axis properly
55860
+ const getMinPeriods = f => {
55861
+ switch (f) {
55862
+ case "daily":
55863
+ return 14;
55864
+ // At least 14 days
55865
+ case "weekly":
55866
+ return 8;
55867
+ // At least 8 weeks
55868
+ case "yearly":
55869
+ return 5;
55870
+ // At least 5 years
55871
+ default:
55872
+ return 12;
55873
+ // At least 12 months
55874
+ }
55875
+ };
55876
+ const minPeriods = getMinPeriods(filter);
55877
+ const periodsDiff = end.diff(start, timeQuantity);
55878
+ if (periodsDiff < minPeriods - 1) {
55879
+ const periodsToAdd = minPeriods - 1 - periodsDiff;
55880
+ start = start.subtract(periodsToAdd, timeQuantity);
55881
+ }
55876
55882
  let i = 0;
55877
55883
  let cumulativeScore = 0;
55878
55884
 
@@ -55939,13 +55945,6 @@ const processChartDateData = ({
55939
55945
  }
55940
55946
  return _data;
55941
55947
  };
55942
-
55943
- /**
55944
- * Formats date axis labels, checking if already formatted
55945
- * @param {string} label - The label to format
55946
- * @param {Function} getFormatDateFn - Function to format dates
55947
- * @returns {string} Formatted date string
55948
- */
55949
55948
  const formatDateAxis = (label, getFormatDateFn) => {
55950
55949
  if (!label) return label;
55951
55950
 
@@ -0,0 +1,330 @@
1
+ /* Isolated Mapbox GL CSS - Scoped to prevent Leaflet conflicts */
2
+
3
+ /* Mapbox GL Core Styles - Scoped with .mapbox-gl-scope */
4
+ .mapbox-gl-scope .mapboxgl-map {
5
+ font: 12px/20px Helvetica Neue, Arial, Helvetica, sans-serif;
6
+ overflow: hidden;
7
+ position: relative;
8
+ -webkit-tap-highlight-color: rgb(0 0 0/0);
9
+ }
10
+
11
+ .mapbox-gl-scope .mapboxgl-canvas {
12
+ left: 0;
13
+ position: absolute;
14
+ top: 0;
15
+ }
16
+
17
+ .mapbox-gl-scope .mapboxgl-map:-webkit-full-screen {
18
+ height: 100%;
19
+ width: 100%;
20
+ }
21
+
22
+ .mapbox-gl-scope .mapboxgl-canary {
23
+ background-color: salmon;
24
+ }
25
+
26
+ .mapbox-gl-scope .mapboxgl-canvas-container.mapboxgl-interactive,
27
+ .mapbox-gl-scope .mapboxgl-ctrl-group button.mapboxgl-ctrl-compass {
28
+ cursor: grab;
29
+ -webkit-user-select: none;
30
+ user-select: none;
31
+ }
32
+
33
+ .mapbox-gl-scope .mapboxgl-canvas-container.mapboxgl-interactive.mapboxgl-track-pointer {
34
+ cursor: pointer;
35
+ }
36
+
37
+ .mapbox-gl-scope .mapboxgl-canvas-container.mapboxgl-interactive:active,
38
+ .mapbox-gl-scope .mapboxgl-ctrl-group button.mapboxgl-ctrl-compass:active {
39
+ cursor: grabbing;
40
+ }
41
+
42
+ .mapbox-gl-scope .mapboxgl-canvas-container.mapboxgl-touch-zoom-rotate,
43
+ .mapbox-gl-scope .mapboxgl-canvas-container.mapboxgl-touch-zoom-rotate .mapboxgl-canvas {
44
+ touch-action: pan-x pan-y;
45
+ }
46
+
47
+ .mapbox-gl-scope .mapboxgl-canvas-container.mapboxgl-touch-drag-pan,
48
+ .mapbox-gl-scope .mapboxgl-canvas-container.mapboxgl-touch-drag-pan .mapboxgl-canvas {
49
+ touch-action: pinch-zoom;
50
+ }
51
+
52
+ .mapbox-gl-scope .mapboxgl-canvas-container.mapboxgl-touch-zoom-rotate.mapboxgl-touch-drag-pan,
53
+ .mapbox-gl-scope .mapboxgl-canvas-container.mapboxgl-touch-zoom-rotate.mapboxgl-touch-drag-pan .mapboxgl-canvas {
54
+ touch-action: none;
55
+ }
56
+
57
+ /* Control positioning */
58
+ .mapbox-gl-scope .mapboxgl-ctrl-bottom,
59
+ .mapbox-gl-scope .mapboxgl-ctrl-bottom-left,
60
+ .mapbox-gl-scope .mapboxgl-ctrl-bottom-right,
61
+ .mapbox-gl-scope .mapboxgl-ctrl-left,
62
+ .mapbox-gl-scope .mapboxgl-ctrl-right,
63
+ .mapbox-gl-scope .mapboxgl-ctrl-top,
64
+ .mapbox-gl-scope .mapboxgl-ctrl-top-left,
65
+ .mapbox-gl-scope .mapboxgl-ctrl-top-right {
66
+ pointer-events: none;
67
+ position: absolute;
68
+ z-index: 2;
69
+ }
70
+
71
+ .mapbox-gl-scope .mapboxgl-ctrl-top-left {
72
+ left: 0;
73
+ top: 0;
74
+ }
75
+
76
+ .mapbox-gl-scope .mapboxgl-ctrl-top {
77
+ left: 50%;
78
+ top: 0;
79
+ transform: translateX(-50%);
80
+ }
81
+
82
+ .mapbox-gl-scope .mapboxgl-ctrl-top-right {
83
+ right: 0;
84
+ top: 0;
85
+ }
86
+
87
+ .mapbox-gl-scope .mapboxgl-ctrl-right {
88
+ right: 0;
89
+ top: 50%;
90
+ transform: translateY(-50%);
91
+ }
92
+
93
+ .mapbox-gl-scope .mapboxgl-ctrl-bottom-right {
94
+ bottom: 0;
95
+ right: 0;
96
+ }
97
+
98
+ .mapbox-gl-scope .mapboxgl-ctrl-bottom {
99
+ bottom: 0;
100
+ left: 50%;
101
+ transform: translateX(-50%);
102
+ }
103
+
104
+ .mapbox-gl-scope .mapboxgl-ctrl-bottom-left {
105
+ bottom: 0;
106
+ left: 0;
107
+ }
108
+
109
+ .mapbox-gl-scope .mapboxgl-ctrl-left {
110
+ left: 0;
111
+ top: 50%;
112
+ transform: translateY(-50%);
113
+ }
114
+
115
+ .mapbox-gl-scope .mapboxgl-ctrl {
116
+ clear: both;
117
+ pointer-events: auto;
118
+ transform: translate(0);
119
+ }
120
+
121
+ .mapbox-gl-scope .mapboxgl-ctrl-top-left .mapboxgl-ctrl {
122
+ float: left;
123
+ margin: 10px 0 0 10px;
124
+ }
125
+
126
+ .mapbox-gl-scope .mapboxgl-ctrl-top .mapboxgl-ctrl {
127
+ float: left;
128
+ margin: 10px 0;
129
+ }
130
+
131
+ .mapbox-gl-scope .mapboxgl-ctrl-top-right .mapboxgl-ctrl {
132
+ float: right;
133
+ margin: 10px 10px 0 0;
134
+ }
135
+
136
+ .mapbox-gl-scope .mapboxgl-ctrl-bottom-right .mapboxgl-ctrl,
137
+ .mapbox-gl-scope .mapboxgl-ctrl-right .mapboxgl-ctrl {
138
+ float: right;
139
+ margin: 0 10px 10px 0;
140
+ }
141
+
142
+ .mapbox-gl-scope .mapboxgl-ctrl-bottom .mapboxgl-ctrl {
143
+ float: left;
144
+ margin: 10px 0;
145
+ }
146
+
147
+ .mapbox-gl-scope .mapboxgl-ctrl-bottom-left .mapboxgl-ctrl,
148
+ .mapbox-gl-scope .mapboxgl-ctrl-left .mapboxgl-ctrl {
149
+ float: left;
150
+ margin: 0 0 10px 10px;
151
+ }
152
+
153
+ /* Control group styling */
154
+ .mapbox-gl-scope .mapboxgl-ctrl-group {
155
+ background: #fff;
156
+ border-radius: 4px;
157
+ }
158
+
159
+ .mapbox-gl-scope .mapboxgl-ctrl-group:not(:empty) {
160
+ box-shadow: 0 0 0 2px #0000001a;
161
+ }
162
+
163
+ .mapbox-gl-scope .mapboxgl-ctrl-group button {
164
+ background-color: initial;
165
+ border: 0;
166
+ box-sizing: border-box;
167
+ cursor: pointer;
168
+ display: block;
169
+ height: 29px;
170
+ outline: none;
171
+ overflow: hidden;
172
+ padding: 0;
173
+ width: 29px;
174
+ }
175
+
176
+ .mapbox-gl-scope .mapboxgl-ctrl-group button+button {
177
+ border-top: 1px solid #ddd;
178
+ }
179
+
180
+ .mapbox-gl-scope .mapboxgl-ctrl button .mapboxgl-ctrl-icon {
181
+ background-position: 50%;
182
+ background-repeat: no-repeat;
183
+ display: block;
184
+ height: 100%;
185
+ width: 100%;
186
+ }
187
+
188
+ .mapbox-gl-scope .mapboxgl-ctrl-attrib-button:focus,
189
+ .mapbox-gl-scope .mapboxgl-ctrl-group button:focus {
190
+ box-shadow: 0 0 2px 2px #0096ff;
191
+ }
192
+
193
+ .mapbox-gl-scope .mapboxgl-ctrl button:disabled {
194
+ cursor: not-allowed;
195
+ }
196
+
197
+ .mapbox-gl-scope .mapboxgl-ctrl button:disabled .mapboxgl-ctrl-icon {
198
+ opacity: .25;
199
+ }
200
+
201
+ .mapbox-gl-scope .mapboxgl-ctrl-group button:first-child {
202
+ border-radius: 4px 4px 0 0;
203
+ }
204
+
205
+ .mapbox-gl-scope .mapboxgl-ctrl-group button:last-child {
206
+ border-radius: 0 0 4px 4px;
207
+ }
208
+
209
+ .mapbox-gl-scope .mapboxgl-ctrl-group button:only-child {
210
+ border-radius: inherit;
211
+ }
212
+
213
+ .mapbox-gl-scope .mapboxgl-ctrl button:not(:disabled):hover {
214
+ background-color: #0000000d;
215
+ }
216
+
217
+ /* Marker styles */
218
+ .mapbox-gl-scope .mapboxgl-marker {
219
+ position: absolute;
220
+ z-index: 1;
221
+ }
222
+
223
+ .mapbox-gl-scope .mapboxgl-marker svg {
224
+ display: block;
225
+ }
226
+
227
+ /* Popup styles */
228
+ .mapbox-gl-scope .mapboxgl-popup {
229
+ position: absolute;
230
+ text-align: center;
231
+ margin-bottom: 20px;
232
+ }
233
+
234
+ .mapbox-gl-scope .mapboxgl-popup-content-wrapper {
235
+ padding: 1px;
236
+ text-align: left;
237
+ border-radius: 12px;
238
+ }
239
+
240
+ .mapbox-gl-scope .mapboxgl-popup-content {
241
+ margin: 13px 24px 13px 20px;
242
+ line-height: 1.3;
243
+ font-size: 13px;
244
+ min-height: 1px;
245
+ }
246
+
247
+ .mapbox-gl-scope .mapboxgl-popup-content p {
248
+ margin: 17px 0;
249
+ }
250
+
251
+ .mapbox-gl-scope .mapboxgl-popup-tip-container {
252
+ width: 40px;
253
+ height: 20px;
254
+ position: absolute;
255
+ left: 50%;
256
+ margin-top: -1px;
257
+ margin-left: -20px;
258
+ overflow: hidden;
259
+ pointer-events: none;
260
+ }
261
+
262
+ .mapbox-gl-scope .mapboxgl-popup-tip {
263
+ width: 17px;
264
+ height: 17px;
265
+ padding: 1px;
266
+ margin: -10px auto 0;
267
+ pointer-events: auto;
268
+ -webkit-transform: rotate(45deg);
269
+ -moz-transform: rotate(45deg);
270
+ -ms-transform: rotate(45deg);
271
+ transform: rotate(45deg);
272
+ }
273
+
274
+ .mapbox-gl-scope .mapboxgl-popup-content-wrapper,
275
+ .mapbox-gl-scope .mapboxgl-popup-tip {
276
+ background: white;
277
+ color: #333;
278
+ box-shadow: 0 3px 14px rgba(0, 0, 0, 0.4);
279
+ }
280
+
281
+ .mapbox-gl-scope .mapboxgl-popup-close-button {
282
+ position: absolute;
283
+ top: 0;
284
+ right: 0;
285
+ border: none;
286
+ text-align: center;
287
+ width: 24px;
288
+ height: 24px;
289
+ font: 16px/24px Tahoma, Verdana, sans-serif;
290
+ color: #757575;
291
+ text-decoration: none;
292
+ background: transparent;
293
+ }
294
+
295
+ .mapbox-gl-scope .mapboxgl-popup-close-button:hover,
296
+ .mapbox-gl-scope .mapboxgl-popup-close-button:focus {
297
+ color: #585858;
298
+ }
299
+
300
+ /* Attribution */
301
+ .mapbox-gl-scope .mapboxgl-ctrl-attribution {
302
+ background: #fff;
303
+ background: rgba(255, 255, 255, 0.8);
304
+ margin: 0;
305
+ }
306
+
307
+ .mapbox-gl-scope .mapboxgl-ctrl-attribution,
308
+ .mapbox-gl-scope .mapboxgl-ctrl-scale-line {
309
+ padding: 0 5px;
310
+ color: #333;
311
+ line-height: 1.4;
312
+ }
313
+
314
+ .mapbox-gl-scope .mapboxgl-ctrl-attribution a {
315
+ text-decoration: none;
316
+ }
317
+
318
+ .mapbox-gl-scope .mapboxgl-ctrl-attribution a:hover,
319
+ .mapbox-gl-scope .mapboxgl-ctrl-attribution a:focus {
320
+ text-decoration: underline;
321
+ }
322
+
323
+ /* Hide attribution by default */
324
+ .mapbox-gl-scope .mapboxgl-ctrl-attribution {
325
+ display: none !important;
326
+ }
327
+
328
+ .mapbox-gl-scope .mapboxgl-ctrl-logo {
329
+ display: none !important;
330
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datastake-daf",
3
- "version": "0.6.845",
3
+ "version": "0.6.847",
4
4
  "dependencies": {
5
5
  "@ant-design/icons": "^5.2.5",
6
6
  "@antv/g2": "^5.1.1",
@@ -2,6 +2,7 @@ import { useState, useEffect, useRef } from "react";
2
2
  import DashboardService from "../services/DashboardService.js";
3
3
  import { isErrorResponse, getErrorMessage } from "../../helpers/errorHandling.js";
4
4
  import { message } from "antd";
5
+
5
6
  // config: {
6
7
  // stop: boolean,
7
8
  // defaultData: object,
@@ -9,48 +10,42 @@ import { message } from "antd";
9
10
  // url: string
10
11
  // basePath: string,
11
12
  // }
12
- export const useWidgetFetch = ({config, getData = DashboardService.getWidget, onFetch = () => {}}) => {
13
- const { stop, defaultData, ...rest} = config;
14
- const [ data, setData ] = useState(defaultData || {});
15
- const [ loading, setLoading ] = useState(false);
16
- const [ initFetchDone, setInitFetchDone ] = useState(false);
17
- const isMounted = useRef(true);
18
-
19
- const fetchData = async () => {
20
- setLoading(true);
21
- try {
22
- const { data } = await getData(rest);
23
- if (!isMounted.current) return;
24
- setData(data || defaultData);
25
- if (isErrorResponse(data)) {
26
- const errorMessage = getErrorMessage(data);
27
- message.error(errorMessage);
28
- return;
29
- }
30
- onFetch();
31
- setInitFetchDone(true);
32
- } catch (err) {
33
- console.log(err);
34
- }
35
- if (isMounted.current) {
36
- setLoading(false);
37
- }
38
- };
39
- useEffect(() => {
40
- isMounted.current = true;
41
- return () => {
42
- isMounted.current = false;
43
- };
44
- }, []);
45
- useEffect(() => {
46
- if(!stop) {
47
- fetchData();
48
- }
49
- }, [JSON.stringify(config), stop]);
50
- return {
51
- data,
52
- loading,
53
- setData,
54
- initFetchDone
55
- }
56
- }
13
+
14
+ export const useWidgetFetch = ({ config, getData = DashboardService.getWidget, onFetch = () => { } }) => {
15
+ const { stop, defaultData, ...rest } = config;
16
+ const [data, setData] = useState(defaultData || {});
17
+ const [loading, setLoading] = useState(false);
18
+
19
+ const fetchData = async () => {
20
+
21
+ setLoading(true);
22
+
23
+ try {
24
+ const { data } = await getData(rest);
25
+ setData(data || defaultData);
26
+ if (isErrorResponse(data)) {
27
+ const errorMessage = getErrorMessage(data);
28
+ message.error(errorMessage);
29
+ return;
30
+ }
31
+ onFetch();
32
+ } catch (err) {
33
+ console.log(err);
34
+ }
35
+
36
+ setLoading(false);
37
+ };
38
+
39
+
40
+ useEffect(() => {
41
+ if (!stop) {
42
+ fetchData();
43
+ }
44
+ }, [JSON.stringify(config), stop]);
45
+
46
+ return {
47
+ data,
48
+ loading,
49
+ setData,
50
+ }
51
+ }