Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LCFS-1207: Autopopulate fuelCategory and fuelCode when selecting fuelType #1452

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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