Skip to content

Commit

Permalink
fix(fields): remove WA, add block registration and fix missing block …
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
schettn authored Sep 14, 2021
1 parent eccd3a7 commit fb4c2df
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
12 changes: 10 additions & 2 deletions examples/my-gatsby-site/jaen-pages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand All @@ -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"
}
]
},
Expand Down
12 changes: 6 additions & 6 deletions packages/jaen-pages/src/containers/fields/ChoiceField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ const ChoiceField: React.FC<ChoiceFieldProps> = ({
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

Expand All @@ -80,11 +81,10 @@ const ChoiceField: React.FC<ChoiceFieldProps> = ({
: 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const ImageField: React.FC<ImageFieldProps> = ({
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)
Expand All @@ -50,11 +50,10 @@ const ImageField: React.FC<ImageFieldProps> = ({
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
Expand Down
12 changes: 6 additions & 6 deletions packages/jaen-pages/src/containers/fields/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ interface TextFieldProps extends FieldIdentifier {
const TextField: React.FC<TextFieldProps> = ({
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}))

Expand All @@ -58,11 +59,10 @@ const TextField: React.FC<TextFieldProps> = ({
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
Expand Down
4 changes: 4 additions & 0 deletions packages/jaen-pages/src/contexts/cms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ export const CMSProvider: React.FC<CMSProviderType> = ({
id {
fieldName
pageId
block {
fieldName
position
}
}
file {
childImageSharp {
Expand Down
2 changes: 1 addition & 1 deletion packages/jaen-pages/src/tools/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fb4c2df

Please sign in to comment.