Browse Source

chore(web): implement error boundary

pull/1/head
isra el 3 years ago
parent
commit
e18922c85f
  1. 34
      web/components/ErrorBoundary.tsx
  2. 7
      web/pages/_app.tsx
  3. 5
      web/pages/dashboard/index.tsx

34
web/components/ErrorBoundary.tsx

@ -0,0 +1,34 @@
import React, { Component } from 'react'
interface ErrorBoundaryProps {
fallback?: string | React.ReactNode
children: React.ReactNode
}
interface ErrorBoundaryState {
hasError: boolean
}
export default class ErrorBoundary extends Component<
ErrorBoundaryProps,
ErrorBoundaryState
> {
state = { hasError: false }
static getDerivedStateFromError(error) {
console.log(error)
return { hasError: true }
}
componentDidCatch(error, errorInfo) {
console.log(error, errorInfo)
}
render() {
if (this.state.hasError) {
return this.props.fallback || 'Something went wrong'
}
return this.props.children
}
}

7
web/pages/_app.tsx

@ -6,10 +6,14 @@ import { ChakraProvider } from '@chakra-ui/react'
import Navbar from '../components/Navbar'
import Meta from '../components/meta/Meta'
import { GoogleOAuthProvider } from '@react-oauth/google'
import ErrorBoundary from '../components/ErrorBoundary'
function MyApp({ Component, pageProps }: AppProps) {
return (
<ErrorBoundary>
<Provider store={store}>
<GoogleOAuthProvider clientId={process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID }>
<GoogleOAuthProvider
clientId={process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID}
>
<ChakraProvider>
<Meta />
<Navbar />
@ -19,6 +23,7 @@ function MyApp({ Component, pageProps }: AppProps) {
</ChakraProvider>
</GoogleOAuthProvider>
</Provider>
</ErrorBoundary>
)
}

5
web/pages/dashboard/index.tsx

@ -9,6 +9,7 @@ import { selectAuth } from '../../store/authReducer'
import Router from 'next/router'
import { useEffect } from 'react'
import SendSMS from '../../components/dashboard/SendSMS'
import ErrorBoundary from '../../components/ErrorBoundary'
export default function Dashboard() {
const { user: currentUser } = useSelector(selectAuth)
@ -35,10 +36,14 @@ export default function Dashboard() {
<br />
<SimpleGrid columns={{ base: 1, md: 2 }} spacing={{ base: 5, lg: 8 }}>
<Box backdropBlur='2xl' borderWidth='1px' borderRadius='lg'>
<ErrorBoundary>
<ApiKeyList />
</ErrorBoundary>
</Box>
<Box backdropBlur='2xl' borderWidth='1px' borderRadius='lg'>
<ErrorBoundary>
<DeviceList />
</ErrorBoundary>
</Box>
</SimpleGrid>
</Box>

Loading…
Cancel
Save