From d77ee11f957dd952ed61ab9bdcf42875cc016bd4 Mon Sep 17 00:00:00 2001 From: prv-proton Date: Thu, 5 Dec 2024 01:32:03 -0800 Subject: [PATCH 1/3] Fix: LCFS Bug ag-grid copy paste function --- frontend/src/assets/locales/en/fuelCode.json | 1 + .../components/BCDataGrid/BCGridEditor.jsx | 41 +++++++++++++++---- .../Editors/AutocompleteCellEditor.jsx | 4 +- .../views/FuelCodes/AddFuelCode/_schema.jsx | 16 +++++++- 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/frontend/src/assets/locales/en/fuelCode.json b/frontend/src/assets/locales/en/fuelCode.json index 50d99f989..e92c970f3 100644 --- a/frontend/src/assets/locales/en/fuelCode.json +++ b/frontend/src/assets/locales/en/fuelCode.json @@ -37,6 +37,7 @@ "effectiveDate": "Effective date", "expiryDate": "Expiry date", "fuel": "Fuel", + "fuelTypeId": "Fuel type id", "feedstock": "Feedstock", "feedstockLocation": "Feedstock location", "misc": "Misc (e.g. CCS, EOR, waste steam, non rendered, etc.,)", 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 682cb52cb..6086eed9e 100644 --- a/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx +++ b/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx @@ -78,12 +78,19 @@ export const fuelCodeColDefs = (optionsData, errors, isCreate, canEdit) => [ valueGetter: (params) => { if (params.data?.fuelCodePrefix?.prefix) { return params.data.fuelCodePrefix.prefix - } else if (params.data.prefixId) { + }else if (params.data.prefixId) { const selectedOption = optionsData?.fuelCodePrefixes?.find( (obj) => obj.fuelCodePrefixId === params.data.prefixId ) return selectedOption.prefix } + const selectedOption = optionsData?.fuelCodePrefixes?.find( + (obj) => obj.prefix === params.data.prefix + ) + if (params.data.prefix) { + params.data.prefixId = selectedOption.fuelCodePrefixId + } + return params.data.prefix }, valueSetter: (params) => { if (params.newValue !== params.oldValue) { @@ -316,6 +323,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) { From 0f5eaac110739acc3f6b6fe0bd5218dd296c28c1 Mon Sep 17 00:00:00 2001 From: prv-proton Date: Fri, 6 Dec 2024 10:11:41 -0800 Subject: [PATCH 2/3] comment resolution. --- frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx b/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx index 214a0d89a..c50defdce 100644 --- a/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx +++ b/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx @@ -78,7 +78,7 @@ export const fuelCodeColDefs = (optionsData, errors, isCreate, canEdit) => [ valueGetter: (params) => { if (params.data?.fuelCodePrefix?.prefix) { return params.data.fuelCodePrefix.prefix - }else if (params.data.prefixId) { + } else if (params.data.prefixId) { const selectedOption = optionsData?.fuelCodePrefixes?.find( (obj) => obj.fuelCodePrefixId === params.data.prefixId ) @@ -87,7 +87,7 @@ export const fuelCodeColDefs = (optionsData, errors, isCreate, canEdit) => [ const selectedOption = optionsData?.fuelCodePrefixes?.find( (obj) => obj.prefix === params.data.prefix ) - if (params.data.prefix) { + if (params.data.prefix && selectedOption) { params.data.prefixId = selectedOption.fuelCodePrefixId } return params.data.prefix From 747be06151852e05bdaf24ce119cea404d4f9d1b Mon Sep 17 00:00:00 2001 From: prv-proton Date: Fri, 6 Dec 2024 10:12:51 -0800 Subject: [PATCH 3/3] . --- frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx b/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx index c50defdce..e943ec57f 100644 --- a/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx +++ b/frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx @@ -87,7 +87,7 @@ export const fuelCodeColDefs = (optionsData, errors, isCreate, canEdit) => [ const selectedOption = optionsData?.fuelCodePrefixes?.find( (obj) => obj.prefix === params.data.prefix ) - if (params.data.prefix && selectedOption) { + if (selectedOption) { params.data.prefixId = selectedOption.fuelCodePrefixId } return params.data.prefix