Skip to content

Commit

Permalink
Merge pull request #1372 from bcgov/fix/prashanth-ag-grid-paste-1301-…
Browse files Browse the repository at this point in the history
…1302

Fix: LCFS Bug ag-grid copy paste function
  • Loading branch information
prv-proton authored Dec 7, 2024
2 parents 20e45d6 + 29aceb6 commit 8e4aa91
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
1 change: 1 addition & 0 deletions frontend/src/assets/locales/en/fuelCode.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"approvalDate": "Approval date",
"effectiveDate": "Effective date",
"expirationDate": "Expiry date",
"fuelTypeId": "Fuel type id",
"fuelType": "Fuel",
"feedstock": "Feedstock",
"feedstockLocation": "Feedstock location",
Expand Down
41 changes: 34 additions & 7 deletions frontend/src/components/BCDataGrid/BCGridEditor.jsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -33,6 +34,7 @@ import { BCAlert2 } from '@/components/BCAlert'
export const BCGridEditor = ({
gridRef,
alertRef,
enablePaste = true,
handlePaste,
onCellEditingStopped,
onCellValueChanged,
Expand Down Expand Up @@ -97,27 +99,52 @@ 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) => {
const newRow = { ...row }
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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => ({
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/views/FuelCodes/AddFuelCode/_schema.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8e4aa91

Please sign in to comment.