-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
559ff2e
commit d250b49
Showing
2 changed files
with
101 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import * as React from "react"; | ||
import axios from "axios"; | ||
|
||
import Box from "@mui/material/Box"; | ||
import Card from "@mui/material/Card"; | ||
import CardContent from "@mui/material/CardContent"; | ||
import Typography from "@mui/material/Typography"; | ||
import Stack from "@mui/material/Stack"; | ||
|
||
const fetchUserData = async (userName) => { | ||
console.log(userName); | ||
|
||
const editorPromise = axios | ||
.get(`https://world.openfoodfacts.org/editor/${userName}.json?fields=count`) | ||
.then(({ data }) => { | ||
console.log({ data }); | ||
return data?.count; | ||
}) | ||
.catch(() => undefined); | ||
const contributorPromise = axios | ||
.get( | ||
`https://world.openfoodfacts.org/contributor/${userName}.json?fields=count` | ||
) | ||
.then(({ data }) => { | ||
console.log({ data }); | ||
return data?.count; | ||
}) | ||
.catch(() => undefined); | ||
const photographerPromise = axios | ||
.get( | ||
`https://world.openfoodfacts.org/photographer/${userName}.json?fields=count` | ||
) | ||
.then(({ data }) => { | ||
console.log({ data }); | ||
return data?.count; | ||
}) | ||
.catch(() => undefined); | ||
|
||
const [editorCount, contributorCount, photographerCount] = await Promise.all([ | ||
editorPromise, | ||
contributorPromise, | ||
photographerPromise, | ||
]); | ||
|
||
console.log(editorCount, contributorCount, photographerCount); | ||
return { editorCount, contributorCount, photographerCount }; | ||
}; | ||
|
||
const CountCard = (props) => { | ||
const { translationKey, value } = props; | ||
|
||
return ( | ||
<Card sx={{ width: 300 }} elevation={3}> | ||
<CardContent> | ||
<Typography sx={{ fontSize: 16 }} color="text.secondary" gutterBottom> | ||
{translationKey} | ||
</Typography> | ||
<Typography variant="h3" color="text.primary" component="div"> | ||
{value.toLocaleString()} | ||
</Typography> | ||
</CardContent> | ||
</Card> | ||
); | ||
}; | ||
|
||
const UserData = ({ userName }) => { | ||
const [counts, setCounts] = React.useState({}); | ||
|
||
React.useEffect(() => { | ||
fetchUserData(userName) | ||
.then((counts) => { | ||
console.log({ counts }); | ||
setCounts(counts); | ||
}) | ||
.catch(() => {}); | ||
}, [userName]); | ||
console.log(counts); | ||
return ( | ||
<Box sx={{ p: 2, mb: 10 }}> | ||
<Typography component="h3" variant="h5" sx={{ pb: 3 }}> | ||
{userName} statistics: | ||
</Typography> | ||
<Stack direction={{ xs: "column", sm: "row" }} spacing={2}> | ||
{Object.keys(counts).map((countType) => ( | ||
<CountCard | ||
key={countType} | ||
translationKey={countType} | ||
value={counts[countType]} | ||
/> | ||
))} | ||
</Stack> | ||
</Box> | ||
); | ||
}; | ||
export default UserData; |
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