Skip to content

Commit

Permalink
Autopopulate fuelCategory and fuelCode when selecting fuelType
Browse files Browse the repository at this point in the history
  • Loading branch information
areyeslo committed Dec 13, 2024
1 parent 390f6f1 commit 39d0e62
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
7 changes: 7 additions & 0 deletions backend/lcfs/web/api/other_uses/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@ class ExpectedUseTypeSchema(BaseSchema):
description: Optional[str] = None


class FuelCategorySchema(BaseSchema):
fuel_category_id: int
category: str
description: Optional[str] = None


class FuelTypeSchema(BaseSchema):
fuel_type_id: int
fuel_type: str
fossil_derived: Optional[bool] = None
provision_1_id: Optional[int] = None
provision_2_id: Optional[int] = None
fuel_categories: List[FuelCategorySchema]
default_carbon_intensity: Optional[float] = None
fuel_codes: Optional[List[FuelCodeSchema]] = []
provision_of_the_act: Optional[List[ProvisionOfTheActSchema]] = []
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/BCDataGrid/columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const actions = (props) => ({
cellRendererParams: props,
pinned: 'left',
maxWidth: 110,
minWidth: 90,
editable: false,
suppressKeyboardEvent,
filter: false,
Expand Down
41 changes: 30 additions & 11 deletions frontend/src/views/OtherUses/AddEditOtherUses.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,39 @@ export const AddEditOtherUses = () => {
const ciOfFuel = findCiOfFuel(params.data, optionsData)
params.node.setDataValue('ciOfFuel', ciOfFuel)

// Auto-populate the "Unit" field based on the selected fuel type
if (params.colDef.field === 'fuelType') {
const fuelType = optionsData?.fuelTypes?.find(
(obj) => params.data.fuelType === obj.fuelType
);
if (fuelType && fuelType.units) {
params.node.setDataValue('units', fuelType.units);
} else {
params.node.setDataValue('units', '');
// Auto-populate fields based on the selected fuel type
if (params.colDef.field === 'fuelType') {
const fuelType = optionsData?.fuelTypes?.find(
(obj) => params.data.fuelType === obj.fuelType
);
if (fuelType) {
// Auto-populate the "units" field
if (fuelType.units) {
params.node.setDataValue('units', fuelType.units);
} else {
params.node.setDataValue('units', '');
}

// Auto-populate the "fuelCategory" field
const fuelCategoryOptions = fuelType.fuelCategories.map(
(item) => item.category
);
params.node.setDataValue('fuelCategory', fuelCategoryOptions[0] ?? null);

// Auto-populate the "fuelCode" field
const fuelCodeOptions = fuelType.fuelCodes.map(
(code) => code.fuelCode
);
params.node.setDataValue('fuelCode', fuelCodeOptions[0] ?? null);
params.node.setDataValue(
'fuelCodeId',
fuelType.fuelCodes[0]?.fuelCodeId ?? null
);
}
}
}
}
},
[optionsData]
[optionsData, findCiOfFuel]
)

const onCellEditingStopped = useCallback(
Expand Down
17 changes: 11 additions & 6 deletions frontend/src/views/OtherUses/_schema.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ export const otherUsesColDefs = (optionsData, errors) => [
headerName: i18n.t('otherUses:otherUsesColLabels.fuelCategory'),
headerComponent: RequiredHeader,
cellEditor: AutocompleteCellEditor,
cellEditorParams: {
options: optionsData.fuelCategories.map((obj) => obj.category),
multiple: false,
disableCloseOnSelect: false,
freeSolo: false,
openOnFocus: true
cellEditorParams: (params) => {
const fuelType = optionsData?.fuelTypes?.find(
(obj) => params.data.fuelType === obj.fuelType
);
return {
options: fuelType ? fuelType.fuelCategories.map((item) => item.category) : [],
multiple: false,
disableCloseOnSelect: false,
freeSolo: false,
openOnFocus: true
};
},
suppressKeyboardEvent,
cellRenderer: (params) =>
Expand Down

0 comments on commit 39d0e62

Please sign in to comment.