Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Installing react error boundry #81

Merged
merged 4 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"date-fns": "^4.1.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-error-boundary": "^5.0.0",
"react-hook-form": "^7.54.2",
"react-hot-toast": "^2.4.1",
"react-icons": "^5.4.0",
Expand Down
60 changes: 31 additions & 29 deletions src/features/bookings/BookingTable.jsx
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;
58 changes: 28 additions & 30 deletions src/layouts/AppLayout.jsx
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>
);
}
81 changes: 44 additions & 37 deletions src/layouts/ErrorBoundry.jsx
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;
21 changes: 14 additions & 7 deletions src/main.jsx
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>,
);
82 changes: 49 additions & 33 deletions src/ui/Common/ErrorFallback.jsx
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>
</>
);
}
Loading