-
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.
Refactor RestaurantsPage and add error handling
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
Showing
7 changed files
with
66 additions
and
26 deletions.
There are no files selected for viewing
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
5 changes: 5 additions & 0 deletions
5
frontend/src/components/CreateDataInvitation/CreateDataInvitation.styled.ts
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import styled from "styled-components"; | ||
|
||
export const StyledParagraph = styled.p` | ||
margin-bottom: 1rem; | ||
`; |
11 changes: 11 additions & 0 deletions
11
frontend/src/components/CreateDataInvitation/CreateDataInvitation.tsx
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 |
---|---|---|
@@ -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> | ||
</> | ||
) | ||
} |
This file was deleted.
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
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; | ||
`; |
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 |
---|---|---|
@@ -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> | ||
) | ||
} |