Skip to content

Commit

Permalink
Zero is not a valid number for quantity supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
areyeslo committed Nov 29, 2024
1 parent 66fbb2e commit b179102
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 3 additions & 2 deletions backend/lcfs/web/api/other_uses/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ class OtherUsesTableOptionsSchema(BaseSchema):
class OtherUsesCreateSchema(BaseSchema):
other_uses_id: Optional[int] = None
compliance_report_id: int
quantity_supplied: int
fuel_type: str
fuel_category: str
expected_use: str
provision_of_the_act: str
fuel_code: Optional[str] = None
quantity_supplied: int = Field(
..., gt=0, description="Quantity supplied must be greater than 0"
)
units: str
ci_of_fuel: Optional[float] = None
expected_use: str
Expand Down
26 changes: 26 additions & 0 deletions frontend/src/views/OtherUses/AddEditOtherUses.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ export const AddEditOtherUses = () => {
return ciOfFuel;
}, []);

const validateField = (params, field, validationFn, errorMessage, alertRef) => {
const newValue = params.newValue;

if (params.colDef.field === field) {
if (!validationFn(newValue)) {
alertRef.current?.triggerAlert({
message: errorMessage,
severity: 'error',
});
return false;
}
}

return true; // Proceed with the update
};

const onGridReady = (params) => {
const ensureRowIds = (rows) => {
return rows.map((row) => {
Expand Down Expand Up @@ -156,6 +172,16 @@ export const AddEditOtherUses = () => {

const onCellEditingStopped = useCallback(
async (params) => {
const isValid = validateField(
params,
'quantitySupplied',
(value) => value !== null && !isNaN(value) && value > 0,
'Quantity supplied must be greater than 0.',
alertRef
);

if (!isValid) return;

if (params.oldValue === params.newValue) return
params.data.complianceReportId = complianceReportId
params.data.validationStatus = 'pending'
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/views/OtherUses/_schema.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,10 @@ export const otherUsesColDefs = (optionsData, errors) => [
headerName: i18n.t('otherUses:otherUsesColLabels.quantitySupplied'),
headerComponent: RequiredHeader,
cellEditor: NumberEditor,
valueFormatter,
valueFormatter: (params) => valueFormatter({ value: params.value }),
type: 'numericColumn',
cellEditorParams: {
precision: 0,
min: 0,
showStepperButtons: false
},
cellStyle: (params) => StandardCellErrors(params, errors),
Expand Down

0 comments on commit b179102

Please sign in to comment.