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

feat: Nutrition Game #70 #114

Draft
wants to merge 33 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c7043b9
bug: shortCut issue solved
Shmigolk Jul 27, 2022
9c92737
UI: add settings icon and moved links in NavBar to the right
Shmigolk Jul 27, 2022
d0149e2
UI: align pictures and add gap
Shmigolk Jul 27, 2022
4529d8b
UI: add settings icon and moved links in NavBar to the right
Shmigolk Jul 27, 2022
0df05c0
UI: align pictures and add gap
Shmigolk Jul 28, 2022
c89e1f2
UI: create table component
Shmigolk Jul 29, 2022
3a5bd0d
UI: add media queueueueueueury
Shmigolk Jul 29, 2022
9412b2c
fix: add controlled input
Shmigolk Jul 31, 2022
d36a119
fix: add controlled input
Shmigolk Jul 31, 2022
ce50230
Merge remote-tracking branch 'upstream/master' into nutrition
Shmigolk Aug 1, 2022
93d4b4e
feature: change the code of additional nutr
Shmigolk Aug 1, 2022
fe21b02
feature: implement adding nutriments to the table
Shmigolk Aug 1, 2022
a674dfd
feature: implement adding nutriments to the table and removing them f…
Shmigolk Aug 1, 2022
630f0ef
feature: add delete icons and implement deleting nutriments from the …
Shmigolk Aug 1, 2022
257bd35
feature: add Title and the description of the product (hardcoded)
Shmigolk Aug 1, 2022
cb1e486
feature: get data from API. Display picture
Shmigolk Aug 2, 2022
ecd4f07
feature: make clickHandler shorter
Shmigolk Aug 2, 2022
fb90329
feature: move nutriments arraus out of the component. Refresh the tab…
Shmigolk Aug 3, 2022
b952bc4
feature: replace useEffect with React.useEffect
Shmigolk Aug 3, 2022
7d36892
feature: restructure Table component. Make TableRowComp
Shmigolk Aug 9, 2022
b179515
feature: now delete icon toggle display property
Shmigolk Aug 9, 2022
cb6e2c2
feature: now delete icon toggle display property
Shmigolk Aug 9, 2022
089d9df
Merge remote-tracking branch 'origin/master' into nutrition
alexfauquette Aug 9, 2022
79d4643
remove unused import
alexfauquette Aug 9, 2022
9b873e3
feature: now i18n works into the table
Shmigolk Aug 11, 2022
10f5d56
Merge branch 'master' into nutrition
Shmigolk Aug 11, 2022
0add0f0
feature: add keys to common json and en json
Shmigolk Aug 11, 2022
93e7719
Merge remote-tracking branch 'origin/nutrition' into nutrition
Shmigolk Aug 11, 2022
79764f1
feature: display correct properties in select elements
Shmigolk Aug 12, 2022
ae0bffe
feature: on change is working
Shmigolk Aug 12, 2022
30be061
UI: fixed width problem
Shmigolk Aug 13, 2022
7214c83
UI: change the order of elements of productCard and add temporary bor…
Shmigolk Aug 13, 2022
d19a758
bug: fix onChange handler bug
Shmigolk Aug 13, 2022
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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
"dependencies": {
"@emotion/react": "^11.9.0",
"@emotion/styled": "^11.8.1",
"@material-ui/system": "^4.12.2",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it useful to have this package? I see that it is kind of used in pages/nutrition/index.jsx but seems to be importing a non used flexbox (what would be the advantage of using that flexbox compared to css defined flexbox?)

"@mui/icons-material": "^5.8.4",
"@mui/lab": "^5.0.0-alpha.89",
"@mui/material": "^5.8.7",
"@mui/x-data-grid": "^5.13.0",
"axios": "^0.27.2",
"clsx": "^1.2.1",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this package used?

"i18next": "^21.8.13",
"lodash.isequal": "^4.5.0",
"react": "^18.2.0",
Expand All @@ -19,6 +21,7 @@
"react-medium-image-zoom": "^4.4.3",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"react-virtualized": "^9.22.3",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this being used somewhere?

"web-vitals": "^2.1.4"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
NotFoundPage,
NutriscoreValidator,
Home,
Nutrition,
} from "./pages";
import ResponsiveAppBar from "./components/ResponsiveAppBar";
import DevModeContext from "./contexts/devMode";
Expand Down Expand Up @@ -62,6 +63,7 @@ export default function App() {
<Route path="/settings" element={<SettingsPage />} />
<Route path="/questions" element={<QuestionsPage />} />
<Route path="/insights" element={<InsightsPage />} />
<Route path="/nutrition" element={<Nutrition />} />
<Route path="*" element={<NotFoundPage />} />

<Route path="/nutriscore" element={<NutriscoreValidator />} />
Expand Down
11 changes: 4 additions & 7 deletions src/components/ResponsiveAppBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const pages = [
{ url: "eco-score", translationKey: "menu.eco-score" },
{ translationKey: "menu.manage" },
{ url: "insights", translationKey: "menu.insights" },
// { url: "settings", translationKey: "menu.settings" },
alexfauquette marked this conversation as resolved.
Show resolved Hide resolved
{ url: "nutrition", translationKey: "menu.nutrition" },
];

const ResponsiveAppBar = () => {
Expand Down Expand Up @@ -156,12 +156,9 @@ const ResponsiveAppBar = () => {
component="a"
href="/"
sx={{
mr: 2,
fontFamily: "monospace",
fontWeight: 700,
letterSpacing: ".3rem",
color: "inherit",
textDecoration: "none",
display: { xs: "none", md: "flex" },
flexDirection: "row",
alignItems: "baseline",
}}
Shmigolk marked this conversation as resolved.
Show resolved Hide resolved
>
Hunger Games
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"insights": "Insights",
"nutritions": "Nutrition facts",
"settings": "Settings",
"eco-score": "Eco-score"
"eco-score": "Eco-score",
"nutrition": "nutrition"
Shmigolk marked this conversation as resolved.
Show resolved Hide resolved
},
"insights": {
"insights": "Insights",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"insights": "Insights",
"nutritions": "Nutrition facts",
"settings": "Settings",
"eco-score": "Eco-score"
"eco-score": "Eco-score",
"nutrition": "nutrition"
Shmigolk marked this conversation as resolved.
Show resolved Hide resolved
},
"insights": {
"insights": "Insights",
Expand Down
1 change: 1 addition & 0 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { default as LogoUpdatePage } from "./logos/LogoUpdate";

// experimental
export { default as NutriscoreValidator } from "./nutriscoreValidator";
export { default as Nutrition } from "./nutrition"
26 changes: 26 additions & 0 deletions src/pages/nutrition/additionalNutritions.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as React from 'react';
import TextField from '@mui/material/TextField';
import Autocomplete from '@mui/material/Autocomplete';

export default function AdditionalNutriments({options, setNutriments, setAdditionalNutriments}) {
return (
<Autocomplete
disablePortal
id="combo-box-demo"
options={options}
sx={{ width: 300 }}
onChange={(event) => {
const nutrIndex = event.target.dataset.optionIndex
if (nutrIndex) {
setNutriments(prev => [...prev, options[nutrIndex]])
setAdditionalNutriments(prev => prev.filter(elem => elem !== options[nutrIndex]))
console.log(options)
Shmigolk marked this conversation as resolved.
Show resolved Hide resolved
}

}}
renderInput={(params) => <TextField {...params} label="Nutrition" />}
/>
);
}

// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

23 changes: 23 additions & 0 deletions src/pages/nutrition/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from "react";
import NutritionTable from "./table";
import ImageTable from "./picture";
import { Box } from "@mui/material";
import { flexbox } from "@material-ui/system";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { flexbox } from "@material-ui/system";

imported but not used. Normally when you do yarn start in the console, you should see some warnings about lines where you import or define variables you do not use. You should remove them to get a cleaner code.


Here is what I get when I run yarn start from your branch

image


export default function Nutrition() {
return (
<Box display={"flex"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<Box display={"flex"}
<Box display="flex"

Some points which will be useful for tech interview about React (JSX syntax to be precise):

  • By default props are provided as follow propName={propValue}
  • You do not need the {} for sting. FOr example propName={"some text"} is same as propName="some text"
  • A prop alone is equivalent to true. For example <Component isOpen /> is the same as <Component isOpen={true} />.

flexDirection={{ xs: "column", md: "row" }}
gap={2}
sx={{
width: 1,
height: 1,
alignItems: "center",
justifyContent: "center",
padding: 4
}}>
<ImageTable />
<NutritionTable />
</Box>
);
}
43 changes: 43 additions & 0 deletions src/pages/nutrition/picture.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Zoom from "react-medium-image-zoom";
import Button from "@mui/material/Button";
import DeleteIcon from "@mui/icons-material/Delete";

export default function ImageTable() {
return (
<Box flexGrow={1}
flexShrink={1}
sx={{
maxWidth: '380px',

}}>
<Zoom wrapStyle={{ height: "100%" }}>
<img
// TODO: use getFullSizeImage when the zoom is activated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to have an issue linked to this TODO, that way we don't forget about it :)

// src={getFullSizeImage(question.source_image_url)}
src={"https://static.openfoodfacts.org/images/image-placeholder.png"}
alt=""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if possible please add the alt description

style={{ maxWidth: "100%", maxHeight: "100%" }}
/>
</Zoom>
<Box display={"flex"} flexDirection={"row"} Gap={'.5em'}>
<Button
color="secondary"
variant="contained"
size="large"
sx={{ display: "flex", flexDirection: "column", flexGrow: 1, width: '47%' }}
>
SKIP
</Button>
<Button
color="success"
variant="contained"
size="large"
sx={{ display: "flex", flexDirection: "column", flexGrow: 1, width: '47%' }}
>
VALIDATE
</Button>
</Box>
</Box>)
}
36 changes: 36 additions & 0 deletions src/pages/nutrition/selectComp.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from 'react';
import InputLabel from '@mui/material/InputLabel';
import MenuItem from '@mui/material/MenuItem';
import FormControl from '@mui/material/FormControl';
import Select, { SelectChangeEvent } from '@mui/material/Select';

export default function SelectAutoWidth({}) {
const [age, setAge] = React.useState('');

const handleChange = (event: SelectChangeEvent) => {
setAge(event.target.value);
};

return (
<div>
<FormControl sx={{ m: 1, minWidth: 80, margin: '0' }}>
<InputLabel id="demo-simple-select-autowidth-label">Unit</InputLabel>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these ids could be improved, what about something like 'dropdown-unit-label' ?... better to use something as specific as we can or we could end up having multiple similar ids

<Select
labelId="demo-simple-select-autowidth-label"
id="demo-simple-select-autowidth"
value={age}
onChange={handleChange}
autoWidth
label="Age"
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>g</MenuItem>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think it would be nice to somewhere something that gives you an idea of what these values (10, 21, 22) actually mean. What about using an object outside of this component, something like const weightUnits = {g:10, mg:21, kg:22} (please note that I don't really understand what those values refer to and where do they come from). It is better to have some auto documentation rather than having some unkown values

<MenuItem value={21}>mg</MenuItem>
<MenuItem value={22}>kg</MenuItem>
</Select>
</FormControl>
</div>
);
}
160 changes: 160 additions & 0 deletions src/pages/nutrition/table.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import * as React from "react";
import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import TextField from "@mui/material/TextField";
import SelectAutoWidth from "./selectComp";
import { Box } from "@mui/material";
import Checkbox from "@mui/material/Checkbox";
import AdditionalNutriments from "./additionalNutritions";

function createData(
label, property, unit
) {
return { label, property, unit };
}

export default function NutritionTable() {

const [nutriments, setNutriments] = React.useState([
{
off_nutriment_id: "energy_kj",
label: "Energie (kJ)",
value: "",
unit: "null",
quantification: "=",
robotoffPrediction: null
},
{
off_nutriment_id: "energy_kcal",
label: "Protein",
value: "",
unit: "null",
quantification: "<",
robotoffPrediction: null
},
{
off_nutriment_id: "energy_kcal",
label: "Shugar",
value: "",
unit: "null",
quantification: "<",
robotoffPrediction: null
},
{
off_nutriment_id: "energy_kcal",
label: "Fat",
value: "",
unit: null,
quantification: "<",
robotoffPrediction: null
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this values could live outside of the component .... const NUTRIMENTS = [ ... ] and just used in the useState const [nutriments, setNutriments] = React.useState(NUTRIMENTS). That way the component is a bit more clean

])

const [additionalNutriments, setAdditionalNutriments] = React.useState([
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing here

off_nutriment_id: "energy_kj",
label: "Energie (kJ)",
value: "",
unit: "null",
quantification: "=",
robotoffPrediction: null
},
{
off_nutriment_id: "energy_kcal",
label: "Protein",
value: "",
unit: "null",
quantification: "<",
robotoffPrediction: null
},
{
off_nutriment_id: "energy_kcal",
label: "Shugar",
value: "",
unit: "null",
quantification: "<",
robotoffPrediction: null
},
{
off_nutriment_id: "energy_kcal",
label: "Fat",
value: "",
unit: null,
quantification: "<",
robotoffPrediction: null
}
])

function onchangeHandler(e) {
const {value, name} = e.target
setNutriments(prevState => prevState.map(
nutr => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I prefer not to use abbreviations as they quickly become difficult to read and understand, specially for other developers. Plase use a whole word, more typing but better readability. In this cases I tend to use the suffix ..item, something like nutrimentItem that way I understand that it is the thing inside the loop

return name === nutr.label? {...nutr, value} : nutr
}
))
}

const rows = nutriments.map(nutrition => {
return (
createData(<Box sx={{
display: "flex",
alignItems: "center",
flexDirection: "row"
}}><TextField id="outlined-basic"
type={'number'}
label={nutrition.label}
variant="outlined"
sx={{ width: "10rem" }}
value={nutrition.value}
name={nutrition.label}
onChange={onchangeHandler}
/> , <SelectAutoWidth /></Box>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd advice against this kind of implicit closing elements (</Box>) as the chance of forgetting or using it wrongly is relatively high

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plase see the coment below

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume it's a mistake. The </Box> is planned to be before the coma But that's not important.

@Shmigolk I think you get confused by the MUI demo, because it's cells only contain strings/numbers, so they use

<TableCell>{row.thValueToDisplay}</TabCell>

In our case, we want to render inputs which is a bit more complex.

I propose you the following strategy:
create a component NutritionTableRow as follow:

const NutritionTableRow = ({ nutrimentData, updateValue, updateUnit, updateQuantificator }) => {
	const onNutrimentValueChange = (event) => {updateValue(event.target.value)}
	const onUnitChange = () => {...}
	const onQuantificatorChange = () => {...}
	
	const {label, value, quantification, unit} = nutrimentData
	
	return <TableRow>
		<TableCell>
			<TextField select value={quantification} onChange={onQuantificatorChange}>
				<MenuItem value="=">=</MenuItem>
				<MenuItem value=">">></MenuItem>
				<MenuItem value="<"><</MenuItem>
			</TextField>
		</TableCell>
		<TableCell>
			<TextField label={label} value={value} onChange={onNutrimentValueChange} />
		</TableCell>
		//  Same for the unit
	<TableRow>
}

Then you will be able to simplify your table content as follow:

	nutriments.map(nutriment => <NutritionTableRow nutrimentData={nutriment}  updateValue={updateUnit(nutriment.off_nutriment_id)}/>)

The trickiest part being updateValue which is a function returning a function.

const updateValue = (nutrimentId) => (newValue) => {
	// We get the index of the nutriment we want to update
	const indexToModify = nutriments.findIndex(nutriment => nutriment.off_nutriment_id === nutrimentId)
	
	// early return if not element correspond to this id
	if(indexToModify < 0){ return }
	
	// We update the value to the nutriment
	const newNutriment = {...nutriments[indexToModify], value: newValue }
	setNutriments([...nutriments.slice(0, indexToModify), newNutriment, ...nutriments.slice(indexToModify+1)])
}

<Checkbox sx={{}} />));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is sx={{}} needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed checkbox and leaved delete icon. Still working on it

});

return (
<Box>
<TableContainer sx={{ margin: 0, maxWidth: "1000px", width: "380px" }}>
<Table aria-label="simple table">
<TableHead>
<TableRow>
<TableCell sx={{
maxWidth: "8em",
fontSize: "large",
fontWeight: "bold"
}}>nutrition.table.value</TableCell>
<TableCell align="left"
sx={{}}>isPresent</TableCell>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is sx={{}} needed?

</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about using the map directly?, I mean, instead of first map to createData() and using that result

<TableRow
key={row.label}
sx={{ "&:last-child td, &:last-child th": { border: 0 } }}
>
<TableCell component="th"
scope="row"
sx={{ width: 10 }}
>
{row.label}
</TableCell>
<TableCell align="left"
sx={{ width: "1rem" }}>{row.property}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
<AdditionalNutriments
options = {additionalNutriments}
setNutriments={setNutriments}
setAdditionalNutriments={setAdditionalNutriments}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are trying to keep up to date 2 different arrays which is error pron. As soon as you forget an edge case, the two arrays will not be sync and it's hard to debug

I propose to keep the setNutriments that could be renamed addNutrimentToTheTable
Replace the setAdditionalNutriments by missingNutriments which would be computed as follow:

const missingNutriments = offNutriments.filter(nutrimentKey => 
	data.every(element) => element.off_nutriment_id !== nutrimentKey) 
)

with offNutriments the list of all the nutrients available on openfoodfact

This would replace options and setAdditionalNutriments

/>
</Box>
);
}
Loading