|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import { |
| 4 | + buildCollection, |
| 5 | + Entity, |
| 6 | + EntityCollectionView, |
| 7 | + EntityReference, |
| 8 | + ReferenceWidget, |
| 9 | + useAuthController, |
| 10 | + useReferenceDialog, |
| 11 | + useSelectionController, |
| 12 | + useSideEntityController, |
| 13 | + useSnackbarController |
| 14 | +} from "firecms"; |
| 15 | +import { Button, Chip, GitHubIcon, IconButton, Paper, Tooltip, Typography, } from "@firecms/ui"; |
| 16 | +import { Product } from "../types"; |
| 17 | +import { usersCollection } from "../collections/users_collection"; |
| 18 | + |
| 19 | +/** |
| 20 | + * Sample CMS view not bound to a collection, customizable by the developer |
| 21 | + * @constructor |
| 22 | + */ |
| 23 | +export function ExampleCMSView() { |
| 24 | + |
| 25 | + // hook to display custom snackbars |
| 26 | + const snackbarController = useSnackbarController(); |
| 27 | + |
| 28 | + const selectionController = useSelectionController(); |
| 29 | + |
| 30 | + const [sampleSelectedProduct, setSampleSelectedProduct] = React.useState<EntityReference | null>(); |
| 31 | + |
| 32 | + // hook to open the side dialog that shows the entity forms |
| 33 | + const sideEntityController = useSideEntityController(); |
| 34 | + |
| 35 | + // hook to do operations related to authentication |
| 36 | + const authController = useAuthController(); |
| 37 | + |
| 38 | + // hook to open a reference dialog |
| 39 | + const referenceDialog = useReferenceDialog({ |
| 40 | + path: "products", |
| 41 | + onSingleEntitySelected(entity: Entity<Product> | null) { |
| 42 | + snackbarController.open({ |
| 43 | + type: "success", |
| 44 | + message: "Selected " + entity?.values.name |
| 45 | + }) |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + const customProductCollection = buildCollection({ |
| 50 | + id: "custom_product", |
| 51 | + path: "custom_product", |
| 52 | + name: "Custom products", |
| 53 | + properties: { |
| 54 | + name: { |
| 55 | + name: "Name", |
| 56 | + validation: { required: true }, |
| 57 | + dataType: "string" |
| 58 | + }, |
| 59 | + very_custom_field: { |
| 60 | + name: "Very custom field", |
| 61 | + dataType: "string" |
| 62 | + } |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + const githubLink = ( |
| 67 | + <Tooltip |
| 68 | + title="Get the source code of this example view"> |
| 69 | + <IconButton |
| 70 | + href={"https://github.com/firecmsco/firecms/blob/main/examples/example_v3/src/views/ExampleCMSView.tsx"} |
| 71 | + rel="noopener noreferrer" |
| 72 | + target="_blank" |
| 73 | + component={"a"} |
| 74 | + size="large"> |
| 75 | + <GitHubIcon/> |
| 76 | + </IconButton> |
| 77 | + </Tooltip> |
| 78 | + ); |
| 79 | + |
| 80 | + return ( |
| 81 | + <div className="flex h-full"> |
| 82 | + <div className="m-auto flex flex-col items-center max-w-4xl"> |
| 83 | + |
| 84 | + <div className="flex flex-col gap-12 items-start"> |
| 85 | + |
| 86 | + <div className="mt-24"> |
| 87 | + <h4 className="font-bold text-xl mb-4"> |
| 88 | + This is an example of an additional view |
| 89 | + </h4> |
| 90 | + <p> |
| 91 | + {authController.user |
| 92 | + ? <>Logged in as <Chip>{authController.user.displayName}</Chip></> |
| 93 | + : <>You are not logged in</>} |
| 94 | + </p> |
| 95 | + </div> |
| 96 | + |
| 97 | + <div className="grid grid-cols-1 sm:grid-cols-3 gap-2"> |
| 98 | + <Paper className={"w-full flex flex-col p-4 items-start"}> |
| 99 | + <p className="mb-4 flex-grow"> |
| 100 | + Use this button to select an entity under the |
| 101 | + path <code>products</code> programmatically |
| 102 | + </p> |
| 103 | + <Button |
| 104 | + variant={"outlined"} |
| 105 | + size={"small"} |
| 106 | + onClick={referenceDialog.open}> |
| 107 | + Test reference dialog |
| 108 | + </Button> |
| 109 | + </Paper> |
| 110 | + |
| 111 | + <Paper className="w-full flex flex-col p-4 items-start"> |
| 112 | + <p className="mb-4 flex-grow"> |
| 113 | + Use this button to open a snackbar |
| 114 | + </p> |
| 115 | + <Button |
| 116 | + variant={"outlined"} |
| 117 | + size={"small"} |
| 118 | + onClick={() => snackbarController.open({ |
| 119 | + type: "success", |
| 120 | + message: "This is pretty cool" |
| 121 | + })}> |
| 122 | + Test snackbar |
| 123 | + </Button> |
| 124 | + </Paper> |
| 125 | + |
| 126 | + <Paper className="w-full flex flex-col p-4 items-start"> |
| 127 | + <p className="mb-4 flex-grow"> |
| 128 | + Use this button to open an entity in a custom path with a custom schema |
| 129 | + </p> |
| 130 | + <Button |
| 131 | + size={"small"} |
| 132 | + variant={"outlined"} |
| 133 | + onClick={() => sideEntityController.open({ |
| 134 | + entityId: "B003WT1622", |
| 135 | + path: "/products-test", |
| 136 | + collection: customProductCollection, |
| 137 | + width: 1000 |
| 138 | + })}> |
| 139 | + Open custom entity |
| 140 | + </Button> |
| 141 | + </Paper> |
| 142 | + </div> |
| 143 | + |
| 144 | + <div className={"w-full"}> |
| 145 | + <Typography className={"mb-4"}> |
| 146 | + You can include reference widgets in your views: |
| 147 | + </Typography> |
| 148 | + <ReferenceWidget |
| 149 | + name={"Sample reference widget"} |
| 150 | + value={sampleSelectedProduct ?? null} |
| 151 | + onReferenceSelected={({ reference, entity }) => setSampleSelectedProduct(reference)} |
| 152 | + path={"products"} |
| 153 | + size={"small"} |
| 154 | + className={"w-full"}/> |
| 155 | + </div> |
| 156 | + |
| 157 | + <div className="w-full"> |
| 158 | + <p className="mb-4"> |
| 159 | + You can include full entity collections in your views: |
| 160 | + </p> |
| 161 | + <Paper |
| 162 | + className={"h-[400px]"}> |
| 163 | + <EntityCollectionView {...usersCollection} |
| 164 | + fullPath={"users"} |
| 165 | + selectionController={selectionController}/> |
| 166 | + </Paper> |
| 167 | + </div> |
| 168 | + |
| 169 | + <div className="mt-auto"> |
| 170 | + {githubLink} |
| 171 | + </div> |
| 172 | + |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + </div> |
| 176 | + ); |
| 177 | +} |
0 commit comments