Skip to content

Commit

Permalink
feat: add stories for mfb popover
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin9foong committed Dec 30, 2024
1 parent 37e0940 commit b368fd0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const mrfModeHandlers = [
getAdminFormView({ mode: FormResponseMode.Multirespondent }),
]

const emailModeHandlers = [getAdminFormView({ mode: FormResponseMode.Email })]

export const EncryptMode = {
parameters: {
msw: encryptModeHandlers,
Expand All @@ -50,3 +52,9 @@ export const MrfMode = {
msw: mrfModeHandlers,
},
}

export const EmailMode = {
parameters: {
msw: emailModeHandlers,
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ const FieldSearchBar = ({
searchValue: string
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void
}) => {
// TODO (MFB-v1.1): Remove useUser and isTest which is used for beta flag when out of beta
const { user } = useUser()
const isTest = import.meta.env.STORYBOOK_NODE_ENV === 'test'

return (
<>
Expand All @@ -53,7 +55,8 @@ const FieldSearchBar = ({
onChange={onChange}
placeholder="Search fields"
/>
{user?.betaFlags?.mfb ? <MagicFormBuilderContainer /> : null}
{/* TODO (MFB-v1.1): Remove this beta flag and isTest check when out of beta*/}
{user?.betaFlags?.mfb || isTest ? <MagicFormBuilderContainer /> : null}
</InputGroup>
</>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Meta } from '@storybook/react/*'

import { StoryRouter } from '~utils/storybook'

import { MagicFormBuilderPopover } from './MagicFormBuilderContainer'

export default {
component: MagicFormBuilderPopover,
title:
'Features/AdminForm/create/builder-and-design/BuilderAndDesignDrawer/FieldListDrawer/MagicFormBuilderContainer',
decorators: [StoryRouter({ initialEntries: ['/12345'], path: '/:formId' })],
} as Meta

const defaultArgs = {
isOpen: false,
isAcceptDenyOpen: false,
onMfbClick: () => {},
onClose: () => {},
onAccept: () => {},
onDeny: () => {},
}

export const NotActive = {
args: defaultArgs,
}

export const ActivePrompt = {
args: {
...defaultArgs,
isOpen: true,
isAcceptDenyOpen: false,
},
}

export const ActiveAcceptDeny = {
args: {
...defaultArgs,
isOpen: true,
isAcceptDenyOpen: true,
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,40 @@ export const MagicFormBuilderContainer = () => {
const isMobile = useIsMobile()
const [isOpen, setIsOpen] = useState(false)

const clearRecentlyCreatedFieldIds = useMagicFormBuilderStore(
(state) => state.clearRecentlyCreatedFieldIds,
)
const recentlyCreatedFieldIds = useMagicFormBuilderStore(
recentlyCreatedFieldIdsSelector,
)

const onClickDefaults = () => {
setIsOpen(false)
setTimeout(() => {
clearRecentlyCreatedFieldIds()
}, 100) // only clear after the popover is closed
}

const { deleteMultipleFormFieldsMutation } = useDeleteFormField()

const isAcceptDenyOpen =
!!recentlyCreatedFieldIds && recentlyCreatedFieldIds.size > 0

return !isMobile ? (
<>
<MagicFormBuilderPopover isOpen={isOpen} setIsOpen={setIsOpen} />
<MagicFormBuilderPopover
isOpen={isOpen}
isAcceptDenyOpen={isAcceptDenyOpen}
onMfbClick={() => setIsOpen(!isOpen)}
onClose={() => setIsOpen(false)}
onAccept={onClickDefaults}
onDeny={() => {
deleteMultipleFormFieldsMutation.mutate(
Array.from(recentlyCreatedFieldIds),
)
onClickDefaults()
}}
/>
</>
) : null
}
Expand Down Expand Up @@ -243,56 +274,35 @@ interface TextPromptInputs {
prompt: string
}

const MagicFormBuilderPopover = ({
export const MagicFormBuilderPopover = ({
isOpen,
setIsOpen,
onMfbClick,
onClose,
isAcceptDenyOpen,
onAccept,
onDeny,
}: {
isOpen: boolean
setIsOpen: (isOpen: boolean) => void
onMfbClick: () => void
onClose: () => void
isAcceptDenyOpen: boolean
onAccept: () => void
onDeny: () => void
}) => {
const clearRecentlyCreatedFieldIds = useMagicFormBuilderStore(
(state) => state.clearRecentlyCreatedFieldIds,
)
const recentlyCreatedFieldIds = useMagicFormBuilderStore(
recentlyCreatedFieldIdsSelector,
)

const isAcceptDenyOpen =
!!recentlyCreatedFieldIds && recentlyCreatedFieldIds.size > 0

const onClickDefaults = () => {
setIsOpen(false)
setTimeout(() => {
clearRecentlyCreatedFieldIds()
}, 100) // only clear after the popover is closed
}

const { deleteMultipleFormFieldsMutation } = useDeleteFormField()

return (
<Popover isLazy placement="right-start" isOpen={isOpen}>
<PopoverAnchor>
<MagicFormBuilderButton
isActive={isOpen}
onClick={() => setIsOpen(!isOpen)}
/>
<MagicFormBuilderButton isActive={isOpen} onClick={onMfbClick} />
</PopoverAnchor>
<Portal>
<PopoverContent bg="white">
{!isAcceptDenyOpen ? (
<MagicFormBuilderCreateFormPrompt
onClose={() => setIsOpen(false)}
/>
<MagicFormBuilderCreateFormPrompt onClose={onClose} />
) : (
<MagicFormBuilderAcceptDeny
onAccept={onClickDefaults}
onDeny={() => {
deleteMultipleFormFieldsMutation.mutate(
Array.from(recentlyCreatedFieldIds),
)
onClickDefaults()
}}
onClose={() => setIsOpen(false)}
onAccept={onAccept}
onDeny={onDeny}
onClose={onClose}
/>
)}
</PopoverContent>
Expand Down

0 comments on commit b368fd0

Please sign in to comment.