forked from openfoodfacts/hunger-games
-
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.
Play with some taxonomy explorer (openfoodfacts#1084)
- Loading branch information
1 parent
80fca77
commit 737f0f5
Showing
6 changed files
with
327 additions
and
15 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import axios, { AxiosResponse } from "axios"; | ||
|
||
type TagType = "categories"; | ||
|
||
export type TaxonomyItem = { | ||
id: string; | ||
text: string; | ||
taxonomy_name: string; | ||
}; | ||
|
||
export type GetTaxonomyParams = { | ||
/** | ||
* @default "categories" | ||
*/ | ||
categories?: TagType; | ||
/** | ||
* @default ["fr", "en"] | ||
*/ | ||
languages?: string[]; | ||
/** | ||
* The tag | ||
*/ | ||
tag: string; | ||
}; | ||
export default function getTaxonomy( | ||
params: GetTaxonomyParams, | ||
): Promise<AxiosResponse<{ options?: TaxonomyItem[] }>> { | ||
const { categories = "categories", tag, languages = ["fr", "en"] } = params; | ||
|
||
return axios.get( | ||
`https://world.openfoodfacts.org/api/v2/taxonomy?tagtype=${categories}&tags=${tag}&lc=${languages.join( | ||
",", | ||
)}`, | ||
); | ||
} |
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,26 @@ | ||
import * as React from "react"; | ||
|
||
export class ErrorBoundary extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { hasError: false }; | ||
} | ||
|
||
static getDerivedStateFromError(error) { | ||
// Update state so the next render will show the fallback UI. | ||
return { hasError: true }; | ||
} | ||
|
||
componentDidCatch(error, info) { | ||
// Example "componentStack": | ||
// in ComponentThatThrows (created by App) | ||
// in ErrorBoundary (created by App) | ||
// in div (created by App) | ||
// in App | ||
console.log(error, info.componentStack); | ||
} | ||
|
||
render() { | ||
return this.props.children; | ||
} | ||
} |
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,111 @@ | ||
import * as React from "react"; | ||
import { | ||
Box, | ||
Button, | ||
Card, | ||
CardContent, | ||
List, | ||
ListItem, | ||
Typography, | ||
} from "@mui/material"; | ||
|
||
export type CategoryType = { | ||
id: string; | ||
children?: string[]; | ||
parents?: string[]; | ||
name?: { en?: string; fr?: string }; | ||
agribalyse_food_code?: { en: string }; | ||
ciqual_food_code?: { en: string }; | ||
intake24_category_code?: { en: string }; | ||
}; | ||
|
||
export default function CategoryCard(props: { | ||
id: string; | ||
data: CategoryType | "failed" | "loading"; | ||
clickChildren?: (id: string) => void; | ||
clickParent?: (id: string) => void; | ||
goTo?: () => void; | ||
hideParent?: boolean; | ||
hideChildren?: boolean; | ||
}) { | ||
const { id: itemId, data, clickParent, hideChildren, goTo } = props; | ||
|
||
const [showMore, setShowMore] = React.useState(false); | ||
|
||
if (data === "loading") { | ||
return null; | ||
} | ||
|
||
if (data === "failed") { | ||
return ( | ||
<Card sx={{ border: "solid black 1px", mb: 0.5 }}> | ||
<CardContent> | ||
<Box> | ||
<Typography variant="h5">{itemId}</Typography> | ||
<Typography>No data found</Typography> | ||
</Box> | ||
</CardContent> | ||
</Card> | ||
); | ||
} | ||
const { parents = [], children = [], name, id, ...other } = data; | ||
|
||
return ( | ||
<Card sx={{ border: "solid black 1px", mb: 0.5 }}> | ||
<CardContent> | ||
<Box> | ||
<Typography variant="h5"> | ||
{name ? Object.values(name).join("/") : id} | ||
{!!goTo && <Button onClick={() => goTo()}>Go</Button>} | ||
</Typography> | ||
{showMore && ( | ||
<pre style={{ fontSize: 10 }}>{JSON.stringify(other, null, 2)}</pre> | ||
)} | ||
<button onClick={() => setShowMore((p) => !p)}> | ||
{showMore ? "hide data" : "additional info"} | ||
</button> | ||
</Box> | ||
<Box | ||
sx={{ | ||
display: "flex", | ||
flexDirection: "row", | ||
borderTop: "solid black 1px", | ||
pt: 1, | ||
mt: 1, | ||
}} | ||
> | ||
<Box sx={{ flexGrow: 1 }}> | ||
<Typography>parents</Typography> | ||
<List> | ||
{parents.map((parentId) => ( | ||
<ListItem | ||
key={parentId} | ||
onClick={() => clickParent?.(parentId)} | ||
sx={{ p: 0 }} | ||
> | ||
{parentId} | ||
</ListItem> | ||
))} | ||
</List> | ||
</Box> | ||
<Box sx={{ flexGrow: 1 }}> | ||
<Typography>enfants</Typography> | ||
<List> | ||
{children && | ||
!hideChildren && | ||
children.map((childId) => ( | ||
<ListItem | ||
key={childId} | ||
onClick={() => clickParent?.(childId)} | ||
sx={{ p: 0 }} | ||
> | ||
{childId} | ||
</ListItem> | ||
))} | ||
</List> | ||
</Box> | ||
</Box> | ||
</CardContent> | ||
</Card> | ||
); | ||
} |
Oops, something went wrong.