-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from acharayaP03/installing_react_error_boundry
Installing react error boundry
- Loading branch information
Showing
7 changed files
with
180 additions
and
136 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,39 @@ | ||
import BookingRow from './BookingRow'; | ||
import { Table, Pagination } from '@/ui/Tables'; | ||
import { Menus } from '@/ui/ActionControls'; | ||
import { Empty, Spinner } from '@/ui/Common'; | ||
import { useBookings } from './useBookings'; | ||
import BookingRow from "./BookingRow"; | ||
import { Table, Pagination } from "@/ui/Tables"; | ||
import { Menus } from "@/ui/ActionControls"; | ||
import { Empty, Spinner } from "@/ui/Common"; | ||
import { useBookings } from "./useBookings"; | ||
|
||
function BookingTable() { | ||
const { isLoading, bookings, count } = useBookings(); | ||
const { isLoading, bookings, count } = useBookings(); | ||
|
||
if (isLoading) return <Spinner />; | ||
if (!bookings.length) <Empty resourceName='bookings' />; | ||
if (isLoading) return <Spinner />; | ||
if (!bookings.length) <Empty resourceName="bookings" />; | ||
|
||
return ( | ||
<Menus> | ||
<Table columns='0.6fr 2fr 2.4fr 1.4fr 1fr 3.2rem'> | ||
<Table.Header> | ||
<div>Cabin</div> | ||
<div>Guest</div> | ||
<div>Dates</div> | ||
<div>Status</div> | ||
<div>Amount</div> | ||
<div></div> | ||
</Table.Header> | ||
return ( | ||
<Menus> | ||
<Table columns="0.6fr 2fr 2.4fr 1.4fr 1fr 3.2rem"> | ||
<Table.Header> | ||
<div>Cabin</div> | ||
<div>Guest</div> | ||
<div>Dates</div> | ||
<div>Status</div> | ||
<div>Amount</div> | ||
<div></div> | ||
</Table.Header> | ||
|
||
<Table.Body | ||
data={bookings} | ||
render={(booking) => <BookingRow key={booking.id} booking={booking} />} | ||
/> | ||
<Table.Footer> | ||
<Pagination count={count} /> | ||
</Table.Footer> | ||
</Table> | ||
</Menus> | ||
); | ||
<Table.Body | ||
data={bookings} | ||
render={(booking) => ( | ||
<BookingRow key={booking.id} booking={booking} /> | ||
)} | ||
/> | ||
<Table.Footer> | ||
<Pagination count={count} /> | ||
</Table.Footer> | ||
</Table> | ||
</Menus> | ||
); | ||
} | ||
|
||
export default BookingTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,39 @@ | ||
import { Outlet } from 'react-router-dom'; | ||
import Header from './Header'; | ||
import Sidebar from './Sidebar'; | ||
import ErrorBoundary from './ErrorBoundry'; | ||
import styled from 'styled-components'; | ||
import { Outlet } from "react-router-dom"; | ||
import Header from "./Header"; | ||
import Sidebar from "./Sidebar"; | ||
import ErrorBoundary from "./ErrorBoundry"; | ||
import styled from "styled-components"; | ||
|
||
const StyledAppLayout = styled.div` | ||
display: grid; | ||
grid-template-columns: 26rem 1fr; | ||
grid-template-rows: auto 1fr; | ||
height: 100vh; | ||
display: grid; | ||
grid-template-columns: 26rem 1fr; | ||
grid-template-rows: auto 1fr; | ||
height: 100vh; | ||
`; | ||
const Main = styled.main` | ||
padding: 4rem 4.8rem 6.4rem; | ||
background-color: var(--color-grey-50); | ||
overflow: scroll; | ||
padding: 4rem 4.8rem 6.4rem; | ||
background-color: var(--color-grey-50); | ||
overflow: scroll; | ||
`; | ||
|
||
const Container = styled.div` | ||
max-width: 120rem; | ||
margin: 0 auto; | ||
max-width: 120rem; | ||
margin: 0 auto; | ||
display: flex; | ||
flex-direction: column; | ||
gap: 3.2rem; | ||
display: flex; | ||
flex-direction: column; | ||
gap: 3.2rem; | ||
`; | ||
export default function AppLayout() { | ||
return ( | ||
<ErrorBoundary> | ||
<StyledAppLayout> | ||
<Header /> | ||
<Sidebar /> | ||
<Main> | ||
<Container> | ||
<Outlet /> | ||
</Container> | ||
</Main> | ||
</StyledAppLayout> | ||
</ErrorBoundary> | ||
); | ||
return ( | ||
<StyledAppLayout> | ||
<Header /> | ||
<Sidebar /> | ||
<Main> | ||
<Container> | ||
<Outlet /> | ||
</Container> | ||
</Main> | ||
</StyledAppLayout> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,54 @@ | ||
import React, { Component } from 'react'; | ||
import { Heading } from '@/ui/Common'; | ||
import styled from 'styled-components'; | ||
import React, { Component } from "react"; | ||
import { Heading } from "@/ui/Common"; | ||
import styled from "styled-components"; | ||
|
||
const ErrorContainer = styled.div` | ||
display: block; | ||
margin: 10rem auto; | ||
padding: 4rem; | ||
text-align: center; | ||
border: 1px solid var(--color-grey-300); | ||
border-radius: 5px; | ||
max-width: 114rem; | ||
display: block; | ||
margin: 10rem auto; | ||
padding: 4rem; | ||
text-align: center; | ||
border: 1px solid var(--color-grey-300); | ||
border-radius: 5px; | ||
max-width: 114rem; | ||
`; | ||
|
||
const P = styled.p` | ||
margin-top: 1.6rem; | ||
margin-top: 1.6rem; | ||
`; | ||
|
||
/** | ||
* @description ErrorBoundary component is a class component that catches error in the application | ||
* this is our custom error boundary component that catches error in the application | ||
* not is use at the moment but can be used in the future and remove react-error-boundary package | ||
*/ | ||
class ErrorBoundary extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { hasError: false }; | ||
} | ||
static getDerivedStateFromError(error) { | ||
return { hasError: true }; | ||
} | ||
componentDidCatch(error, errorInfo) { | ||
console.error(error, errorInfo); | ||
} | ||
render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<ErrorContainer> | ||
<div> | ||
<Heading as='h2'>Apologies.</Heading> | ||
<P> | ||
Something went wrong while processing your request. we are currently working on this | ||
issue and will get back as soon as possible | ||
</P> | ||
</div> | ||
</ErrorContainer> | ||
); | ||
} | ||
return this.props.children; | ||
} | ||
constructor(props) { | ||
super(props); | ||
this.state = { hasError: false }; | ||
} | ||
static getDerivedStateFromError(error) { | ||
return { hasError: true }; | ||
} | ||
componentDidCatch(error, errorInfo) { | ||
console.error(error, errorInfo); | ||
} | ||
render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<ErrorContainer> | ||
<div> | ||
<Heading as="h2">Apologies.</Heading> | ||
<P> | ||
Something went wrong while processing your request. | ||
we are currently working on this issue and will get | ||
back as soon as possible | ||
</P> | ||
</div> | ||
</ErrorContainer> | ||
); | ||
} | ||
return this.props.children; | ||
} | ||
} | ||
|
||
export default ErrorBoundary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import App from './App.jsx'; | ||
import React from "react"; | ||
import ReactDOM from "react-dom/client"; | ||
import App from "./App.jsx"; | ||
import { ErrorBoundary } from "react-error-boundary"; | ||
import { ErrorFallback } from "./ui/Common"; | ||
|
||
ReactDOM.createRoot(document.getElementById('root')).render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode>, | ||
ReactDOM.createRoot(document.getElementById("root")).render( | ||
<React.StrictMode> | ||
<ErrorBoundary | ||
FallbackComponent={ErrorFallback} | ||
onReset={() => window.location.replace("/")} | ||
> | ||
<App /> | ||
</ErrorBoundary> | ||
</React.StrictMode>, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,59 @@ | ||
import styled from 'styled-components'; | ||
|
||
import styled from "styled-components"; | ||
import GlobalStyles from "@/styles/GlobalStyles"; | ||
import { Button } from "../Buttons"; | ||
const StyledErrorFallback = styled.main` | ||
height: 100vh; | ||
background-color: var(--color-grey-50); | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
padding: 4.8rem; | ||
height: 100vh; | ||
background-color: var(--color-grey-50); | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
padding: 4.8rem; | ||
`; | ||
|
||
const Box = styled.div` | ||
/* Box */ | ||
background-color: var(--color-grey-0); | ||
border: 1px solid var(--color-grey-100); | ||
border-radius: var(--border-radius-md); | ||
/* Box */ | ||
background-color: var(--color-grey-0); | ||
border: 1px solid var(--color-grey-100); | ||
border-radius: var(--border-radius-md); | ||
padding: 4.8rem; | ||
flex: 0 1 96rem; | ||
text-align: center; | ||
padding: 4.8rem; | ||
flex: 0 1 96rem; | ||
text-align: center; | ||
& h1 { | ||
margin-bottom: 1.6rem; | ||
} | ||
& h1 { | ||
margin-bottom: 1.6rem; | ||
} | ||
& p { | ||
font-family: 'Sono'; | ||
margin-bottom: 3.2rem; | ||
color: var(--color-grey-500); | ||
} | ||
& p { | ||
font-family: "Sono"; | ||
margin-bottom: 3.2rem; | ||
color: var(--color-grey-500); | ||
} | ||
`; | ||
|
||
export default function ErrorFallback({ error }) { | ||
return ( | ||
<StyledErrorFallback> | ||
<Box> | ||
<h1>Something went wrong</h1> | ||
<p>{error.message}</p> | ||
<button onClick={() => window.location.reload()}>Reload</button> | ||
</Box> | ||
</StyledErrorFallback> | ||
); | ||
/** | ||
* @props error and resetErrorBoundary | ||
* @description error is a any error that is thrown in the application with in component | ||
* @description resetErrorBoundary is a function that resets the error boundary, | ||
* it is a reset function that is called when the user clicks the "Try again" button on ErrorBoundry component on main.js | ||
* | ||
* @note this only catches error when the react is rendering the component. | ||
* @param {*} param0 | ||
* @returns | ||
*/ | ||
export default function ErrorFallback({ error, resetErrorBoundary }) { | ||
return ( | ||
<> | ||
<GlobalStyles /> | ||
<StyledErrorFallback> | ||
<Box> | ||
<h1>Something went wrong</h1> | ||
<p>{error.message}</p> | ||
<Button size="large" onClick={resetErrorBoundary}> | ||
Try again | ||
</Button> | ||
</Box> | ||
</StyledErrorFallback> | ||
</> | ||
); | ||
} |