From fb4c2df08b8129b48245238736bd6d767d69f47b Mon Sep 17 00:00:00 2001 From: Nico Schett <52858351+schettn@users.noreply.github.com> Date: Tue, 14 Sep 2021 10:37:24 +0000 Subject: [PATCH] fix(fields): remove WA, add block registration and fix missing block data --- examples/my-gatsby-site/jaen-pages.json | 12 ++++++++++-- .../src/containers/fields/ChoiceField/index.tsx | 12 ++++++------ .../src/containers/fields/ImageField/index.tsx | 7 +++---- .../src/containers/fields/TextField/index.tsx | 12 ++++++------ packages/jaen-pages/src/contexts/cms.tsx | 4 ++++ packages/jaen-pages/src/tools/fields.ts | 2 +- 6 files changed, 30 insertions(+), 19 deletions(-) diff --git a/examples/my-gatsby-site/jaen-pages.json b/examples/my-gatsby-site/jaen-pages.json index 6ddcf717..116ada0d 100644 --- a/examples/my-gatsby-site/jaen-pages.json +++ b/examples/my-gatsby-site/jaen-pages.json @@ -2,8 +2,8 @@ "pages": { "SitePage /30896cb4-b20a-48c5-bcde-740920566241": { "context": { - "createdAt": "2021-09-14T04:17:52.257Z", - "fileUrl": "https://cloudflare-ipfs.com/ipfs/QmStw3KjpJsv1jSWEBVxTjdndCusKXgNr2SfRV62ckhmnj" + "createdAt": "2021-09-14T10:14:21.623Z", + "fileUrl": "https://cloudflare-ipfs.com/ipfs/QmQDMy8MF2esgevUreoLRWEtGVXFMAz5ys1vK8BZtvG24X" }, "migrations": [ { @@ -13,6 +13,14 @@ { "fileUrl": "https://cloudflare-ipfs.com/ipfs/QmPbyHY9aSP9xmryMnMF2M3RrvFVZ8t8ai5kNbprNfPyoc", "createdAt": "2021-09-14T04:17:52.257Z" + }, + { + "fileUrl": "https://cloudflare-ipfs.com/ipfs/QmTiZPiXxHWrLTxVYehE3dv7RiKmL1E8JYffQygxaKgsWC", + "createdAt": "2021-09-14T09:43:48.175Z" + }, + { + "fileUrl": "https://cloudflare-ipfs.com/ipfs/Qmdh4qmwFSefUHVPV7QSQzJsktcFrFAnPRPoXNzqshhXTE", + "createdAt": "2021-09-14T10:14:21.623Z" } ] }, diff --git a/packages/jaen-pages/src/containers/fields/ChoiceField/index.tsx b/packages/jaen-pages/src/containers/fields/ChoiceField/index.tsx index 4d59ce7e..5ec3f417 100644 --- a/packages/jaen-pages/src/containers/fields/ChoiceField/index.tsx +++ b/packages/jaen-pages/src/containers/fields/ChoiceField/index.tsx @@ -47,15 +47,16 @@ const ChoiceField: React.FC = ({ header, onRenderPopover, onRender, - ...field + initValue, + fieldName }) => { const dispatch = useAppDispatch() - let {initValue, fieldName} = field - const {block, updatedFieldName} = useBlock(fieldName) fieldName = updatedFieldName + const field = {initValue, fieldName, block} + const {jaenPageContext} = useTemplate() const pageId = jaenPageContext.id @@ -80,11 +81,10 @@ const ChoiceField: React.FC = ({ : contextValue || initValue || options[0] const onSelect = (option: Option) => { - // TODO: !block is a hack to get around the fact that we don't have a block register - if (!block && !isRegistered && option !== initValue) { + if (!isRegistered && option !== initValue) { register() } - if (!block && option === initValue) { + if (option === initValue) { unregister() } else { let fieldDetails: FieldUpdateDetails diff --git a/packages/jaen-pages/src/containers/fields/ImageField/index.tsx b/packages/jaen-pages/src/containers/fields/ImageField/index.tsx index efea0ac4..9fd794f8 100644 --- a/packages/jaen-pages/src/containers/fields/ImageField/index.tsx +++ b/packages/jaen-pages/src/containers/fields/ImageField/index.tsx @@ -29,9 +29,9 @@ const ImageField: React.FC = ({ fieldName, ...props }) => { - const field = {initValue, fieldName} const {block, updatedFieldName} = useBlock(fieldName) fieldName = updatedFieldName + const field = {initValue, fieldName, block} const dispatch = useAppDispatch() const isEditing = useAppSelector(state => state.options.isEditing) @@ -50,11 +50,10 @@ const ImageField: React.FC = ({ const isRegistered = updatedValue !== undefined const handleOnChange = (data: ImageType) => { - // TODO: !block is a hack to get around the fact that we don't have a block register - if (!block && !isRegistered && data !== initValue) { + if (!isRegistered && data !== initValue) { register() } - if (!block && data === initValue) { + if (data === initValue) { unregister() } else { let fieldDetails: FieldUpdateDetails diff --git a/packages/jaen-pages/src/containers/fields/TextField/index.tsx b/packages/jaen-pages/src/containers/fields/TextField/index.tsx index da023cf5..c359ea05 100644 --- a/packages/jaen-pages/src/containers/fields/TextField/index.tsx +++ b/packages/jaen-pages/src/containers/fields/TextField/index.tsx @@ -27,18 +27,19 @@ interface TextFieldProps extends FieldIdentifier { const TextField: React.FC = ({ rtf = true, toolbar = 'balloon', - ...field + fieldName, + initValue }) => { const dispatch = useAppDispatch() const isEditing = useAppSelector(state => state.options.isEditing) const {jaenPageContext} = useTemplate() const pageId = jaenPageContext.id - let {initValue, fieldName} = field - const {block, updatedFieldName} = useBlock(fieldName) fieldName = updatedFieldName + const field = {initValue, fieldName, block} + const register = () => dispatch(registerPageField({pageId, field})) const unregister = () => dispatch(unregisterPageField({pageId, field})) @@ -58,11 +59,10 @@ const TextField: React.FC = ({ const value = isRegistered ? updatedValue : contextValue || initValue || '' const handleOnChange = (data: string) => { - // TODO: !block is a hack to get around the fact that we don't have a block register - if (!block && !isRegistered && data !== initValue) { + if (!isRegistered && data !== initValue) { register() } - if (!block && data === initValue) { + if (data === initValue) { unregister() } else { let fieldDetails: FieldUpdateDetails diff --git a/packages/jaen-pages/src/contexts/cms.tsx b/packages/jaen-pages/src/contexts/cms.tsx index 2fa1be35..a461de03 100644 --- a/packages/jaen-pages/src/contexts/cms.tsx +++ b/packages/jaen-pages/src/contexts/cms.tsx @@ -203,6 +203,10 @@ export const CMSProvider: React.FC = ({ id { fieldName pageId + block { + fieldName + position + } } file { childImageSharp { diff --git a/packages/jaen-pages/src/tools/fields.ts b/packages/jaen-pages/src/tools/fields.ts index 8c9587eb..229490db 100644 --- a/packages/jaen-pages/src/tools/fields.ts +++ b/packages/jaen-pages/src/tools/fields.ts @@ -5,7 +5,7 @@ export const getFieldContent = ( block: BlockIdentifier | undefined ) => { if (block) { - return (field as BlocksField | undefined)?.blocks?.[block.position] + return (field as BlocksField | undefined)?.blocks?.[block.position]?.fields?.[block.blockFieldName] } return (field as PlainField | undefined)?.content