Skip to content

Commit

Permalink
feat: extract NewPostModal to own component and add story
Browse files Browse the repository at this point in the history
  • Loading branch information
karrui committed Jul 7, 2023
1 parent c3e5432 commit 0fe60c9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useDisclosure } from '@chakra-ui/react'

import { NewPostModal } from './NewPostModal'
import { type Meta, type StoryObj } from '@storybook/react'
import { meHandlers } from 'tests/msw/handlers/me'

const meta: Meta<typeof NewPostModal> = {
title: 'Features/Posts/NewPostModal',
component: NewPostModal,
parameters: {
layout: 'fullscreen',
// Prevent flaky tests due to modal animating in.
chromatic: { pauseAnimationAtEnd: true },
msw: {
handlers: [meHandlers.me()],
},
},
}

export default meta
type Story = StoryObj<typeof NewPostModal>

export const Default: Story = {
render: (args) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const modalProps = useDisclosure({ defaultIsOpen: true })

return (
<NewPostModal
{...args}
{...modalProps}
onClose={() => console.log('close modal')}
/>
)
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import {
ModalHeader,
ModalOverlay,
type ModalProps,
useDisclosure,
} from '@chakra-ui/react'
import { Button, useToast } from '@opengovsg/design-system-react'
import { ComposePost } from '~/features/posts/components'
import { useZodForm } from '~/lib/form'
import { trpc } from '~/utils/trpc'
import { useUploadImagesMutation } from '../api'
import { clientAddPostSchema } from '../schemas/clientAddPostSchema'
import { useUploadImagesMutation } from '../../api'
import { clientAddPostSchema } from '../../schemas/clientAddPostSchema'

const NewPostModal = ({
export type NewPostModalProps = Pick<ModalProps, 'isOpen' | 'onClose'>

export const NewPostModal = ({
onClose: onCloseProp,
isOpen,
}: Pick<ModalProps, 'isOpen' | 'onClose'>) => {
}: NewPostModalProps) => {
const toast = useToast({
status: 'success',
})
Expand Down Expand Up @@ -84,15 +85,3 @@ const NewPostModal = ({
</Modal>
)
}

export const NewPostModalButton = (): JSX.Element => {
const { isOpen, onOpen, onClose } = useDisclosure()
return (
<>
<Button size="xs" onClick={onOpen}>
New post
</Button>
<NewPostModal isOpen={isOpen} onClose={onClose} />
</>
)
}
15 changes: 15 additions & 0 deletions src/features/posts/components/NewPostModal/NewPostModalButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useDisclosure } from '@chakra-ui/react'
import { Button } from '@opengovsg/design-system-react'
import { NewPostModal } from './NewPostModal'

export const NewPostModalButton = (): JSX.Element => {
const { isOpen, onOpen, onClose } = useDisclosure()
return (
<>
<Button size="xs" onClick={onOpen}>
New post
</Button>
<NewPostModal isOpen={isOpen} onClose={onClose} />
</>
)
}
1 change: 1 addition & 0 deletions src/features/posts/components/NewPostModal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './NewPostModalButton'
2 changes: 1 addition & 1 deletion src/features/posts/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './Post'
export * from './NewPostModalButton'
export * from './NewPostModal'
export * from './ComposePost'
export * from './PostActions'

0 comments on commit 0fe60c9

Please sign in to comment.