Skip to content

Commit

Permalink
fix: modals sometimes not close after the route changes
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Jun 21, 2021
1 parent 73b2457 commit f5f6595
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
9 changes: 3 additions & 6 deletions src/AppContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
HashRouterProps,
} from 'react-router-dom'

import App from './App'
import { ProfileProvider } from './models/profile'

const ReactRouter: React.FC<BrowserRouterProps | HashRouterProps> = (args) => {
Expand All @@ -27,16 +26,14 @@ const styleCache = createCache({
key: 'yasd',
})

const AppContainer: React.FC = () => {
const AppContainer: React.FC = ({ children }) => {
return (
<Suspense fallback={<div></div>}>
<Suspense fallback={<div />}>
<CacheProvider value={styleCache}>
<ReactRouter>
<ProfileProvider>
<ThemeProvider theme={light}>
<ModalProvider>
<App />
</ModalProvider>
<ModalProvider>{children}</ModalProvider>
</ThemeProvider>
</ProfileProvider>
</ReactRouter>
Expand Down
12 changes: 12 additions & 0 deletions src/components/ScrollToTop/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { useModal } from '@sumup/circuit-ui'
import React, { useEffect } from 'react'
import { useLocation } from 'react-router-dom'

const ScrollToTop: React.FC = () => {
const { pathname } = useLocation()
const { isModalOpen, removeModal } = useModal()

useEffect(() => {
window.scrollTo(0, 0)
}, [pathname])

useEffect(
() => {
if (isModalOpen) {
removeModal()
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[pathname],
)

return <></>
}

Expand Down
41 changes: 25 additions & 16 deletions src/pages/Devices/components/DeviceItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ const DeviceItem = ({ device }: { device: DeviceInfo }): JSX.Element => {
const surgeHost = useSurgeHost()
const history = useHistory()

const onDeviceSettingsClick = useCallback(() => {
setModal({
children({ onClose }) {
return onClose && device.dhcpDevice ? (
<DeviceSettingsModal
title={device.dhcpDevice.displayName || device.name}
dhcpDevice={device.dhcpDevice}
onClose={onClose}
/>
) : (
<React.Fragment />
)
},
})
}, [device.dhcpDevice, device.name, setModal])

const onClick = useCallback(() => {
const actions = [
{
Expand All @@ -40,21 +56,7 @@ const DeviceItem = ({ device }: { device: DeviceInfo }): JSX.Element => {
actions.push({
id: 'device_settings',
title: 'devices.device_settings',
onClick: () => {
setModal({
children({ onClose }) {
return onClose && device.dhcpDevice ? (
<DeviceSettingsModal
title={device.dhcpDevice.displayName || device.name}
dhcpDevice={device.dhcpDevice}
onClose={onClose}
/>
) : (
<React.Fragment />
)
},
})
},
onClick: onDeviceSettingsClick,
})
}

Expand All @@ -71,7 +73,14 @@ const DeviceItem = ({ device }: { device: DeviceInfo }): JSX.Element => {
)
},
})
}, [device, setModal])
}, [
device.dhcpDevice,
device.displayIPAddress,
device.name,
onDeviceSettingsClick,
setModal,
history,
])

const gateway = useMemo<boolean>(() => {
const { hasTCPConnection } = device
Expand Down

0 comments on commit f5f6595

Please sign in to comment.