Skip to content

Commit 3d0779d

Browse files
committed
feat(shape-7911): add new state as context in the demo, replace any by permissions types, add ModalSize type and ModalSize test
1 parent 1b62eec commit 3d0779d

File tree

10 files changed

+70
-17
lines changed

10 files changed

+70
-17
lines changed

packages/cli/templates/react/src/components/FieldPluginExample/ModalToggle.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { FunctionComponent } from 'react'
2+
import type { SetModalOpen } from '@storyblok/field-plugin'
23

34
const ModalToggle: FunctionComponent<{
45
isModalOpen: boolean
5-
setModalOpen: (
6-
isModalOpen: boolean,
7-
modalSize?: { width: string; height: string },
8-
) => void
6+
setModalOpen: SetModalOpen<number>
97
}> = ({ isModalOpen, setModalOpen }) => {
108
return (
119
<div>

packages/demo/src/components/ContextRequester.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,22 @@ export const ContextRequester: PluginComponent = (props) => {
55
const { data, actions } = props
66
return (
77
<Stack gap={2}>
8-
<Typography variant="subtitle1">Story</Typography>
9-
<Typography textAlign="center">{JSON.stringify(data.story)}</Typography>
8+
<Typography variant="subtitle1">Story: </Typography>
9+
<Typography textAlign="center">
10+
{JSON.stringify(data.story, null, 2)}
11+
</Typography>
12+
<Typography variant="subtitle1">Permissions: </Typography>
13+
<Typography textAlign="center">
14+
{JSON.stringify(data.userPermissions, null, 2)}
15+
</Typography>
16+
<Typography variant="subtitle1">Is admin: </Typography>
17+
<Typography textAlign="center">
18+
{data.isSpaceAdmin ? 'Yes' : 'No'}
19+
</Typography>
20+
<Typography variant="subtitle1">Is AI enabled: </Typography>
21+
<Typography textAlign="center">
22+
{data.isAIEnabled ? 'Yes' : 'No'}
23+
</Typography>
1024
<Button
1125
variant="outlined"
1226
color="secondary"

packages/demo/src/components/ModalToggle.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export const ModalToggle: PluginComponent = (props) => {
99
<Button
1010
variant="outlined"
1111
color="secondary"
12-
onClick={() => actions.setModalOpen(!data.isModalOpen)}
12+
onClick={() =>
13+
actions.setModalOpen(!data.isModalOpen, { width: '50%' })
14+
}
1315
>
1416
Toggle Modal
1517
</Button>

packages/field-plugin/src/createFieldPlugin/FieldPluginActions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { Asset, StoryData } from '../messaging'
2-
import { FieldPluginData } from './FieldPluginData'
1+
import type { Asset, StoryData, ModalSize } from '../messaging'
2+
import type { FieldPluginData } from './FieldPluginData'
33

44
export type SetContent<Content> = (
55
content: Content,
66
) => Promise<FieldPluginData<Content>>
77
export type SetModalOpen<Content> = (
88
isModalOpen: boolean,
9+
modalSize?: ModalSize,
910
) => Promise<FieldPluginData<Content>>
1011
export type RequestContext = () => Promise<StoryData>
1112
export type SelectAsset = () => Promise<Asset>

packages/field-plugin/src/createFieldPlugin/FieldPluginData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type FieldPluginData<Content> = {
99
options: Record<string, string>
1010
spaceId: number | undefined
1111
userId: number | undefined
12-
userPermissions: Record<string, any> | undefined
12+
userPermissions: Record<string, string[] | number[]> | undefined
1313
isSpaceAdmin: boolean
1414
interfaceLang: string
1515
storyLang: string

packages/field-plugin/src/createFieldPlugin/createPluginActions/createPluginActions.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,44 @@ describe('createPluginActions', () => {
138138
} satisfies Partial<ModalChangeMessage>),
139139
)
140140
})
141+
it('sends modalSize to the container when opening the modal', () => {
142+
const { uid, postToContainer, onUpdateState } = mock()
143+
const {
144+
actions: { setModalOpen },
145+
} = createPluginActions({
146+
uid,
147+
postToContainer,
148+
onUpdateState,
149+
validateContent,
150+
})
151+
152+
setModalOpen(true, { width: '50%' })
153+
expect(postToContainer).toHaveBeenCalledWith(
154+
expect.objectContaining({
155+
event: 'toggleModal',
156+
status: true,
157+
modalSize: { width: '50%' },
158+
} satisfies Partial<ModalChangeMessage>),
159+
)
160+
161+
setModalOpen(true, { height: '50%' })
162+
expect(postToContainer).toHaveBeenCalledWith(
163+
expect.objectContaining({
164+
event: 'toggleModal',
165+
status: true,
166+
modalSize: { height: '50%' },
167+
} satisfies Partial<ModalChangeMessage>),
168+
)
169+
170+
setModalOpen(true, { width: '50%', height: '50%' })
171+
expect(postToContainer).toHaveBeenCalledWith(
172+
expect.objectContaining({
173+
event: 'toggleModal',
174+
status: true,
175+
modalSize: { width: '50%', height: '50%' },
176+
} satisfies Partial<ModalChangeMessage>),
177+
)
178+
})
141179
})
142180
describe('value state change', () => {
143181
it('updates the value state when setContent is called', () => {

packages/field-plugin/src/createFieldPlugin/createPluginActions/createPluginActions.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ export const createPluginActions: CreatePluginActions = ({
107107
)
108108
})
109109
},
110-
setModalOpen: (
111-
isModalOpen: boolean,
112-
modalSize?: { width: string; height: string },
113-
) => {
110+
setModalOpen: (isModalOpen, modalSize) => {
114111
return new Promise((resolve) => {
115112
const callbackId = pushCallback('stateChanged', (message) =>
116113
resolve(

packages/field-plugin/src/messaging/pluginMessage/containerToPluginMessage/LoadedMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type LoadedMessage = MessageToPlugin<'loaded'> & {
1414
interfaceLanguage: string
1515
spaceId: number | null
1616
userId: number | undefined
17-
userPermissions: Record<string, any> | undefined
17+
userPermissions: Record<string, string[] | number[]> | undefined
1818
isSpaceAdmin: boolean
1919
story: StoryData
2020
storyId: number | undefined

packages/field-plugin/src/messaging/pluginMessage/containerToPluginMessage/StateChangedMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type StateChangedMessage = MessageToPlugin<'state-changed'> & {
1414
interfaceLanguage: string
1515
spaceId: number | null
1616
userId: number | undefined
17-
userPermissions: Record<string, any> | undefined
17+
userPermissions: Record<string, string[] | number[]> | undefined
1818
isSpaceAdmin: boolean
1919
story: StoryData
2020
storyId: number | undefined

packages/field-plugin/src/messaging/pluginMessage/pluginToContainerMessage/ModalChangeMessage.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { hasKey } from '../../../utils'
22
import { isMessageToContainer, MessageToContainer } from './MessageToContainer'
33

4+
export type ModalSize = { width?: string; height?: string }
5+
46
export type ModalChangeMessage = MessageToContainer<'toggleModal'> & {
57
status: boolean
6-
modalSize?: { width: string; height: string }
8+
modalSize?: ModalSize
79
}
10+
811
export const isModalChangeMessage = (obj: unknown): obj is ModalChangeMessage =>
912
isMessageToContainer(obj) &&
1013
obj.event === 'toggleModal' &&

0 commit comments

Comments
 (0)