Skip to content

Commit

Permalink
feat: remove initialValue form StreamField
Browse files Browse the repository at this point in the history
  • Loading branch information
schettn authored Sep 8, 2021
1 parent 2e828fa commit 1c60e15
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 63 deletions.
34 changes: 1 addition & 33 deletions examples/my-gatsby-site/src/templates/SamplePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,7 @@ const SamplePage: JaenTemplate = () => {
</Box>
<Box maxW="sm" borderWidth="1px" borderRadius="lg" overflow="hidden">
<h1>StreamField</h1>
<fields.StreamField
fieldName="streamfield1"
initValue={{
0: {
typeName: 'TestBlock',
fields: {
body: {
_type: 'TextBlock',
text:
'<p>This should resolve the window is not defined error and you should be able to build your code successfully. If you need more information regarding this, check the Gatsby documentation on Debugging HTML Builds.</p>'
},
body2: {
_type: 'TextBlock',
text: '<p>02</p>'
}
}
},
1: {
typeName: 'TestBlock',
fields: {
body: {
_type: 'TextBlock',
text: '<p>11</p>'
},
body2: {
_type: 'TextBlock',
text: '<p>12</p>'
}
}
}
}}
blocks={[TestBlock]}
/>
<fields.StreamField fieldName="streamfield2" blocks={[TestBlock]} />
</Box>
<Box maxW="sm" borderWidth="1px" borderRadius="lg" overflow="hidden">
<h1>TextField</h1>
Expand Down
10 changes: 0 additions & 10 deletions packages/jaen-pages/src/containers/blocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,8 @@ export function prepareBlocks<T>(
Block.BlockFields[blockFieldName]

if (fieldOptions.block) {
const value = fieldOptions.initValue?.fields[blockFieldName as string]

let initValue = Block.defaultValues[blockFieldName]

if (value?._type === 'TextBlock') {
initValue = value.text as any
} else if (value?._type === 'ImageBlock') {
initValue = value as any
}

const ConfiguredField = (
<Field
{...fieldOptions}
Expand All @@ -84,7 +76,6 @@ export const BlockItem = React.memo(
block,
height,
width,
initValue,
position,
onDelete,
isEditing
Expand All @@ -95,7 +86,6 @@ export const BlockItem = React.memo(
streamFieldWidth={width}
values={prepareBlocks(Block, {
fieldName,
initValue,
block
})}
/>
Expand Down
20 changes: 6 additions & 14 deletions packages/jaen-pages/src/containers/fields/StreamField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,23 @@ import {
import {merge} from '@common/utils'
import {BlockItem, GenericBC, prepareBlocks} from '@containers/blocks'
import {useTemplate} from '@contexts/template'
import {RevertCSSWrapper} from '@snek-at/jaen'
import {SFWrapper} from '@snek-at/jaen-shared-ui'
import {RevertCSSWrapper} from '@src/../../jaen/src'
import {BlocksField} from '@src/types'
import {useAppDispatch, useAppSelector, useAppState} from '@store/index'
import {pageFieldBlocksSelector} from '@store/selectors/pages'
import {withRedux} from '@store/withRedux'
import React, {useEffect, useState, useRef, useMemo} from 'react'
import {useCallback} from 'react'

import {InitValueType} from './types'

type StreamFieldProps = {
fieldName: string
initValue: InitValueType
blocks: GenericBC[]
reverseOrder?: boolean
}

const StreamField: React.FC<StreamFieldProps> = ({
fieldName,
initValue,
blocks,
reverseOrder
}) => {
Expand Down Expand Up @@ -82,17 +78,13 @@ const StreamField: React.FC<StreamFieldProps> = ({

const visibleBlocks = useMemo(
() =>
merge(
contextValue || initValue,
SFBlocks,
value => value.deleted
) as typeof SFBlocks,
[initValue, SFBlocks]
merge(contextValue, SFBlocks, value => value.deleted) as typeof SFBlocks,
[contextValue, SFBlocks]
)

const allBlocks = useMemo(
() => merge(contextValue || initValue, SFBlocks) as typeof SFBlocks,
[initValue, SFBlocks]
() => merge(contextValue, SFBlocks) as typeof SFBlocks,
[contextValue, SFBlocks]
)

const visibleBlocksKeys = Object.keys(visibleBlocks).sort(
Expand Down Expand Up @@ -142,7 +134,7 @@ const StreamField: React.FC<StreamFieldProps> = ({
}
}
// check if position is in contextValue or initValue, if not, unregister it instead of deleting it
if (position in contextValue || initValue) {
if (position in contextValue) {
dispatch(deletePageField(payload))
} else {
dispatch(unregisterPageField(payload))
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions packages/jaen-pages/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import {IGatsbyImageData} from 'gatsby-plugin-image'
import React from 'react'

import {InitValueType} from './containers/fields/StreamField/types'

export type JaenTemplate = React.FC & {TemplateName: string}

export type StreamBlockIdentifier = {
initValue: InitValueType['string']
fieldName: string
block?: {typeName: string; position: number}
}
Expand Down

0 comments on commit 1c60e15

Please sign in to comment.