Skip to content

Commit

Permalink
refinement
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette committed Jan 5, 2025
1 parent a7aa8bb commit 2a1a9cb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
23 changes: 10 additions & 13 deletions src/pages/nutrition/NutrimentCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function clean(input: undefined | string | null | number): string {
if (input == undefined) {
return "";
}
return `${input}`.replaceAll(" ", "");
return `${input}`.replaceAll(" ", "").toLowerCase();
}

function getLegendColor(productValue, value, productUnit, unit) {
Expand All @@ -58,18 +58,15 @@ function getLegendColor(productValue, value, productUnit, unit) {

const ratioProduct = getRatio(cleanProductUnit);
const ratioInput = getRatio(cleanUnit);
if (ratioProduct ===
ratioInput) {
return "red";
}
else {
const numberProduct = Number.parseFloat(cleanProductValue.match(/(\.|,|\d)+/)[0])
const numberInput = Number.parseFloat(cleanValue.match(/(\.|,|\d)+/)[0])
if (ratioProduct * numberProduct === ratioInput * numberInput) {
return 'green'
}
return 'red'
const numberProduct = Number.parseFloat(cleanProductValue.match(/(\.|,|\d)+/)[0])
const numberInput = Number.parseFloat(cleanValue.match(/(\.|,|\d)+/)[0])


if (ratioProduct * numberProduct === ratioInput * numberInput) {
return 'green'
}
return 'red'

// Should never reach that part.
return undefined;
}
Expand Down Expand Up @@ -142,7 +139,7 @@ export const NutrimentCell = (props: NutrimentCellProps) => {
</legend>
)}
</div>
{isValidUnit(unit, nutrimentId) ? (
{forcedUnit === undefined && isValidUnit(unit, nutrimentId) ? (
<select
style={{ width: 55 }}
value={unit || ""}
Expand Down
6 changes: 4 additions & 2 deletions src/pages/nutrition/useNutrimentTranslations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ interface Nutri {
name?: string;
nutrients?: Nutri[];
}
function parseNutrients(data: Nutri[]): Record<string, string> {
function parseNutrients(data: undefined | Nutri[]): Record<string, string> {
const rep = {};

if (data === undefined) {
return {}
}
data.forEach((item) => {
const { id, name, nutrients } = item;

Expand Down Expand Up @@ -44,7 +47,6 @@ export default function useNutrimentTranslations(lc: string) {
axios
.get(`https://world.openfoodfacts.org/cgi/nutrients.pl?lc=${language}`)
.then(({ data }) => {
console.log(data);
setTranslations((p) => ({
...p,
[language]: parseNutrients(data.nutrients),
Expand Down

0 comments on commit 2a1a9cb

Please sign in to comment.