datastake-daf 0.6.775 → 0.6.777

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,23 +1,33 @@
1
- import React, { useMemo, useCallback } from 'react';
1
+ import React, { useMemo, useCallback, useState } from 'react';
2
2
  import { Widget, PieChart } from '../../../../../../../../index.js';
3
+ import { Select } from 'antd';
3
4
  import { getHealthAndSafetyDistributionData, isHealthAndSafetyDistributionEmpty, calculateHealthAndSafetyPieData, getHealthAndSafetyTooltipChildren } from './helper';
4
5
  import { renderTooltipJsx } from '../../../../../../../../utils';
5
6
  import { useWidgetFetch } from '../../../../../../../hooks/useWidgetFetch.js';
6
7
 
7
- const HealthAndSafety = ({
8
+ const filterOptions = [
9
+ { value: 'aidKitAccessible', label: 'Aid kit availability' },
10
+ { value: 'hsTrainingConfirmation', label: 'H&S training delivery' },
11
+ { value: 'duosFormed', label: 'Workers safe pairing' },
12
+ { value: 'presenceOfChildren', label: 'No children' },
13
+ ];
14
+
15
+ const HealthAndSafety = ({
8
16
  id,
9
17
  getSummaryDetail,
10
- loading = false,
11
- t = (s) => s
18
+ loading = false,
19
+ t = (s) => s
12
20
  }) => {
21
+ const [selectedField, setSelectedField] = useState('aidKitAccessible');
22
+
13
23
  const defaultConfig = useMemo(
14
24
  () => ({
15
25
  basepath: "planting-cycle",
16
26
  url: `/summary/${id}/filtered-piechart`,
17
- filters: { field: 'duosFormed' },
27
+ filters: { field: selectedField },
18
28
  stop: !id,
19
29
  }),
20
- [id],
30
+ [id, selectedField],
21
31
  );
22
32
 
23
33
  const customGetData = useMemo(() => {
@@ -45,53 +55,75 @@ const HealthAndSafety = ({
45
55
  });
46
56
 
47
57
  // Process the fetched pie chart data
48
- // The API returns data in format: [{count: 1, duosFormed: "null"}, {count: 1, duosFormed: "no"}, {count: 1, duosFormed: "yes"}]
58
+ // The API returns data in format: [{count: 1, [field]: "null"}, {count: 1, [field]: "no"}, {count: 1, [field]: "yes"}]
49
59
  const healthAndSafetyDistributionData = useMemo(() => {
50
60
  if (!pieChartData) return { compliant: 0, notCompliant: 0, empty: 0 };
51
-
61
+
52
62
  // If it's already a distribution object
53
63
  if (pieChartData.compliant !== undefined || pieChartData.notCompliant !== undefined) {
54
64
  return pieChartData;
55
65
  }
56
-
57
- // If it's an array, process it
66
+
67
+ // If it's an array, process it using the selected field
58
68
  if (Array.isArray(pieChartData)) {
59
69
  const distribution = { compliant: 0, notCompliant: 0, empty: 0 };
60
-
70
+
61
71
  pieChartData.forEach(item => {
62
- const duosFormedValue = item.duosFormed;
72
+ const fieldValue = item[selectedField];
63
73
  const count = item.count || 0;
64
-
65
- // Map duosFormed values to distribution categories
66
- if (duosFormedValue === "yes" || duosFormedValue === true) {
74
+
75
+ // Map field values to distribution categories
76
+ if (fieldValue === "yes" || fieldValue === true) {
67
77
  distribution.compliant += count;
68
- } else if (duosFormedValue === "no" || duosFormedValue === false) {
78
+ } else if (fieldValue === "no" || fieldValue === false) {
69
79
  distribution.notCompliant += count;
70
- } else if (duosFormedValue === "null" || duosFormedValue === null || duosFormedValue === undefined) {
80
+ } else if (fieldValue === "null" || fieldValue === null || fieldValue === undefined) {
71
81
  distribution.empty += count;
72
82
  }
73
83
  });
74
-
84
+
75
85
  return distribution;
76
86
  }
77
-
87
+
78
88
  // Fallback: try to extract from activityData-like structure
79
89
  return getHealthAndSafetyDistributionData(pieChartData);
80
- }, [pieChartData]);
90
+ }, [pieChartData, selectedField]);
81
91
 
82
92
  const isEmpty = useMemo(() => isHealthAndSafetyDistributionEmpty(healthAndSafetyDistributionData), [healthAndSafetyDistributionData]);
83
93
  const pieData = useMemo(() => calculateHealthAndSafetyPieData(healthAndSafetyDistributionData, t), [healthAndSafetyDistributionData, t]);
84
94
 
95
+ // Get the label for the selected field to use as tooltip title
96
+ const selectedFieldLabel = useMemo(() => {
97
+ const selectedOption = filterOptions.find(opt => opt.value === selectedField);
98
+ return selectedOption ? selectedOption.label : 'Health and Safety';
99
+ }, [selectedField]);
100
+
85
101
  const getTooltipChildren = useCallback(
86
- (item) => getHealthAndSafetyTooltipChildren(item, isEmpty, healthAndSafetyDistributionData, t, renderTooltipJsx),
87
- [t, isEmpty, healthAndSafetyDistributionData],
102
+ (item) => getHealthAndSafetyTooltipChildren(item, isEmpty, healthAndSafetyDistributionData, t, renderTooltipJsx, selectedFieldLabel),
103
+ [t, isEmpty, healthAndSafetyDistributionData, selectedFieldLabel],
88
104
  );
89
105
 
106
+ const handleFilterChange = useCallback((value) => {
107
+ setSelectedField(value);
108
+ }, []);
109
+
90
110
  return (
91
111
  <Widget
92
112
  loading={loading || pieChartLoading}
93
113
  title={<div>{t("Health and Safety")}</div>}
94
114
  className="with-border-header h-w-btn-header "
115
+ addedHeader={
116
+ <>
117
+ <div className="flex-1" />
118
+ <Select
119
+ value={selectedField}
120
+ style={{ width: 180 }}
121
+ onChange={handleFilterChange}
122
+ options={filterOptions.map(opt => ({ ...opt, label: t(opt.label) }))}
123
+ popupMatchSelectWidth={180}
124
+ />
125
+ </>
126
+ }
95
127
  >
96
128
  <div
97
129
  style={{
@@ -61,6 +61,4 @@ export const removeKeysFromObject = (obj = {}, keys = []) => {
61
61
  }
62
62
  }
63
63
  return result;
64
- }
65
-
66
- export const hasKeyInObject = (obj, key) => Object.keys(obj || {}).includes(key);
64
+ }
package/src/hooks.js CHANGED
@@ -13,5 +13,4 @@ export { useFirebase } from "./@daf/hooks/useFirebase.js"
13
13
  export { useIsDatastake } from "./@daf/hooks/useIsDatastake.js"
14
14
  export { useWidgetFetch } from "./@daf/hooks/useWidgetFetch.js"
15
15
  export { useAdminDashboard } from "./@daf/hooks/useAdminDashboard.js"
16
- export { useGetQueryParams } from "./@daf/hooks/useGetQueryParams.js"
17
- export { useViewFormUrlParams } from "./@daf/hooks/useViewFormUrlParams.js"
16
+ export { useGetQueryParams } from "./@daf/hooks/useGetQueryParams.js"
package/src/utils.js CHANGED
@@ -27,7 +27,7 @@ export { default as locales } from './constants/locales/index.js';
27
27
 
28
28
  export { getTagColor } from "./@daf/utils/productTag.js";
29
29
 
30
- export { convertUndefinedToNull, hasKeyInObject, removeKeysFromObject } from './@daf/utils/object'
30
+ export { convertUndefinedToNull } from './@daf/utils/object'
31
31
 
32
32
  export { default as ErrorFormat, formatErrors } from './helpers/ErrorFormater'
33
33