Skip to content

Commit 3c412a1

Browse files
Handled scenarios related to add technique
1 parent da98eee commit 3c412a1

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/components/ContentManager/ManageContent.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useState, useEffect } from 'react';
22
import { FaChevronUp, FaChevronDown } from 'react-icons/fa';
33
import './ManageContent.css';
44
import { fetchAllTechniques, fetchTechnique, dataArray, getUniqueTechniques } from '../../utils/dataMap';
5-
import { transformData, transformKeyValue, transformReference, formatDetails, formatFields, formatReferences, handleNewTactic, addTechniqueIfNotExists } from './ManageContentUtils';
5+
import { transformData, transformKeyValue, transformReference, formatDetails, formatFields, formatReferences, handleNewTactic, addTechniqueIfNotExists, consolidateData } from './ManageContentUtils';
66
import { RiAddCircleLine, RiCheckboxFill, RiDeleteBinLine } from 'react-icons/ri';
77
import { Alert } from '../Alert/Alert'
88

@@ -351,7 +351,7 @@ const ManageContent = (props) => {
351351
handleNewTactic(techniqueName, tactics)
352352
let inputJSON = localStorage.getItem('technique_table') ? JSON.parse(localStorage.getItem('technique_table')) : [];
353353
addedTechnique = addTechniqueIfNotExists(inputJSON, tactics, techniqueName)
354-
localStorage.setItem('technique_table', JSON.stringify(addedTechnique));
354+
localStorage.setItem('technique_table', JSON.stringify(consolidateData(addedTechnique)));
355355
}
356356

357357
// Now, update the localStorage based on techniqueName

src/components/ContentManager/ManageContentUtils.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@ export const formatFields = (inputJson) => {
5050
return inputJson.map(item => item.value);
5151
};
5252

53+
export const consolidateData = (data) => {
54+
// Step 0: Remove rows where all keys have empty string values
55+
const filteredData = data.filter(row => {
56+
return Object.values(row).some(value => value !== '');
57+
});
58+
59+
// Step 1: Collect all non-empty values for each column
60+
const nonEmptyValues = {};
61+
62+
filteredData.forEach((row) => {
63+
Object.entries(row).forEach(([key, value]) => {
64+
if (value !== '') {
65+
if (!nonEmptyValues[key]) {
66+
nonEmptyValues[key] = [];
67+
}
68+
nonEmptyValues[key].push(value);
69+
}
70+
});
71+
});
72+
73+
// Step 2: Reconstruct rows with consolidated non-empty values
74+
return filteredData.map((row) => {
75+
const newRow = {};
76+
77+
Object.keys(row).forEach((key) => {
78+
if (nonEmptyValues[key] && nonEmptyValues[key].length > 0) {
79+
newRow[key] = nonEmptyValues[key].shift();
80+
} else {
81+
newRow[key] = '';
82+
}
83+
});
84+
85+
return newRow;
86+
});
87+
};
88+
89+
5390

5491
const downloadJSON = (data) => {
5592
const now = new Date();

0 commit comments

Comments
 (0)