Skip to content

Commit

Permalink
Refactor RestaurantsPage and add error handling
Browse files Browse the repository at this point in the history
The RestaurantsPage component has been restructured to improve code organization and readability. A new component, CreateDataInvitation, was added to handle the scenario where there are no restaurants available. Error handling related to fetching restaurants data from API has been improved. In the event of an error, the error message is displayed in the UI to ease debugging. An `error-color` CSS variable was added for use in error messages.
  • Loading branch information
josch87 committed Jun 5, 2024
1 parent 2942910 commit 0ca9a2e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 26 deletions.
1 change: 1 addition & 0 deletions frontend/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
--header-height: 70px;
--default-color: #333333;
--primary-color: #6200ea;
--error-color: red;
}

body {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import RestaurantsPage from "./pages/RestaurantsPage.tsx";
import RestaurantsPage from "./pages/RestaurantsPage/RestaurantsPage.tsx";
import RestaurantDetailsPage from "./pages/RestaurantDetailsPage.tsx";
import {Route, Routes} from "react-router-dom";
import AddRestaurantsPage from "./pages/AddRestaurantsPage.tsx";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import styled from "styled-components";

export const StyledParagraph = styled.p`
margin-bottom: 1rem;
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Button from "../Button/Button.tsx";
import {StyledParagraph} from "./CreateDataInvitation.styled.ts";

export default function CreateDataInvitation() {
return (
<>
<StyledParagraph>Create your first restaurant! All your restaurants will be displayed here.</StyledParagraph>
<Button href="/restaurants/add">Add restaurant</Button>
</>
)
}
25 changes: 0 additions & 25 deletions frontend/src/pages/RestaurantsPage.tsx

This file was deleted.

7 changes: 7 additions & 0 deletions frontend/src/pages/RestaurantsPage/RestaurantsPage.styled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import styled from "styled-components";

export const StyledErrorParagraph = styled.p`
font-size: .8rem;
color: var(--error-color);
margin-top: 1rem;
`;
41 changes: 41 additions & 0 deletions frontend/src/pages/RestaurantsPage/RestaurantsPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import RestaurantCardList from "../../components/RestaurantCardList/RestaurantCardList.tsx";
import DefaultPageTemplate from "../templates/DefaultPageTemplate/DefaultPageTemplate.tsx";
import axios, {AxiosError} from "axios";
import {useEffect, useState} from "react";
import {RestaurantType} from "../../model/Restaurant.ts";
import CreateDataInvitation from "../../components/CreateDataInvitation/CreateDataInvitation.tsx";
import {StyledErrorParagraph} from "./RestaurantsPage.styled.ts";


export default function RestaurantsPage() {
const [restaurants, setRestaurants] = useState<RestaurantType[]>([]);
const [error, setError] = useState<AxiosError>();

useEffect(() => {
axios.get("/api/restaurants")
.then((response) => setRestaurants(response.data))
.catch((error => {
setError(error)
console.error(error.message);
}))

}, []);

if (error) {
return (
<DefaultPageTemplate pageTitle="Restaurants">
<p>Sorry, we encountered an error loading the restaurants. Please try again later.</p>
<StyledErrorParagraph>{error.message}</StyledErrorParagraph>
</DefaultPageTemplate>
)
}

return (
<DefaultPageTemplate pageTitle="Restaurants">
{restaurants.length === 0 ?
<CreateDataInvitation/> :
<RestaurantCardList restaurants={restaurants}/>
}
</DefaultPageTemplate>
)
}

0 comments on commit 0ca9a2e

Please sign in to comment.