diff --git a/frontend/src/assets/locales/en/fuelCode.json b/frontend/src/assets/locales/en/fuelCode.json index 383255080..84f150236 100644 --- a/frontend/src/assets/locales/en/fuelCode.json +++ b/frontend/src/assets/locales/en/fuelCode.json @@ -36,6 +36,7 @@ "approvalDate": "Approval date", "effectiveDate": "Effective date", "expirationDate": "Expiry date", + "fuelTypeId": "Fuel type id", "fuelType": "Fuel", "feedstock": "Feedstock", "feedstockLocation": "Feedstock location", diff --git a/frontend/src/components/BCDataGrid/BCGridEditor.jsx b/frontend/src/components/BCDataGrid/BCGridEditor.jsx index 6771a3454..cdc02be64 100644 --- a/frontend/src/components/BCDataGrid/BCGridEditor.jsx +++ b/frontend/src/components/BCDataGrid/BCGridEditor.jsx @@ -1,3 +1,4 @@ +/* eslint-disable react-hooks/exhaustive-deps */ import BCBox from '@/components/BCBox' import { BCGridBase } from '@/components/BCDataGrid/BCGridBase' import { isEqual } from '@/utils/grid/eventHandlers' @@ -33,6 +34,7 @@ import { BCAlert2 } from '@/components/BCAlert' export const BCGridEditor = ({ gridRef, alertRef, + enablePaste = true, handlePaste, onCellEditingStopped, onCellValueChanged, @@ -97,9 +99,13 @@ export const BCGridEditor = ({ const parsedData = Papa.parse(headerRow + '\n' + pastedData, { delimiter: '\t', header: true, + transform: (value) => { + const num = Number(value) // Attempt to convert to a number if possible + return isNaN(num) ? value : num // Return the number if valid, otherwise keep as string + }, skipEmptyLines: true }) - if (parsedData.data.length < 1 || parsedData.data[1].length < 2) { + if (parsedData.data.length < 0 || parsedData.data[1].length < 2) { return } parsedData.data.forEach((row) => { @@ -107,17 +113,38 @@ export const BCGridEditor = ({ newRow.id = uuid() newData.push(newRow) }) - ref.current.api.applyTransaction({ add: newData }) + const transactions = ref.current.api.applyTransaction({ add: newData }) + // Trigger onCellEditingStopped event to update the row in backend. + transactions.add.forEach((node) => { + onCellEditingStopped({ + node, + oldValue: '', + newvalue: node.data[findFirstEditableColumn()], + ...props + }) + }) }, - [ref] + [findFirstEditableColumn, onCellEditingStopped, props, ref] ) useEffect(() => { - window.addEventListener('paste', handlePaste || handleExcelPaste) - return () => { - window.removeEventListener('paste', handlePaste || handleExcelPaste) + const pasteHandler = (event) => { + const gridApi = ref.current?.api + const columnApi = ref.current?.columnApi + + if (handlePaste) { + handlePaste(event, { api: gridApi, columnApi }) + } else { + handleExcelPaste(event) // Fallback to the default paste function + } + } + if (enablePaste) { + window.addEventListener('paste', pasteHandler) + return () => { + window.removeEventListener('paste', pasteHandler) + } } - }, [handleExcelPaste, handlePaste]) + }, [handleExcelPaste, handlePaste, ref, enablePaste]) const handleOnCellEditingStopped = useCallback( async (params) => { diff --git a/frontend/src/components/BCDataGrid/components/Editors/AutocompleteCellEditor.jsx b/frontend/src/components/BCDataGrid/components/Editors/AutocompleteCellEditor.jsx index f68b086b9..de12c61ea 100644 --- a/frontend/src/components/BCDataGrid/components/Editors/AutocompleteCellEditor.jsx +++ b/frontend/src/components/BCDataGrid/components/Editors/AutocompleteCellEditor.jsx @@ -37,7 +37,9 @@ export const AutocompleteCellEditor = forwardRef((props, ref) => { onPaste } = props - const [selectedValues, setSelectedValues] = useState(value || []) + const [selectedValues, setSelectedValues] = useState( + (Array.isArray(value) ? value : value.split(',').map((v) => v.trim())) || [] + ) const inputRef = useRef() useImperativeHandle(ref, () => ({ diff --git a/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx b/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx index 24ec43770..3c39a6f4c 100644 --- a/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx +++ b/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx @@ -84,6 +84,13 @@ export const fuelCodeColDefs = (optionsData, errors, isCreate, canEdit) => [ ) return selectedOption.prefix } + const selectedOption = optionsData?.fuelCodePrefixes?.find( + (obj) => obj.prefix === params.data.prefix + ) + if (selectedOption) { + params.data.prefixId = selectedOption.fuelCodePrefixId + } + return params.data.prefix }, valueSetter: (params) => { if (params.newValue !== params.oldValue) { @@ -319,6 +326,13 @@ export const fuelCodeColDefs = (optionsData, errors, isCreate, canEdit) => [ ) return selectedOption.fuelType } + const selectedOption = optionsData?.fuelTypes?.find( + (obj) => obj.fuelType === params.data.fuel + ) + if (selectedOption) { + params.data.fuelTypeId = selectedOption.fuelTypeId + } + return params.data.fuel }, valueSetter: (params) => { if (params.newValue) {