Skip to content

Instantly share code, notes, and snippets.

@Steven24K
Created September 3, 2021 11:43
Show Gist options
  • Save Steven24K/53cf38985313c880e27fc0c7feb889d2 to your computer and use it in GitHub Desktop.
Save Steven24K/53cf38985313c880e27fc0c7feb889d2 to your computer and use it in GitHub Desktop.
A simple utility function to set the state of a custom block
const setBlockState = <K extends keyof BLOCKPROPS>(prop: K, newValue: BLOCKPROPS[K]) => (s: CMSState<customBlockData, extra_images, PublicSite_CustomPageData>): CMSState<customBlockData, extra_images, PublicSite_CustomPageData> => {
if (!s.blocks.has(props.block_index)) return s
let current_block = s.blocks.get(props.block_index) as any
if (current_block.kind != 'BLOCK_NAME') return s
current_block = ({
...current_block,
value: {
...current_block.value,
fst: {
...current_block.value.fst,
[prop]: newValue
},
}
})
return ({ ...s, blocks: s.blocks.set(props.block_index, current_block) })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment