Skip to content

Commit

Permalink
Fix the search context not working with favourites
Browse files Browse the repository at this point in the history
Signed-off-by: Emil Balitzki <[email protected]>
  • Loading branch information
Corgam committed May 21, 2024
1 parent aee5b8d commit 5b2982f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
21 changes: 14 additions & 7 deletions frontend/src/components/PopUp/SearchPopUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ const SearchPopUp: React.FC<SearchPopUpProps> = ({
const filterBySearchtxtAndIfInFavorites = (item: MapSelection) => {
return (
item.displayName.toLowerCase().includes(searchText.toLowerCase()) &&
!currentSearchCache.favourites.some((fav) => fav.id === item.id)
!currentSearchCache.favourites.some((fav) =>
fav.coordinates.equals(item.coordinates)
)
);
};

Expand All @@ -41,19 +43,24 @@ const SearchPopUp: React.FC<SearchPopUpProps> = ({
};

// Adds an item to the favourites
const addToFavourites = (item: MapSelection) => {
if (!currentSearchCache.favourites.some((fav) => fav.id === item.id)) {
const addToFavourites = (newLocation: MapSelection) => {
if (
!currentSearchCache.favourites.some((fav) =>
fav.coordinates.equals(newLocation.coordinates)
)
) {
const newFav = [...currentSearchCache.favourites, newLocation];
setCurrentSearchCache({
...currentSearchCache,
favourites: [...currentSearchCache.favourites, item],
favourites: newFav,
});
}
};

// Removes an item from the favourites
const removeFromFavourites = (itemToRemove: MapSelection) => {
const removeFromFavourites = (locationToRemove: MapSelection) => {
const updatedFavorites = currentSearchCache.favourites.filter(
(item) => item.id !== itemToRemove.id
(item) => !item.coordinates.equals(locationToRemove.coordinates)
);
setCurrentSearchCache({
...currentSearchCache,
Expand Down Expand Up @@ -107,7 +114,7 @@ const SearchPopUp: React.FC<SearchPopUpProps> = ({
"& .MuiDialog-container": {
"& .MuiPaper-root": {
width: "100%",
maxWidth: "500px", // Set your width here
maxWidth: "500px",
},
},
}}
Expand Down
25 changes: 8 additions & 17 deletions frontend/src/contexts/SearchContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import React, { createContext, useState, ReactNode } from "react";

// Map Selection Type
export type MapSelection = {
id: string;
coordinates: LatLng;
displayName: string;
};
Expand Down Expand Up @@ -33,45 +32,37 @@ type SearchContextProviderProps = {
const defaultSearchCache: SearchCacheProps = {
searchHistory: [
{
id: "1",
coordinates: L.latLng([49.5732, 11.0288]),
coordinates: L.latLng([49.5732, 12.0288]),
displayName: "Nuremberg",
},
{
id: "2",
coordinates: L.latLng([49.5732, 11.0288]),
coordinates: L.latLng([49.5732, 13.0288]),
displayName: "Munich",
},
{
id: "3",
coordinates: L.latLng([49.5732, 11.0288]),
coordinates: L.latLng([49.5732, 14.0288]),
displayName: "Andreij Sacharow Platz 1, 90402 Nuremberg",
},
{
id: "4",
coordinates: L.latLng([49.5732, 11.0288]),
coordinates: L.latLng([49.5732, 15.0288]),
displayName: "Main train station Nuremberg",
},
{
id: "5",
coordinates: L.latLng([49.5732, 11.0288]),
coordinates: L.latLng([49.5732, 16.0288]),
displayName: "Walter-Meckauer-Street 20",
},
{
id: "6",
coordinates: L.latLng([49.5732, 11.0288]),
coordinates: L.latLng([49.5732, 17.0288]),
displayName: "49°26'46.6\"N 11°04'33.7\"E",
},
],
favourites: [
{
id: "1",
coordinates: L.latLng([49.5732, 11.0288]),
coordinates: L.latLng([49.5732, 12.0288]),
displayName: "Nuremberg",
},
{
id: "2",
coordinates: L.latLng([49.5732, 11.0288]),
coordinates: L.latLng([49.5732, 13.0288]),
displayName: "Munich",
},
],
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import App from "./App.tsx";
import "./index.css";
import { TabsContextProvider } from "./contexts/TabsContext.tsx";
import { MapContextProvider } from "./contexts/MapContext.tsx";
import { SearchContextProvider } from "./contexts/SearchContext.tsx";

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<TabsContextProvider>
<MapContextProvider>
<App />
<SearchContextProvider>
<App />
</SearchContextProvider>
</MapContextProvider>
</TabsContextProvider>
</React.StrictMode>
Expand Down

0 comments on commit 5b2982f

Please sign in to comment.