datastake-daf 0.6.796 → 0.6.798

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.
@@ -5762,7 +5762,7 @@ function filterOptions(options, filters, formsValue = {}, repeatValues = {}, fil
5762
5762
  * @param {Array} arr - The array to check.
5763
5763
  * @returns {boolean} - True if the array contains only objects, false otherwise.
5764
5764
  */
5765
- const isArrayOfObjects = arr => Array.isArray(arr) && arr.map(i => typeof i === "object").every(i => i);
5765
+ const isArrayOfObjects$1 = arr => Array.isArray(arr) && arr.map(i => typeof i === "object").every(i => i);
5766
5766
 
5767
5767
  /**
5768
5768
  * Determines if two sets of data have not changed. Used to show Save button disabled or not.
@@ -5793,7 +5793,7 @@ const hasNotChanged = (old, newData) => {
5793
5793
  return old[key] === newData[key].format("YYYY-MM-DD");
5794
5794
  }
5795
5795
  return old[key] === moment__default["default"](newData[key]._d).format("YYYY-MM-DD");
5796
- } else if (isArrayOfObjects(newData[key])) {
5796
+ } else if (isArrayOfObjects$1(newData[key])) {
5797
5797
  return newData[key].length === (old[key] || []).length && newData[key].map((oKey, i) => hasNotChanged((old[key] || [])[i] || {}, oKey || {})).every(i => i);
5798
5798
  }
5799
5799
  } catch (e) {
@@ -6052,6 +6052,54 @@ const sortForm = ({
6052
6052
  return _form;
6053
6053
  };
6054
6054
 
6055
+ // REQUIRED FOR EDIT FORM SUBMIT(SAVE)
6056
+ const extractFiles = (fileList = []) => {
6057
+ const processedFiles = fileList.map(file => file.response || file).filter(file => file?.status === "done");
6058
+
6059
+ // Remove duplicates
6060
+ return Array.isArray(processedFiles) ? [...new Set(processedFiles)] : processedFiles;
6061
+ };
6062
+ const hasFileList = value => {
6063
+ return value && typeof value === "object" && value !== null && "fileList" in value;
6064
+ };
6065
+ const hasContent = obj => {
6066
+ if (typeof obj !== "object" || obj === null) return true;
6067
+ return Object.keys(obj).some(key => propHasValue(obj[key]));
6068
+ };
6069
+ const transformPayload = payload => {
6070
+ if (!payload || typeof payload !== "object") return payload;
6071
+ const transformed = {
6072
+ ...payload
6073
+ };
6074
+ Object.keys(transformed).forEach(key => {
6075
+ const value = transformed[key];
6076
+
6077
+ // Skip null or non-object values
6078
+ if (!value || typeof value !== "object" || !propHasValue(value)) return;
6079
+
6080
+ // Handle arrays - filter out empty objects
6081
+ if (Array.isArray(value)) {
6082
+ transformed[key] = value.filter(hasContent);
6083
+ return;
6084
+ }
6085
+
6086
+ // Handle objects with fileLists
6087
+ if (hasFileList(value)) {
6088
+ transformed[key] = extractFiles(value.fileList);
6089
+ return;
6090
+ }
6091
+
6092
+ // Handle nested objects that might have fileLists
6093
+ Object.keys(value).forEach(nestedKey => {
6094
+ const nestedValue = value[nestedKey];
6095
+ if (hasFileList(nestedValue)) {
6096
+ transformed[key][nestedKey] = extractFiles(nestedValue.fileList);
6097
+ }
6098
+ });
6099
+ });
6100
+ return transformed;
6101
+ };
6102
+
6055
6103
  ({
6056
6104
  background: PropTypes__default["default"].string,
6057
6105
  style: PropTypes__default["default"].any
@@ -13114,7 +13162,7 @@ const formatErrors = e => {
13114
13162
  */
13115
13163
  const renderNumber = (val, doubleDigit = false) => {
13116
13164
  if (typeof val !== 'number') {
13117
- return '--';
13165
+ return '-';
13118
13166
  }
13119
13167
  const _string = Number(val).toLocaleString('en-us');
13120
13168
  if (doubleDigit) {
@@ -14958,6 +15006,8 @@ const copyToClipboard = id => {
14958
15006
  }
14959
15007
  };
14960
15008
 
15009
+ const isArrayOfObjects = arr => Array.isArray(arr) && arr.map(i => typeof i === "object").every(i => i);
15010
+
14961
15011
  exports.ErrorFormat = ErrorFormat;
14962
15012
  exports.MessageTypes = MessageTypes;
14963
15013
  exports.StorageManager = StorageManager;
@@ -14988,6 +15038,7 @@ exports.defaultMapConfig = defaultMapConfig;
14988
15038
  exports.defaultTheme = mmtTheme;
14989
15039
  exports.displayMessage = displayMessage;
14990
15040
  exports.editErrorHandler = editErrorHandler;
15041
+ exports.extractFiles = extractFiles;
14991
15042
  exports.fetchImageAsBuffer = fetchImageAsBuffer;
14992
15043
  exports.filterCreateData = filterCreateData;
14993
15044
  exports.filterOptions = filterOptions;
@@ -15016,8 +15067,11 @@ exports.handleError = handleError;
15016
15067
  exports.handleInfo = handleInfo;
15017
15068
  exports.handleSuccess = handleSuccess;
15018
15069
  exports.handleWarning = handleWarning;
15070
+ exports.hasContent = hasContent;
15071
+ exports.hasFileList = hasFileList;
15019
15072
  exports.hasKeyInObject = hasKeyInObject;
15020
15073
  exports.hasNotChanged = hasNotChanged;
15074
+ exports.isArrayOfObjects = isArrayOfObjects;
15021
15075
  exports.isEmptyOrSpaces = isEmptyOrSpaces;
15022
15076
  exports.isModuleApproved = isModuleApproved;
15023
15077
  exports.isProxy = isProxy;
@@ -15050,6 +15104,7 @@ exports.splitStringInMultipleLines = splitStringInMultipleLines;
15050
15104
  exports.tazamaTheme = tazamaTheme;
15051
15105
  exports.tifTheme = tifTheme;
15052
15106
  exports.titleToCamelCase = titleToCamelCase;
15107
+ exports.transformPayload = transformPayload;
15053
15108
  exports.truncateString = truncateString;
15054
15109
  exports.userHasInterface = userHasInterface;
15055
15110
  exports.userIsAdmin = userIsAdmin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datastake-daf",
3
- "version": "0.6.796",
3
+ "version": "0.6.798",
4
4
  "dependencies": {
5
5
  "@ant-design/icons": "^5.2.5",
6
6
  "@antv/g2": "^5.1.1",
@@ -1,6 +1,6 @@
1
1
  import { Menu } from 'antd';
2
2
  import PropTypes from 'prop-types'
3
- import React, { useState, useEffect } from 'react';
3
+ import React, { useState, useEffect, useMemo } from 'react';
4
4
  import { formPaths } from '../../../../helpers/Forms';
5
5
  import { editErrorHandler, getInputLabel, showHideInput } from './helper';
6
6
 
@@ -92,9 +92,19 @@ export const Sections = ({
92
92
  const [formGroups, setFormGroups] = useState([]);
93
93
  const [selectedGroup, setSelectedGroup] = useState(null);
94
94
 
95
+ const formConfigToUse = useMemo(() => {
96
+ if(Array.isArray(formConf)) {
97
+ return formConf;
98
+ }
99
+ if(typeof formConf === 'object' && Object.keys(formConf)?.length) {
100
+ return formConf;
101
+ }
102
+ return [];
103
+ },[formConf])
104
+
95
105
  // Build form groups with highlight/disable state
96
106
  useEffect(() => {
97
- const _formGroups = formConf.map((c) => {
107
+ const _formGroups = formConfigToUse?.map((c) => {
98
108
  const _highlighted = getHighlighted({ ...c }, { ...data });
99
109
 
100
110
  const disabled = getDisabled({
@@ -113,7 +123,7 @@ export const Sections = ({
113
123
 
114
124
  setHasMissing(!!_formGroups.find(f => f.hasMissing));
115
125
  setFormGroups(_formGroups);
116
- }, [formConf, highlightMandatory]);
126
+ }, [formConfigToUse, highlightMandatory]);
117
127
 
118
128
  // Initial selection logic
119
129
  useEffect(() => {
@@ -121,11 +131,11 @@ export const Sections = ({
121
131
 
122
132
  if (match?.params?.formId) {
123
133
  // Special case: only group id
124
- sGroup = group || (formConf.length ? formConf[0].id : null);
134
+ sGroup = group || (formConf?.length ? formConf?.[0]?.id : null);
125
135
  } else {
126
136
  // General case: full path
127
137
  sGroup = `${group || ''}${subsection ? `/${subsection}` : ''}${subgroup ? `/${subgroup}` : ''}${formid ? `/${formid}` : ''}`;
128
- if (!group && formConf.length) {
138
+ if (!group && formConf?.length) {
129
139
  sGroup = formConf[0].id;
130
140
  }
131
141
  }
@@ -176,12 +186,12 @@ export const Sections = ({
176
186
  let sGroup;
177
187
  if (match?.params?.formId) {
178
188
  // Special case fallback
179
- sGroup = group || (formConf.length ? formConf[0].id : null);
189
+ sGroup = group || (formConf?.length ? formConf?.[0]?.id : null);
180
190
  } else {
181
191
  // General fallback
182
192
  sGroup = `${group || ''}${subsection ? `/${subsection}` : ''}${subgroup ? `/${subgroup}` : ''}${formid ? `/${formid}` : ''}`;
183
- if (!group && formConf.length) {
184
- sGroup = formConf[0].id;
193
+ if (!group && formConf?.length) {
194
+ sGroup = formConf?.[0]?.id;
185
195
  }
186
196
  }
187
197
  setSelectedGroup(sGroup);
@@ -0,0 +1,178 @@
1
+ import { useState, useEffect } from "react";
2
+ import { hasKeyInObject } from "../../../utils/object.js"
3
+ import { mapFormGroup } from "../../../core/components/EditForm/helper.js"
4
+ import { changeInputMeta as changeInputMetaDaf, hasNotChanged} from "../../../../helpers/Forms.js"
5
+
6
+ export const usePrepareForm = ({
7
+ isSupported,
8
+ namespaceConfig,
9
+ APP,
10
+ allData,
11
+ id,
12
+ MainForm,
13
+ namespace,
14
+ setIsChanged,
15
+ changedPath,
16
+ setChangedPath,
17
+ }) => {
18
+ const [form, setForm] = useState({});
19
+ const [data, setData] = useState({});
20
+ const [groups, setGroups] = useState({});
21
+ const [linkingForms, setLinkingForms] = useState({});
22
+ const [loading, setLoading] = useState(true);
23
+ const [notFound, setNotFound] = useState(false);
24
+ const [editValues, setEditValues] = useState({});
25
+ const [inputsMeta, setInputsMeta] = useState({});
26
+ const [originalData, setOriginalData] = useState({});
27
+
28
+ const prepareForm = (setExtra = true) => {
29
+ if (isSupported) {
30
+ const dKey = namespaceConfig?.dataKey;
31
+ const nKey = `${APP}-${namespaceConfig?.view}`;
32
+ if (hasKeyInObject(allData, dKey) && hasKeyInObject(allData[dKey], nKey)) {
33
+ const d = JSON.parse(JSON.stringify(allData[dKey][nKey].data || {}));
34
+ Object.keys(d).forEach((k) => {
35
+ if (d[k] === null) {
36
+ d[k] = undefined;
37
+ }
38
+ });
39
+ if (d.datastakeId === id || d.applicationId === id || id === "user") {
40
+ const formG = mapFormGroup(allData[dKey][nKey].form || {}, d);
41
+ setForm(formG);
42
+
43
+ if (setExtra) {
44
+ if (
45
+ (d || {}).risk &&
46
+ Array.isArray(formG) &&
47
+ formG.find((r) => r.id === "risk")
48
+ ) {
49
+ const form = formG.find((r) => r.id === "risk");
50
+ if (form && form.options && form.template === "risk") {
51
+ const newData = Object.keys(d.risk).reduce((all, key) => {
52
+ const forms = form.options[key].forms;
53
+ const formsKeys = Object.keys(forms);
54
+ const formData = formsKeys.map((k) => {
55
+ const foundValue = d.risk[key].find(
56
+ (v) => v.subCategory === k,
57
+ );
58
+ return (
59
+ foundValue || {
60
+ id: k,
61
+ components: {},
62
+ config: [],
63
+ connectedEvents: {},
64
+ category: key,
65
+ score: 0,
66
+ subCategory: k,
67
+ }
68
+ );
69
+ });
70
+ all[key] = formData;
71
+ return all;
72
+ }, {});
73
+ setData({ ...d, risk: newData });
74
+ MainForm.resetFields();
75
+ setEditValues({ ...d });
76
+ setInputsMeta(d?.meta?.inputs || {});
77
+ setOriginalData({ ...d });
78
+ setLinkingForms(allData[dKey][nKey].linkingForms);
79
+ setLoading(false);
80
+ return;
81
+ }
82
+ }
83
+ setData({ ...d });
84
+ MainForm.resetFields();
85
+ setEditValues({ ...d });
86
+ setInputsMeta(d?.meta?.inputs || {});
87
+ setOriginalData({ ...d });
88
+ setLinkingForms(allData[dKey][nKey].linkingForms);
89
+ setLoading(false);
90
+ }
91
+ }
92
+ }
93
+ }
94
+ };
95
+ const getCertainData = allData?.[namespaceConfig?.dataKey];
96
+
97
+ useEffect(() => {
98
+ if(namespace && namespaceConfig) {
99
+ prepareForm(namespaceConfig?.view);
100
+ }
101
+ }, [getCertainData, namespaceConfig]);
102
+
103
+ const changeLinking = ({ key, linkingKey, value }) => {
104
+ const _val = {
105
+ linking: {
106
+ ...(data?.linking || {}),
107
+ [key]: {
108
+ ...((data?.linking || {})[key] || {}),
109
+ [linkingKey]: value,
110
+ },
111
+ },
112
+ };
113
+
114
+ const _all = { ...data, ..._val };
115
+
116
+ setData(_all);
117
+ };
118
+
119
+ const onValuesChange = (val, all) => {
120
+ setEditValues(all);
121
+ const keys = val && typeof val === "object" ? Object.keys(val) : [];
122
+ if (keys.length === 1) {
123
+ const changedKey = keys[0];
124
+ const ogData = originalData[changedKey];
125
+ const newData = all[changedKey];
126
+ let changePath = JSON.parse(JSON.stringify(changedPath));
127
+ if (!hasNotChanged({ [changedKey]: ogData }, { [changedKey]: newData })) {
128
+ if (!changePath.includes(changedKey)) {
129
+ changePath.push(changedKey);
130
+ }
131
+ } else {
132
+ changePath = changePath.filter((k) => k !== changedKey);
133
+ }
134
+ setChangedPath([...changePath]);
135
+ setIsChanged(changePath.length > 0 || newData ? true : false);
136
+ } else {
137
+ setIsChanged(true);
138
+ }
139
+ prepareForm(false);
140
+ };
141
+
142
+ const changeInputMeta = ({ key, type, value, otherValues = {} }) => {
143
+ return changeInputMetaDaf({
144
+ key,
145
+ type,
146
+ value,
147
+ otherValues,
148
+ setInputsMeta,
149
+ editValues,
150
+ MainForm,
151
+ setData,
152
+ onValuesChange,
153
+ });
154
+ };
155
+
156
+ return {
157
+ form,
158
+ data,
159
+ groups,
160
+ linkingForms,
161
+ loading,
162
+ notFound,
163
+ editValues,
164
+ inputsMeta,
165
+ originalData,
166
+ setGroups,
167
+ setNotFound,
168
+ setLinkingForms,
169
+ prepareForm,
170
+ setInputsMeta,
171
+ setData,
172
+ setLoading,
173
+ setEditValues,
174
+ changeLinking,
175
+ changeInputMeta,
176
+ onValuesChange,
177
+ }
178
+ }