diff --git a/src/components/BeneficiaryDuplicatesTable.js b/src/components/BeneficiaryDuplicatesTable.js index 789f9d4..ab59491 100644 --- a/src/components/BeneficiaryDuplicatesTable.js +++ b/src/components/BeneficiaryDuplicatesTable.js @@ -14,6 +14,8 @@ const useStyles = makeStyles((theme) => ({ tableHeader: theme.table.header, tableRow: theme.table.row, title: theme.paper.title, + tableDisabledRow: theme.table.disabledRow, + tableDisabledCell: theme.table.disabledCell, tableContainer: { overflow: 'auto', }, @@ -35,20 +37,30 @@ const useStyles = makeStyles((theme) => ({ })); function BeneficiaryDuplicatesTable({ - headers, rows, setAdditionalData, beneficiaryUuids, + headers, rows, setAdditionalData, completedData, }) { const classes = useStyles(); const [selectedCells, setSelectedCells] = useState([]); const [selectedRow, setSelectedRow] = useState(null); + const [dontMergeRows, setDontMergeRows] = useState([]); + const [fieldValues, setFieldValues] = useState({}); useEffect(() => { + const filteredIds = rows + .filter((row, index) => !dontMergeRows.includes(index)) + .map((row) => row.beneficiaryId); + const parsedFieldValues = selectedCells.reduce((acc, cell) => { + acc[cell.header] = cell.value ?? ''; + return acc; + }, {}); + setFieldValues(parsedFieldValues); const additionalData = ( - { values: selectedCells.map((cell) => ({ [cell.header]: cell.value })), beneficiaryIds: beneficiaryUuids } + { values: fieldValues, beneficiaryIds: filteredIds } ); // eslint-disable-next-line max-len const additionalDataString = `{\\"values\\": ${JSON.stringify(additionalData.values).replace(/"/g, '\\"')},\\"beneficiaryIds\\": ${JSON.stringify(additionalData.beneficiaryIds).replace(/"/g, '\\"')}}`; setAdditionalData(additionalDataString); - }, [selectedCells]); + }, [selectedCells, dontMergeRows]); const isCellSelected = (rowIndex, header) => selectedCells.some( (cell) => cell.rowIndex === rowIndex && cell.header === header, ); @@ -74,6 +86,10 @@ function BeneficiaryDuplicatesTable({ return; } + if (dontMergeRows.includes(rowIndex)) { + return; + } + const isCellSelectedInColumn = selectedCells.some((cell) => cell.header === header); const isCellClicked = isCellSelected(rowIndex, header); @@ -108,12 +124,41 @@ function BeneficiaryDuplicatesTable({ } }; + const handleMergeCheckboxChange = (rowIndex) => { + if (!dontMergeRows.includes(rowIndex)) { + clearRowSelection(rowIndex); + setDontMergeRows([...dontMergeRows, rowIndex]); + } else { + const index = dontMergeRows.indexOf(rowIndex); + if (index !== -1) { + const newDontMergeRows = [...dontMergeRows]; + newDontMergeRows.splice(index, 1); + setDontMergeRows(newDontMergeRows); + } + } + }; + + // eslint-disable-next-line max-len + const shouldHoverCell = (rowIndex, header) => !isCellSelected(rowIndex, header) && header !== 'individual' && !dontMergeRows.includes(rowIndex); + const shouldDisableCell = (rowIndex) => dontMergeRows.includes(rowIndex); + + useEffect(() => { + if (completedData) { + const numberOfRows = Array.from(Array(rows.length).keys()); + clearAllCellSelection(); + setDontMergeRows(numberOfRows); + } + }, [completedData]); + return (