Skip to content

Commit

Permalink
feat(shape-7911): add new state as context in the demo, replace any b…
Browse files Browse the repository at this point in the history
…y permissions types, add ModalSize type and ModalSize test
  • Loading branch information
Dawntraoz committed Jan 2, 2025
1 parent 1b62eec commit 3d0779d
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { FunctionComponent } from 'react'
import type { SetModalOpen } from '@storyblok/field-plugin'

const ModalToggle: FunctionComponent<{
isModalOpen: boolean
setModalOpen: (
isModalOpen: boolean,
modalSize?: { width: string; height: string },
) => void
setModalOpen: SetModalOpen<number>
}> = ({ isModalOpen, setModalOpen }) => {
return (
<div>
Expand Down
18 changes: 16 additions & 2 deletions packages/demo/src/components/ContextRequester.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@ export const ContextRequester: PluginComponent = (props) => {
const { data, actions } = props
return (
<Stack gap={2}>
<Typography variant="subtitle1">Story</Typography>
<Typography textAlign="center">{JSON.stringify(data.story)}</Typography>
<Typography variant="subtitle1">Story: </Typography>
<Typography textAlign="center">
{JSON.stringify(data.story, null, 2)}
</Typography>
<Typography variant="subtitle1">Permissions: </Typography>
<Typography textAlign="center">
{JSON.stringify(data.userPermissions, null, 2)}
</Typography>
<Typography variant="subtitle1">Is admin: </Typography>
<Typography textAlign="center">
{data.isSpaceAdmin ? 'Yes' : 'No'}
</Typography>
<Typography variant="subtitle1">Is AI enabled: </Typography>
<Typography textAlign="center">
{data.isAIEnabled ? 'Yes' : 'No'}
</Typography>
<Button
variant="outlined"
color="secondary"
Expand Down
4 changes: 3 additions & 1 deletion packages/demo/src/components/ModalToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export const ModalToggle: PluginComponent = (props) => {
<Button
variant="outlined"
color="secondary"
onClick={() => actions.setModalOpen(!data.isModalOpen)}
onClick={() =>
actions.setModalOpen(!data.isModalOpen, { width: '50%' })
}
>
Toggle Modal
</Button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Asset, StoryData } from '../messaging'
import { FieldPluginData } from './FieldPluginData'
import type { Asset, StoryData, ModalSize } from '../messaging'
import type { FieldPluginData } from './FieldPluginData'

export type SetContent<Content> = (
content: Content,
) => Promise<FieldPluginData<Content>>
export type SetModalOpen<Content> = (
isModalOpen: boolean,
modalSize?: ModalSize,
) => Promise<FieldPluginData<Content>>
export type RequestContext = () => Promise<StoryData>
export type SelectAsset = () => Promise<Asset>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type FieldPluginData<Content> = {
options: Record<string, string>
spaceId: number | undefined
userId: number | undefined
userPermissions: Record<string, any> | undefined
userPermissions: Record<string, string[] | number[]> | undefined
isSpaceAdmin: boolean
interfaceLang: string
storyLang: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,44 @@ describe('createPluginActions', () => {
} satisfies Partial<ModalChangeMessage>),
)
})
it('sends modalSize to the container when opening the modal', () => {
const { uid, postToContainer, onUpdateState } = mock()
const {
actions: { setModalOpen },
} = createPluginActions({
uid,
postToContainer,
onUpdateState,
validateContent,
})

setModalOpen(true, { width: '50%' })
expect(postToContainer).toHaveBeenCalledWith(
expect.objectContaining({
event: 'toggleModal',
status: true,
modalSize: { width: '50%' },
} satisfies Partial<ModalChangeMessage>),
)

setModalOpen(true, { height: '50%' })
expect(postToContainer).toHaveBeenCalledWith(
expect.objectContaining({
event: 'toggleModal',
status: true,
modalSize: { height: '50%' },
} satisfies Partial<ModalChangeMessage>),
)

setModalOpen(true, { width: '50%', height: '50%' })
expect(postToContainer).toHaveBeenCalledWith(
expect.objectContaining({
event: 'toggleModal',
status: true,
modalSize: { width: '50%', height: '50%' },
} satisfies Partial<ModalChangeMessage>),
)
})
})
describe('value state change', () => {
it('updates the value state when setContent is called', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ export const createPluginActions: CreatePluginActions = ({
)
})
},
setModalOpen: (
isModalOpen: boolean,
modalSize?: { width: string; height: string },
) => {
setModalOpen: (isModalOpen, modalSize) => {
return new Promise((resolve) => {
const callbackId = pushCallback('stateChanged', (message) =>
resolve(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type LoadedMessage = MessageToPlugin<'loaded'> & {
interfaceLanguage: string
spaceId: number | null
userId: number | undefined
userPermissions: Record<string, any> | undefined
userPermissions: Record<string, string[] | number[]> | undefined
isSpaceAdmin: boolean
story: StoryData
storyId: number | undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type StateChangedMessage = MessageToPlugin<'state-changed'> & {
interfaceLanguage: string
spaceId: number | null
userId: number | undefined
userPermissions: Record<string, any> | undefined
userPermissions: Record<string, string[] | number[]> | undefined
isSpaceAdmin: boolean
story: StoryData
storyId: number | undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { hasKey } from '../../../utils'
import { isMessageToContainer, MessageToContainer } from './MessageToContainer'

export type ModalSize = { width?: string; height?: string }

export type ModalChangeMessage = MessageToContainer<'toggleModal'> & {
status: boolean
modalSize?: { width: string; height: string }
modalSize?: ModalSize
}

export const isModalChangeMessage = (obj: unknown): obj is ModalChangeMessage =>
isMessageToContainer(obj) &&
obj.event === 'toggleModal' &&
Expand Down

0 comments on commit 3d0779d

Please sign in to comment.