Skip to content

Commit

Permalink
Implement mutation in restaurant pages
Browse files Browse the repository at this point in the history
Added SWR mutate function to the CreateRestaurantPage and UpdateRestaurantPage to update the cache after a restaurant's information has been either updated or created. This ensures that the user will see the most accurate, up-to-date restaurant data immediately, without needing to refresh the page or risk encountering stale data.
  • Loading branch information
josch87 committed Jun 9, 2024
1 parent 911a03d commit b7a3167
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions frontend/src/pages/CreateRestaurantPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import axios from "axios";
import { useNavigate } from "react-router-dom";
import { NewRestaurantDTOType, RestaurantType } from "../model/Restaurant.ts";
import { logtail } from "../logger.ts";
import { mutate } from "swr";

export default function CreateRestaurantPage() {
const navigate = useNavigate();
Expand All @@ -16,6 +17,7 @@ export default function CreateRestaurantPage() {
.then((response) => {
const savedRestaurant: RestaurantType = response.data;
logtail.info("Created new restaurant with ID " + savedRestaurant.id);
mutate("/api/restaurants");
navigate("/restaurants/" + savedRestaurant.id);
})
.catch((error) => {
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/pages/UpdateRestaurantPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import RestaurantForm from "../components/RestaurantForm/RestaurantForm.tsx";
import { logtail } from "../logger.ts";
import { useRestaurant } from "../data/restaurantData.ts";
import AlertBox from "../components/AlertBox/AlertBox.tsx";
import { mutate } from "swr";

export default function UpdateRestaurantPage() {
const navigate = useNavigate();
Expand Down Expand Up @@ -44,8 +45,10 @@ export default function UpdateRestaurantPage() {
axios
.put(`/api/restaurants/${id}`, formData)
.then(() => {
logtail.info("Updated data of restaurant with ID " + id);
navigate("/");
logtail.info(`Updated data of restaurant with ID ${id}`);
mutate(`/api/restaurants/${id}`);
mutate("/api/restaurants");
navigate(`/restaurants/${id}`);
})
.catch((error) => {
logtail.error(error.message, {
Expand Down

0 comments on commit b7a3167

Please sign in to comment.