Skip to content

Commit

Permalink
Change the approach to opening modals
Browse files Browse the repository at this point in the history
We should have one modal and change the content in it.
  • Loading branch information
kkosiorowska committed Dec 14, 2023
1 parent 1d4e834 commit dcac86c
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 179 deletions.
13 changes: 3 additions & 10 deletions dapp/src/DApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import theme from "./theme"
import {
DocsDrawerContextProvider,
LedgerWalletAPIProvider,
ModalContextProvider,
SidebarContextProvider,
WalletContextProvider,
} from "./contexts"
import Header from "./components/Header"
import Overview from "./components/Overview"
import Sidebar from "./components/Sidebar"
import DocsDrawer from "./components/DocsDrawer"
import ModalOverlay from "./components/ModalOverlay"

function DApp() {
useDetectThemeMode()
Expand All @@ -26,9 +24,6 @@ function DApp() {
</Box>
<Sidebar />
<DocsDrawer />
{/* The user has several modals in a flow.
Let's use our own modal overlay to prevent the background flickering effect. */}
<ModalOverlay />
</>
)
}
Expand All @@ -39,11 +34,9 @@ function DAppProviders() {
<WalletContextProvider>
<DocsDrawerContextProvider>
<SidebarContextProvider>
<ModalContextProvider>
<ChakraProvider theme={theme}>
<DApp />
</ChakraProvider>
</ModalContextProvider>
<ChakraProvider theme={theme}>
<DApp />
</ChakraProvider>
</SidebarContextProvider>
</DocsDrawerContextProvider>
</WalletContextProvider>
Expand Down
40 changes: 0 additions & 40 deletions dapp/src/components/ModalOverlay/index.tsx

This file was deleted.

25 changes: 0 additions & 25 deletions dapp/src/components/Modals/BaseModal.tsx

This file was deleted.

18 changes: 18 additions & 0 deletions dapp/src/components/Modals/StakeModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react"
import { Button, ModalBody, ModalCloseButton } from "@chakra-ui/react"
import { TextMd } from "../Typography"
import { ModalStep } from "../../contexts"

export default function StakeModal({ goNext }: ModalStep) {
return (
<>
<ModalCloseButton />
<ModalBody>
<TextMd>Stake modal</TextMd>
<Button width="100%" onClick={goNext}>
Stake
</Button>
</ModalBody>
</>
)
}
26 changes: 20 additions & 6 deletions dapp/src/components/Modals/StakingOverviewModal.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import React from "react"
import { ModalHeader } from "@chakra-ui/react"
import BaseModal from "./BaseModal"
import {
Button,
ModalBody,
ModalCloseButton,
ModalFooter,
} from "@chakra-ui/react"
import { TextMd } from "../Typography"
import { ModalStep } from "../../contexts"

export default function StakingOverviewModal() {
export default function StakingOverviewModal({ goNext }: ModalStep) {
return (
<BaseModal>
<ModalHeader textAlign="center">Staking overview</ModalHeader>
</BaseModal>
<>
<ModalCloseButton />
<ModalBody>
<TextMd>Staking overview</TextMd>
</ModalBody>
<ModalFooter>
<Button width="100%" onClick={goNext}>
Get started
</Button>
</ModalFooter>
</>
)
}
22 changes: 8 additions & 14 deletions dapp/src/components/Overview/PositionDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react"
import React, { useState } from "react"
import {
Text,
Button,
Expand All @@ -12,12 +12,10 @@ import {
} from "@chakra-ui/react"
import { BITCOIN, USD } from "../../constants"
import { Info } from "../../static/icons"
import { useModal, useSidebar } from "../../hooks"
import Staking from "../Staking"
import StakingModal from "../StakingModal"

export default function PositionDetails(props: CardProps) {
const { openModal } = useModal()
const { onOpen: openSidebar } = useSidebar()
const [isOpenStakingModal, setIsOpenStakingModal] = useState(false)

return (
<Card {...props}>
Expand All @@ -38,17 +36,13 @@ export default function PositionDetails(props: CardProps) {
</CardBody>
<CardFooter flexDirection="column" gap={2}>
{/* TODO: Handle click actions */}
<Button
onClick={() => {
openSidebar()
openModal("overview")
}}
>
Stake
</Button>
<Button onClick={() => setIsOpenStakingModal(true)}>Stake</Button>
<Button variant="outline">Withdraw</Button>
</CardFooter>
<Staking />
<StakingModal
isOpen={isOpenStakingModal}
onClose={() => setIsOpenStakingModal(false)}
/>
</Card>
)
}
11 changes: 0 additions & 11 deletions dapp/src/components/Staking/index.tsx

This file was deleted.

34 changes: 34 additions & 0 deletions dapp/src/components/StakingModal/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from "react"
import { useModalFlowContext } from "../../hooks"
import StakeModal from "../Modals/StakeModal"
import StakingOverviewModal from "../Modals/StakingOverviewModal"
import ModalBase from "../shared/ModalBase"

function StakingModalSteps() {
const { activeStep, goNext } = useModalFlowContext()

console.log("activeStep ", activeStep)

switch (activeStep) {
case "overview":
return <StakingOverviewModal goNext={goNext} />
case "stake":
return <StakeModal goNext={goNext} />
default:
return null
}
}

export default function StakingModal({
isOpen,
onClose,
}: {
isOpen: boolean
onClose: () => void
}) {
return (
<ModalBase isOpen={isOpen} onClose={onClose} steps={["overview", "stake"]}>
<StakingModalSteps />
</ModalBase>
)
}
73 changes: 73 additions & 0 deletions dapp/src/components/shared/ModalBase/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React, { useCallback, useEffect, useMemo, useState } from "react"
import { Modal, ModalContent, ModalOverlay } from "@chakra-ui/react"
import { HEADER_HEIGHT } from "../../Header"
import { ModalFlowContext, ModalFlowContextValue } from "../../../contexts"
import { useSidebar } from "../../../hooks"

export default function ModalBase({
isOpen,
onClose,
steps,
defaultIndex = 0,
children,
}: {
isOpen: boolean
onClose: () => void
steps: string[]
defaultIndex?: number
children: React.ReactNode
}) {
const { onOpen: openSideBar, onClose: closeSidebar } = useSidebar()

const [activeStep, setActiveStep] = useState(steps[defaultIndex])
const [index, setIndex] = useState(defaultIndex)

const handleGoNext = useCallback(() => {
setIndex((prevIndex) => prevIndex + 1)
}, [])

const handleClose = useCallback(() => {
onClose()
}, [onClose])

useEffect(() => {
if (index >= steps.length) {
handleClose()
} else {
setActiveStep(steps[index])
}
}, [steps, index, handleClose])

useEffect(() => {
if (!isOpen) {
closeSidebar()

const timeout = setTimeout(() => {
setActiveStep(steps[defaultIndex])
setIndex(defaultIndex)
}, 100)
return () => clearTimeout(timeout)
} else {
openSideBar()
}
}, [isOpen, steps, defaultIndex, closeSidebar, openSideBar])

const contextValue: ModalFlowContextValue = useMemo<ModalFlowContextValue>(
() => ({
steps,
activeStep,
onClose: handleClose,
goNext: handleGoNext,
}),
[activeStep, steps, handleGoNext, handleClose],
)

return (
<ModalFlowContext.Provider value={contextValue}>
<Modal size="md" isOpen={isOpen} onClose={handleClose}>
<ModalOverlay mt={HEADER_HEIGHT} />
<ModalContent mt={2 * HEADER_HEIGHT}>{children}</ModalContent>
</Modal>
</ModalFlowContext.Provider>
)
}
45 changes: 0 additions & 45 deletions dapp/src/contexts/ModalContext.tsx

This file was deleted.

19 changes: 19 additions & 0 deletions dapp/src/contexts/ModalFlowContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createContext } from "react"

export type ModalStep = {
goNext: () => void
}

export type ModalFlowContextValue = {
activeStep?: string
steps: string[]
onClose: () => void
goNext: () => void
}

export const ModalFlowContext = createContext<ModalFlowContextValue>({
activeStep: undefined,
steps: [],
goNext: () => {},
onClose: () => {},
})
2 changes: 1 addition & 1 deletion dapp/src/contexts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from "./WalletContext"
export * from "./LedgerWalletAPIProvider"
export * from "./DocsDrawerContext"
export * from "./SidebarContext"
export * from "./ModalContext"
export * from "./ModalFlowContext"
Loading

0 comments on commit dcac86c

Please sign in to comment.