-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frontend : fix problems for product setup
- Loading branch information
1 parent
a9a4be8
commit a883f19
Showing
4 changed files
with
118 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 26 additions & 45 deletions
71
frontend/app/src/domains/production/views/PackageLotsCreator.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,36 @@ | ||
import React from 'react' | ||
import { useEffect, useState } from "react" | ||
import { useKeycloak } from '@react-keycloak/web' | ||
import { ApiBuilder } from '../../../api/ApiBuilder.ts' | ||
import PackageLot from "viandeendirect_eu/dist/model/PackageLot" | ||
import React, { useState } from 'react' | ||
import PackageLotConfigurator from "../components/PackageLotConfigurator.tsx" | ||
import { BeefProductionService } from '../service/BeefProductionService.ts' | ||
import { Alert } from '@mui/material' | ||
|
||
|
||
export default function PackageLotsCreator({ | ||
production: production, | ||
changeProductionCallback: changeProductionCallback, | ||
disabled: disabled = false }) { | ||
export default function PackageLotsCreator({ | ||
production: production, | ||
disabled: disabled = false, | ||
changeQuantitiesCompliancyCallback: changeQuantitiesCompliancyCallback = (isCompliant) => {}}) { | ||
|
||
const { keycloak } = useKeycloak() | ||
const apiBuilder = new ApiBuilder() | ||
return <> | ||
{displayAlerts()} | ||
{production.lots?.map(lot => <PackageLotConfigurator | ||
lot={lot} | ||
disabled={disabled} | ||
changeQuantitySoldCallback={changeQuantitySold} />)} | ||
</> | ||
|
||
useEffect(() => { | ||
if (!production.lots) { | ||
apiBuilder.getAuthenticatedApi(keycloak).then(api => { | ||
apiBuilder.invokeAuthenticatedApi(() => { | ||
api.getPackageTemplates((error, data, response) => { | ||
if (error) { | ||
console.error(error); | ||
} else { | ||
console.log('api.getPackageTemplates called successfully. Returned data: ' + data); | ||
const lots = [] | ||
data.map(template => { | ||
let lot = new PackageLot() | ||
lot.label = template.label | ||
lot.description = template.description | ||
lot.unitPrice = template.unitPrice | ||
lot.netWeight = template.netWeight | ||
lot.quantity = 0 | ||
lot.quantitySold = 0 | ||
lots.push(lot) | ||
}) | ||
changeProductionCallback({ ...production, lots: lots }) | ||
} | ||
}) | ||
}, keycloak) | ||
}) | ||
function displayAlerts() { | ||
const totalQuantitySold = production.lots?.map(lot => lot.netWeight * lot.quantity).reduce((total, added) => total + added) || 0 | ||
if (!isTotalQuantitySoldLowerThanMeatWeight(totalQuantitySold)) { | ||
const meatQuantity = BeefProductionService.getMeatWeight(production.warmCarcassWeight) | ||
return <Alert severity="error">Le poids total des produits préparés ne doit pas dépasser la quantité de viande de l'animal estimée à {meatQuantity} kg.</Alert> | ||
} | ||
}, [keycloak]) | ||
} | ||
|
||
return <> | ||
{production.lots?.map(lot => <PackageLotConfigurator | ||
packageLot={lot} | ||
changeCallback={changeLotConfiguration} | ||
disabled={disabled}/>)} | ||
</> | ||
function isTotalQuantitySoldLowerThanMeatWeight(totalQuantitySoldUpdated) { | ||
return totalQuantitySoldUpdated < BeefProductionService.getMeatWeight(production.warmCarcassWeight) | ||
} | ||
|
||
function changeLotConfiguration() { | ||
changeProductionCallback({ ...production }) | ||
function changeQuantitySold() { | ||
const totalQuantitySold = production.lots?.map(lot => lot.netWeight * lot.quantity).reduce((total, added) => total + added) || 0 | ||
changeQuantitiesCompliancyCallback(isTotalQuantitySoldLowerThanMeatWeight(totalQuantitySold)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters