Fix for broken custom views and new entities

This commit is contained in:
francesco
2021-08-14 21:46:13 +02:00
parent da5da26d96
commit 91d1b1f843
5 changed files with 42 additions and 20 deletions

View File

@@ -21,6 +21,8 @@ import { blogSchema } from "../schemas/blog_schema";
export function BlogEntryPreview({ modifiedValues }: EntityCustomViewParams<InferSchemaType<typeof blogSchema>>) {
console.log("BlogEntryPreview", modifiedValues);
const [headerUrl, setHeaderUrl] = useState<string | undefined>();
useEffect(() => {
if (modifiedValues?.header_image) {

View File

@@ -1,10 +1,11 @@
import React from "react";
import { Box, Button } from "@material-ui/core";
import { Entity, EntityValues, useSnackbarController } from "@camberi/firecms";
import { Product } from "../types";
export function SampleProductsView({ entity, modifiedValues }: {
entity?: Entity<any>;
modifiedValues?: EntityValues<any>;
entity?: Entity<Product>;
modifiedValues?: EntityValues<Product>;
}) {
const snackbarContext = useSnackbarController();
@@ -16,6 +17,19 @@ export function SampleProductsView({ entity, modifiedValues }: {
});
};
const includePropertyValue = (key:string, value: any ): boolean => {
if (key === "related_products")
return false;
return true;
};
const values = modifiedValues ?
Object.entries(modifiedValues)
.filter(([key, value]) => includePropertyValue(key, value))
.map(([key, value]) => ({ [key]: value }))
.reduce((a, b) => ({ ...a, ...b }))
: {};
return (
<Box
display="flex"
@@ -28,7 +42,7 @@ export function SampleProductsView({ entity, modifiedValues }: {
alignItems={"center"}
justifyItems={"center"}>
<Box p={2}>
<Box p={4}>
<p>
This is an example of a custom view added
as a panel to an entity schema.
@@ -36,7 +50,8 @@ export function SampleProductsView({ entity, modifiedValues }: {
<p>
Values in the form:
</p>
<p style={{
{values && <p style={{
color: "#fff",
padding: "8px",
fontSize: ".85em",
@@ -44,8 +59,13 @@ export function SampleProductsView({ entity, modifiedValues }: {
borderRadius: "4px",
backgroundColor: "#4e482f"
}}>
{modifiedValues && JSON.stringify(modifiedValues, null, 2)}
</p>
{JSON.stringify(values, null, 2)}
</p>}
<small>
Note that "Related products" is intentionally excluded from this JSON preview
</small>
</Box>
<Button onClick={onClick} color="primary">

View File

@@ -63,14 +63,14 @@ const categories: EnumValues = {
const sampleView: EntityCustomView = {
path: "sample_custom_view",
name: "Custom view",
builder: ({ schema, entity, modifiedValues }) => (
<SampleProductsView entity={entity} modifiedValues={modifiedValues}/>
)
builder: ({ schema, entity, modifiedValues }) =>
<SampleProductsView entity={entity}
modifiedValues={modifiedValues}/>
};
export const productSchema = buildSchema<Product>({
export const productSchema = buildSchema<any>({
name: "Product",
views: [
sampleView

View File

@@ -138,14 +138,14 @@ export interface EntitySideViewProps<M extends { [Key: string]: any }> {
}
function EntityView<M extends { [Key: string]: any }>({
collectionPath,
entityId,
selectedSubpath,
copy,
permissions,
schema,
subcollections
}: EntitySideViewProps<M>) {
collectionPath,
entityId,
selectedSubpath,
copy,
permissions,
schema,
subcollections
}: EntitySideViewProps<M>) {
const classes = useStylesSide();
@@ -343,7 +343,7 @@ function EntityView<M extends { [Key: string]: any }>({
width={"100%"}
hidden={tabsPosition !== colIndex}>
<ErrorBoundary>
{entity && customView.builder({
{customView.builder({
schema,
entity,
modifiedValues: isModified ? modifiedValues : entity?.values

View File

@@ -12,7 +12,7 @@ type MarkdownProps = {
export default function Markdown({
source
}: MarkdownProps) {
return <MarkdownPreview source={source}
return <MarkdownPreview source={typeof source === "string" ? source : ""}
style={{
fontSize: "inherit",
lineHeight: "inherit",