Skip to content

Commit

Permalink
Simplify redirection after closing the modal window
Browse files Browse the repository at this point in the history
Instead of passing the `closeModal` function, let's just pass the `navigateToOnClose` parameter. The redirection logic will be handled in one place. The`react-router-dom` package has been updated to version `6.26.0` because there was previously a problem with the useNavigate hooks. `useNavigate` caused the component to be re-rendered. This is probably related to this issue - remix-run/react-router#7634
  • Loading branch information
kkosiorowska committed Aug 2, 2024
1 parent 88ed278 commit b39abf7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"react-dom": "^18.2.0",
"react-number-format": "^5.3.1",
"react-redux": "^9.1.0",
"react-router-dom": "^6.22.0",
"react-router-dom": "^6.26.0",
"viem": "^2.13.8",
"wagmi": "^2.9.11"
},
Expand Down
18 changes: 14 additions & 4 deletions dapp/src/components/ModalRoot/withBaseModal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { ComponentType } from "react"
import React, { ComponentType, useCallback } from "react"
import { Modal, ModalContent, ModalOverlay, ModalProps } from "@chakra-ui/react"
import { BaseModalProps } from "#/types"
import { useSidebar } from "#/hooks"
import { useNavigate } from "react-router-dom"

const MODAL_BASE_SIZE = "lg"

Expand All @@ -10,14 +11,23 @@ function withBaseModal<T extends BaseModalProps>(
modalProps?: Omit<ModalProps, "isOpen" | "onClose" | "children">,
) {
return function ModalBase(props: T) {
const { closeModal, closeOnEsc } = props
const { closeModal, closeOnEsc, navigateToOnClose } = props

const { isOpen: isSidebarOpen } = useSidebar()
const navigate = useNavigate()

const handleCloseModal = useCallback(() => {
closeModal()

if (navigateToOnClose) {
navigate(navigateToOnClose)
}
}, [closeModal, navigate, navigateToOnClose])

return (
<Modal
isOpen
onClose={closeModal}
onClose={handleCloseModal}
scrollBehavior="inside"
closeOnOverlayClick={false}
size={MODAL_BASE_SIZE}
Expand All @@ -31,7 +41,7 @@ function withBaseModal<T extends BaseModalProps>(
xl: 0,
}}
>
<WrappedModalContent {...props} closeModal={closeModal} />
<WrappedModalContent {...props} closeModal={handleCloseModal} />
</ModalContent>
</Modal>
)
Expand Down
8 changes: 1 addition & 7 deletions dapp/src/hooks/useDepositCallToAction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { routerPath } from "#/router/path"
import { ACTION_FLOW_TYPES, MODAL_TYPES } from "#/types"
import { useEffect } from "react"
import { useNavigate } from "react-router-dom"
import {
useAllActivitiesCount,
useHasFetchedActivities,
Expand All @@ -15,7 +14,6 @@ function useDepositCallToAction() {
const hasActivities = activitiesCount > 0
const { modalType, openModal, closeModal } = useModal()
const isSignedMessage = useIsSignedMessage()
const navigate = useNavigate()

useEffect(() => {
const shouldOpenDepositModal =
Expand All @@ -24,10 +22,7 @@ function useDepositCallToAction() {
if (shouldOpenDepositModal)
openModal(MODAL_TYPES.STAKE, {
type: ACTION_FLOW_TYPES.STAKE,
closeModal: () => {
closeModal()
navigate(routerPath.home)
},
navigateToOnClose: routerPath.home,
})

return () => {
Expand All @@ -41,7 +36,6 @@ function useDepositCallToAction() {
isSignedMessage,
openModal,
closeModal,
navigate,
modalType,
])
}
Expand Down
4 changes: 0 additions & 4 deletions dapp/src/store/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@ export const middleware = {
serializableCheck: {
isSerializable: (value: unknown) =>
isPlain(value) || typeof value === "bigint",
// // Ignore these action types
ignoredActions: ["modal/openModal"],
// Ignore these field paths in all actions
ignoredPaths: ["modal.props.closeModal"],
},
}
3 changes: 3 additions & 0 deletions dapp/src/types/modal.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Pathname } from "#/router/path"

export type ModalProps = Record<string, unknown>

export type BaseModalProps = {
closeModal: () => void
closeOnEsc?: boolean
navigateToOnClose?: Pathname
}

export const MODAL_TYPES = {
Expand Down
22 changes: 11 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b39abf7

Please sign in to comment.