```
Add duplicate detection for spreadsheet data processing Implemented logic to detect and handle duplicate 'positioninpuck' entries within the same puck during spreadsheet processing. Updated backend to validate duplicates and provide detailed error messages. Enhanced frontend to visually highlight duplicate errors and allow better user feedback during cell editing. ```
This commit is contained in:
@ -83,15 +83,18 @@ const SpreadsheetTable = ({
|
||||
}, [selectedShipment]);
|
||||
|
||||
const generateErrorMap = (errorsList) => {
|
||||
const errorMap = new Map();
|
||||
if (Array.isArray(errorsList)) {
|
||||
errorsList.forEach((error) => {
|
||||
const key = `${error.row}-${headers[error.cell]}`;
|
||||
errorMap.set(key, error.message);
|
||||
});
|
||||
}
|
||||
return errorMap;
|
||||
};
|
||||
const errorMap = new Map();
|
||||
if (Array.isArray(errorsList)) {
|
||||
errorsList.forEach((error) => {
|
||||
const colIndex = headers.findIndex(header => header === error.column);
|
||||
if (colIndex > -1) {
|
||||
const key = `${error.row}-${headers[colIndex]}`;
|
||||
errorMap.set(key, error.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
return errorMap;
|
||||
};
|
||||
|
||||
const errorMap = generateErrorMap(localErrors);
|
||||
|
||||
@ -139,7 +142,6 @@ const SpreadsheetTable = ({
|
||||
|
||||
if (response && response.is_valid !== undefined) {
|
||||
if (response.is_valid) {
|
||||
// If valid, update the value (and use corrected_value if returned)
|
||||
const correctedValue = response.corrected_value ?? newValue;
|
||||
currentRow.data[colIndex] = correctedValue;
|
||||
updatedRawData[rowIndex] = currentRow;
|
||||
@ -148,7 +150,12 @@ const SpreadsheetTable = ({
|
||||
|
||||
// Remove the error and mark as non-editable
|
||||
const updatedErrors = localErrors.filter(
|
||||
(error) => !(error.row === currentRow.row_num && error.cell === colIndex)
|
||||
(error) =>
|
||||
!(
|
||||
error.row === currentRow.row_num &&
|
||||
error.cell === colIndex &&
|
||||
error.message.toLowerCase().includes("duplicate position")
|
||||
)
|
||||
);
|
||||
setLocalErrors(updatedErrors); // Update error list
|
||||
|
||||
@ -188,7 +195,11 @@ const SpreadsheetTable = ({
|
||||
handleCellEdit(rowIndex, colIndex);
|
||||
};
|
||||
|
||||
const allCellsValid = () => nonEditableCells.size === raw_data.length * headers.length;
|
||||
const allCellsValid = () =>
|
||||
nonEditableCells.size === raw_data.length * headers.length &&
|
||||
!localErrors.some((error) =>
|
||||
error.message.toLowerCase().includes("duplicate position")
|
||||
);
|
||||
|
||||
const fieldToCol = {
|
||||
'dewarname': 0,
|
||||
@ -514,6 +525,7 @@ const SpreadsheetTable = ({
|
||||
const key = `${row.row_num}-${header}`;
|
||||
const errorMessage = errorMap.get(key);
|
||||
const isInvalid = !!errorMessage;
|
||||
const isDuplicateError = errorMessage?.toLowerCase().includes("duplicate position"); // Detect duplicate-specific messages
|
||||
const cellValue = row.data[colIndex];
|
||||
const editingValue = editingCell[`${rowIndex}-${colIndex}`];
|
||||
const isCellCorrected = row.corrected_columns?.includes(header); // Use corrected metadata
|
||||
@ -525,13 +537,21 @@ const SpreadsheetTable = ({
|
||||
align="center"
|
||||
style={{
|
||||
backgroundColor: isDefaultAssigned
|
||||
? "#e6fbe6" // Light green
|
||||
? "#e6fbe6" // Light green for default values
|
||||
: isCellCorrected
|
||||
? "#fff8e1" // Light yellow
|
||||
: "transparent",
|
||||
color: isDefaultAssigned ? "#1b5e20" : "inherit",
|
||||
fontWeight: isCellCorrected || isDefaultAssigned ? "bold" : "normal",
|
||||
cursor: isInvalid ? "pointer" : "default",
|
||||
? "#fff8e1" // Light yellow for corrections
|
||||
: isDuplicateError
|
||||
? "#fdecea" // Light red for duplicate errors
|
||||
: "transparent", // No specific color for valid cells
|
||||
color: isDefaultAssigned
|
||||
? "#1b5e20" // Dark green for default values
|
||||
: isDuplicateError
|
||||
? "#d32f2f" // Bright red for duplicates
|
||||
: "inherit",
|
||||
fontWeight: isDefaultAssigned || isCellCorrected || isDuplicateError
|
||||
? "bold"
|
||||
: "normal",
|
||||
cursor: isInvalid ? "pointer" : "default", // Allow editing invalid cells
|
||||
}}
|
||||
>
|
||||
<Tooltip
|
||||
@ -540,7 +560,9 @@ const SpreadsheetTable = ({
|
||||
? "This value was automatically assigned as a default."
|
||||
: isCellCorrected
|
||||
? `Field "${header}" was auto-corrected.`
|
||||
: errorMessage || ""
|
||||
: isDuplicateError
|
||||
? `Duplicate value detected in "${header}". Please ensure values are unique in this column.`
|
||||
: errorMessage || ""
|
||||
}
|
||||
arrow
|
||||
disableHoverListener={!isDefaultAssigned && !isCellCorrected && !isInvalid}
|
||||
|
Reference in New Issue
Block a user