Skip to content

Commit

Permalink
[#379] Fixing the bug where scanned products will show 200 as CO2e (#386
Browse files Browse the repository at this point in the history
)

This bug happened if the slider was not moved before adding the scanned product to the emission lists.
  • Loading branch information
saurabhchatterjee23 authored Nov 2, 2023
1 parent fe444a5 commit fddce3c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
7 changes: 5 additions & 2 deletions app/screens/AddEmission/AddEmissionScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const DEFAULT_SLIDER_VALUE_PURCHASE = 1;
const DEFAULT_SLIDER_VALUE_FASHION = 1;
const DEFAULT_SLIDER_VALUE_MEAL = 1;
const DEFAULT_SLIDER_VALUE_CUSTOM = 200;
const DEFAULT_SLIDER_VALUE_SCANNED = 1;
const EMISSION_NAME_MAX_LENGTH = 150;

const getProductCarbonFootprint = pathOr(0, ["params", "productCarbonFootprint"]);
Expand All @@ -78,6 +79,7 @@ const AddEmissionScreen = ({ locale = "", language = "" }: LocalizationContextPr
);
const [durationSeconds, setDurationSeconds] = useState<number>(DEFAULT_SLIDER_VALUE_STREAMING);
const [co2eqKilograms, setCo2eqKilograms] = useState<number>(DEFAULT_SLIDER_VALUE_CUSTOM);
const [productScannedQuantity, setProductScannedQuantity] = useState<number>(DEFAULT_SLIDER_VALUE_SCANNED);
const [distance, setDistance] = useState<number>(DEFAULT_SLIDER_VALUE_TRANSPORT);
const [foodQuantity, setFoodQuantity] = useState<number>(DEFAULT_SLIDER_VALUE_FOOD);
const [purchaseQuantity, setPurchaseQuantity] = useState<number>(DEFAULT_SLIDER_VALUE_PURCHASE);
Expand Down Expand Up @@ -267,13 +269,14 @@ const AddEmissionScreen = ({ locale = "", language = "" }: LocalizationContextPr

const renderProductScanned = () => {
if (emissionType === EmissionType.productScanned) {
emissionPayload.value = co2eqKilograms;
emissionPayload.value = productScannedQuantity * productCarbonFootprint;
emissionPayload.emissionModelType = EmissionType.productScanned as EmissionModelType;

return (
<ProductScanned
productCarbonFootprint={productCarbonFootprint}
setCo2eqKilograms={setCo2eqKilograms}
defaultValueSlider={DEFAULT_SLIDER_VALUE_SCANNED}
setProductScannedQuantity={setProductScannedQuantity}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ const MAX_SLIDER_VALUE = 10;

interface Props {
productCarbonFootprint: number;
setCo2eqKilograms: (arg0: number) => void;
defaultValueSlider:number;
setProductScannedQuantity: (arg0: number) => void;
}

const ProductScanned: React.FC<Props> = ({ setCo2eqKilograms, productCarbonFootprint }) => {
const [sliderValue, setSliderValue] = useState(1.4);

const emissionAmount =
productCarbonFootprint < 1
? Math.round(Math.round(sliderValue) * productCarbonFootprint * 1000) / 1000
: Math.round(Math.round(sliderValue) * productCarbonFootprint * 10) / 10;
const ProductScanned: React.FC<Props> = ({ setProductScannedQuantity, defaultValueSlider, productCarbonFootprint }) => {
const [sliderValue, setSliderValue] = useState(defaultValueSlider);

const emissionAmount = Math.round(sliderValue) * productCarbonFootprint

const onSliderValueChange = (value: number) => {
setSliderValue(value);
setCo2eqKilograms(emissionAmount);
setProductScannedQuantity(Math.round(value));
};

const useMetricUnits = useSelector(userPreferences.selectors.getUseMetricUnits);
Expand Down

0 comments on commit fddce3c

Please sign in to comment.