Skip to content

Commit 2f5867c

Browse files
committed
refactored handle delete function
1 parent 8f45f1c commit 2f5867c

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

JeMPI_Apps/JeMPI_UI/src/pages/settings/deterministic/DeterministicContent.tsx

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -227,45 +227,38 @@ const DeterministicContent = ({
227227
}
228228

229229
const handleDeleteRow = (index: number) => {
230-
const newComparators = [...comparators]
231-
const newFields = [...fields]
232-
const newOperators = [...operators]
233-
newComparators.splice(index, 1)
234-
newFields.splice(index, 1)
235-
230+
const updateArray = (arr: any[], idx: number) => {
231+
const newArr = [...arr];
232+
newArr.splice(idx, 1);
233+
return newArr;
234+
};
235+
236+
setComparators(updateArray(comparators, index));
237+
setFields(updateArray(fields, index));
236238
if (index > 0) {
237-
newOperators.splice(index - 1, 1)
239+
setOperators(updateArray(operators, index - 1));
238240
}
239-
240-
setComparators(newComparators)
241-
setFields(newFields)
242-
setOperators(newOperators)
243-
244-
const updatedConfiguration = { ...configuration }
241+
242+
const updatedConfiguration = { ...configuration };
245243
const ruleType =
246244
currentTab === 'link'
247245
? 'link'
248246
: currentTab === 'validate'
249247
? 'validate'
250-
: 'matchNotification'
251-
252-
if (newFields.length === 0) {
253-
const newRules = [...rules]
254-
newRules.splice(index, 1)
255-
setRules(newRules)
256-
257-
if (updatedConfiguration.rules && updatedConfiguration.rules[ruleType]) {
258-
updatedConfiguration.rules[ruleType].deterministic.splice(index, 1)
248+
: 'matchNotification';
249+
250+
if (fields.length === 0) {
251+
const newRules = updateArray(rules, index);
252+
setRules(newRules);
253+
254+
if (updatedConfiguration.rules?.[ruleType]) {
255+
updatedConfiguration.rules[ruleType].deterministic.splice(index, 1);
259256
}
260-
261-
setConfiguration(updatedConfiguration as Configuration)
262-
localStorage.setItem(
263-
'configuration',
264-
JSON.stringify(updatedConfiguration)
265-
)
257+
258+
setConfiguration(updatedConfiguration as Configuration);
259+
localStorage.setItem('configuration', JSON.stringify(updatedConfiguration));
266260
}
267-
}
268-
261+
};
269262
return (
270263
<Box>
271264
<Box

0 commit comments

Comments
 (0)