|
| 1 | +import React from "react"; |
| 2 | +import { useSecurity } from "@webiny/app-serverless-cms"; |
| 3 | +import { CmsContentFormRendererPlugin } from "@webiny/app-headless-cms"; |
| 4 | +import { Grid, Cell } from "@webiny/ui/Grid"; |
| 5 | +import { Tabs, Tab } from "@webiny/ui/Tabs"; |
| 6 | +import { Alert } from "@webiny/ui/Alert"; |
| 7 | + |
| 8 | +type CmsContentFormRendererProps = React.ComponentProps<typeof CmsContentFormRendererPlugin>; |
| 9 | + |
| 10 | +const PizzaLayout: CmsContentFormRendererProps["render"] = ({ fields, data }) => { |
| 11 | + // Access security identity. |
| 12 | + const { getPermission } = useSecurity(); |
| 13 | + |
| 14 | + // Get the necessary permission. |
| 15 | + const bakeryPermission = getPermission("bakery"); |
| 16 | + |
| 17 | + // Check if the user has the permission to edit a recipe. |
| 18 | + const canEditRecipe = bakeryPermission && bakeryPermission["canEditRecipe"] === true; |
| 19 | + |
| 20 | + const priceTooLow = data["price"] < 20 && data["numberOfIngredients"] > 6; |
| 21 | + |
| 22 | + return ( |
| 23 | + <Tabs> |
| 24 | + <Tab label="General"> |
| 25 | + {priceTooLow && ( |
| 26 | + <Grid> |
| 27 | + <Cell span={12}> |
| 28 | + <Alert type={"warning"} title={"Please double-check your input"}> |
| 29 | + The price of <strong>{data["price"]}</strong> seems too low for a |
| 30 | + pizza with over <strong>6</strong> ingredients. |
| 31 | + </Alert> |
| 32 | + </Cell> |
| 33 | + </Grid> |
| 34 | + )} |
| 35 | + |
| 36 | + <Grid> |
| 37 | + <Cell span={12}>{fields["name"]}</Cell> |
| 38 | + </Grid> |
| 39 | + <Grid> |
| 40 | + <Cell span={6}>{fields["price"]}</Cell> |
| 41 | + <Cell span={6}>{fields["numberOfIngredients"]}</Cell> |
| 42 | + </Grid> |
| 43 | + </Tab> |
| 44 | + {/* Hide the Recipe tab if the user doesn't have the required permission. */} |
| 45 | + {canEditRecipe && ( |
| 46 | + <Tab label="Recipe"> |
| 47 | + <Grid> |
| 48 | + <Cell span={12}>{fields["recipe"]}</Cell> |
| 49 | + </Grid> |
| 50 | + </Tab> |
| 51 | + )} |
| 52 | + <Tab label="History"> |
| 53 | + <Grid> |
| 54 | + <Cell span={12}>{fields["history"]}</Cell> |
| 55 | + </Grid> |
| 56 | + </Tab> |
| 57 | + </Tabs> |
| 58 | + ); |
| 59 | +}; |
| 60 | + |
| 61 | +export const Extension = () => { |
| 62 | + return ( |
| 63 | + <> |
| 64 | + <CmsContentFormRendererPlugin modelId="pizza" render={PizzaLayout} /> |
| 65 | + </> |
| 66 | + ); |
| 67 | +}; |
0 commit comments