diff --git a/client/src/api/apartments.ts b/client/src/api/apartments.ts index 66bec7d..5b86ad4 100644 --- a/client/src/api/apartments.ts +++ b/client/src/api/apartments.ts @@ -1,5 +1,6 @@ -import { apartmentsApi } from "./urls"; +import { apartmentsApi, currentApartmentApi } from "./urls"; import ky from "ky"; +import { IApartmentRes, IApartmentsRes } from "../types/apartmentsRes"; async function getApartments( token: string | null, @@ -11,7 +12,7 @@ async function getApartments( sortBy?: string | undefined, page?: number, perPage?: number -) { +): Promise { const roveHomeQuery = roveHomeFilters && roveHomeFilters.length !== 0 ? `rove_home=${roveHomeFilters.join(",")}` @@ -61,4 +62,18 @@ async function getApartments( return res.json(); } -export { getApartments }; +async function getCurrentApartment( + token: string | null, + id: string +): Promise { + const url = `${currentApartmentApi}/${id}`; + const res = await ky.get(url, { + headers: { + Authorization: `Zoho-oauthtoken ${token}`, + }, + }); + + return res.json(); +} + +export { getApartments, getCurrentApartment }; diff --git a/client/src/api/urls.ts b/client/src/api/urls.ts index 7213b07..c207bbd 100644 --- a/client/src/api/urls.ts +++ b/client/src/api/urls.ts @@ -3,6 +3,8 @@ const serverApi = import.meta.env.VITE_SERVER_API; const apartmentsApi = `${serverApi}/apartments`; +const currentApartmentApi = `${serverApi}/apartment`; + const updateAccessTokenApi = `${serverApi}/updateAccessToken`; -export { apartmentsApi, updateAccessTokenApi }; +export { apartmentsApi, updateAccessTokenApi, currentApartmentApi }; diff --git a/client/src/components/ApartmentSidebar.tsx b/client/src/components/ApartmentSidebar.tsx index 1661f26..2dc0491 100644 --- a/client/src/components/ApartmentSidebar.tsx +++ b/client/src/components/ApartmentSidebar.tsx @@ -2,7 +2,6 @@ import { useNavigate } from "react-router-dom"; import Button from "./Button"; // import { formatNumber } from "../calc/formatNumber"; import { IAparmentRes } from "../types/apartmentsRes"; -import { apartmentRoutes } from "../consts/apartmentsRoutes"; interface ApartmentSidebarProps { currentApartment: IAparmentRes; @@ -10,15 +9,12 @@ interface ApartmentSidebarProps { const ApartmentSidebar = ({ currentApartment }: ApartmentSidebarProps) => { const navigate = useNavigate(); - const route = apartmentRoutes.find( - (aprt) => aprt.type === currentApartment.Unit_Type - )?.virtualTour; const unitNo = currentApartment.Unit_No.split("-")[1]; const wing = currentApartment.Unit_No.split("-")[0] === "E" ? "East Wing" : "West Wing"; const handleOn3DTourClick = () => { - navigate(`../virtual-tour/${route ? route : "apartments-studio-1"}`); + navigate(`../virtual-tour/${currentApartment.id}`); }; return ( diff --git a/client/src/components/apartmentPage/ButtonPanel.tsx b/client/src/components/apartmentPage/ButtonPanel.tsx index c627db5..15dc8d2 100644 --- a/client/src/components/apartmentPage/ButtonPanel.tsx +++ b/client/src/components/apartmentPage/ButtonPanel.tsx @@ -3,6 +3,8 @@ import Button from "../Button"; import HeartIcon from "../icons/Heart"; import LeftArrowSliderIcon from "../icons/LeftArrowSliderIcon"; import { IAparmentRes } from "../../types/apartmentsRes"; +import useFavorites from "../../store/useFavorites"; +import { useEffect, useState } from "react"; interface ButtonPanelProps { currentApartment: IAparmentRes; @@ -11,11 +13,55 @@ interface ButtonPanelProps { const ButtonPanel = ({ currentApartment }: ButtonPanelProps) => { const navigate = useNavigate(); const unitNo = currentApartment.Unit_No.split("-")[1]; + const [isFavorite, setIsFavorite] = useState(false); + const { setFavorites } = useFavorites(); const handleOnBackClick = () => { - navigate(-1); + navigate("../search"); }; + const handleOnFavoriteClick = ( + e: React.MouseEvent + ) => { + e.stopPropagation(); + const favorites = localStorage.getItem("Favorites"); + + if (!favorites) { + setIsFavorite(true); + const updatedFavorites = JSON.stringify([currentApartment]); + + localStorage.setItem("Favorites", updatedFavorites); + } else { + const _favorites = JSON.parse(favorites) as IAparmentRes[]; + + if (_favorites.some((apart) => apart.id === currentApartment.id)) { + setIsFavorite(false); + const updatedFavorites = [..._favorites].filter( + (apart) => apart.id !== currentApartment.id + ); + const convertedFavorites = JSON.stringify(updatedFavorites); + setFavorites(updatedFavorites); + localStorage.setItem("Favorites", convertedFavorites); + } else { + setIsFavorite(true); + const updatedFavorites = [..._favorites, currentApartment]; + setFavorites(updatedFavorites); + const convertedFavorites = JSON.stringify(updatedFavorites); + localStorage.setItem("Favorites", convertedFavorites); + } + } + }; + + useEffect(() => { + const favorites = localStorage.getItem("Favorites"); + if (favorites) { + const _isFavorite = (JSON.parse(favorites) as IAparmentRes[]).some( + (apart) => apart.id === currentApartment.id + ); + setIsFavorite(_isFavorite); + } + }, [currentApartment.id]); + return (
@@ -33,7 +79,12 @@ const ButtonPanel = ({ currentApartment }: ButtonPanelProps) => {
-