Skip to content

Commit

Permalink
Merge pull request Yanabada#257 from Yanabada/feature/Yanabada#256
Browse files Browse the repository at this point in the history
Feature/Yanabada#256
  • Loading branch information
Gaoridang authored Feb 22, 2024
2 parents 81f0370 + ac0e413 commit 35c4c24
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/pages/home/components/Banner/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const ImageContainer = styled.div`
export const Image = styled(motion.img)`
width: 100%;
height: 100%;
object-fit: fill;
object-fit: cover;
border-radius: 10px;
cursor: pointer;
`;
Expand Down
5 changes: 2 additions & 3 deletions src/pages/products/components/KakaoMap/ProductsMarkers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@ export const ProductsMarkers = ({
const map = useMap();

const handleSelect = (product: ProductType) => {
// positions, productId, setProductId
const { latitude, longitude } = product;
setSelectedProductId(product.id);
map.panTo(new kakao.maps.LatLng(latitude, longitude));
console.log(selectedProductId, product.id);
};
console.log(selectedProductId);

return products.map((product) => (
<CustomOverlayMap key={product.id} position={{ lat: product.latitude, lng: product.longitude }}>
<S.Pin
data-selected={selectedProductId === product.id ? "true" : "false"}
onClick={() => handleSelect(product)}
>
{getPriceWithComma(product.price)}
{getPriceWithComma(product.sellingPrice)}
</S.Pin>
</CustomOverlayMap>
));
Expand Down
8 changes: 4 additions & 4 deletions src/pages/products/components/KakaoMap/Research.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const Research = () => {
const [searchParams, setSearchParams] = useSearchParams();

const handleResearch = () => {
searchParams.set("swx", position.smallX.toString());
searchParams.set("swy", position.smallY.toString());
searchParams.set("nex", position.bigX.toString());
searchParams.set("ney", position.bigY.toString());
searchParams.set("smallX", position.smallX.toString());
searchParams.set("smallY", position.smallY.toString());
searchParams.set("bigX", position.bigX.toString());
searchParams.set("bigY", position.bigY.toString());
setSearchParams(searchParams);
setMapMoved(false);
};
Expand Down
4 changes: 3 additions & 1 deletion src/pages/products/components/KakaoMap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ProductCardForMap from "../ProductCard/ProductCardForMap";
import { MyPositionMarker } from "./MyPositionMarker";
import Research from "./Research";
import { ProductType } from "@pages/products/types/productsType";
import { useSearchParams } from "react-router-dom";

export interface UserPositionType {
lat: number | null;
Expand Down Expand Up @@ -38,6 +39,7 @@ const KakaoMap = ({ products }: MapProps) => {
errorMessage: "",
isLoading: true
});
const searchParams = useSearchParams();

useEffect(() => {
if (products.length > 0) {
Expand Down Expand Up @@ -96,7 +98,7 @@ const KakaoMap = ({ products }: MapProps) => {
height: "100%",
zIndex: 1000
}}
level={5}
level={searchParams[0].size === 0 ? 13 : 5}
ref={mapRef}
>
{userPosition.lat && userPosition.lng ? <MyPositionMarker position={userPosition} /> : null}
Expand Down
95 changes: 47 additions & 48 deletions src/pages/products/components/ProductList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,67 +1,64 @@
import useIntersect from "@pages/products/hooks/useIntersect";
import * as S from "../../styles/style";
import KakaoMap from "../KakaoMap";
import ProductCard from "../ProductCard";
import useProducts from "@pages/products/api/queries";
import { useMapState } from "@pages/products/stores/mapStore";
import { ScrollRestoration, useSearchParams } from "react-router-dom";
import { Category, Option, OrderState } from "@pages/products/api/products";
import { useEffect, useState } from "react";
import {
Category,
GetProductsRequestParams,
Option,
OrderState
} from "@pages/products/api/products";
import { useMemo } from "react";
import useIntersect from "@pages/products/hooks/useIntersect";
import KakaoMap from "../KakaoMap";
import NoProduct from "../NoProduct";
import { useMapState } from "@pages/products/stores/mapStore";
import GoToMapButton from "../ToMapButton";

const ProductList = () => {
const [searchParams] = useSearchParams();
const [queryParams, setQueryParams] = useState({
options: searchParams.getAll("options"),
order: searchParams.get("order"),
category: searchParams.get("category"),
keyword: searchParams.get("keyword"),
checkInDate: searchParams.get("checkInDate"),
checkOutDate: searchParams.get("checkOutDate"),
swx: Number(searchParams.get("swx")),
swy: Number(searchParams.get("swy")),
nex: Number(searchParams.get("nex")),
ney: Number(searchParams.get("ney"))
});
const searchParamString = searchParams.toString();

const queryParams = useMemo(() => {
const params = new URLSearchParams(searchParamString);

return {
options: params.getAll("options") as Option[],
order: params.get("order") as OrderState,
category: params.get("category") as Category,
keyword: params.get("keyword"),
checkInDate: params.get("checkInDate"),
checkOutDate: params.get("checkOutDate"),
smallX: Number(params.get("smallX")),
smallY: Number(params.get("smallY")),
bigX: Number(params.get("bigX")),
bigY: Number(params.get("bigY"))
};
}, [searchParamString]);

useEffect(() => {
setQueryParams({
options: searchParams.getAll("options"),
order: searchParams.get("order"),
category: searchParams.get("category"),
keyword: searchParams.get("keyword"),
checkInDate: searchParams.get("checkInDate"),
checkOutDate: searchParams.get("checkOutDate"),
swx: Number(searchParams.get("swx")),
swy: Number(searchParams.get("swy")),
nex: Number(searchParams.get("nex")),
ney: Number(searchParams.get("ney"))
const removeFalsy = <T extends object>(
obj: T,
...rest: Array<Record<string, any>>
): Partial<T> => {
const result = Object.assign({}, ...rest);

Object.entries(obj).forEach(([key, value]) => {
if (value && value !== 0) {
result[key] = value;
}
});
}, [searchParams]);

return result;
};

const {
data: products,
hasNextPage,
isFetching,
fetchNextPage
} = useProducts({
...(queryParams.options && { options: queryParams.options as Option[] }),
...(queryParams.order && { order: queryParams.order as OrderState }),
...(queryParams.category && { category: queryParams.category as Category }),
...(queryParams.checkInDate && { checkInDate: queryParams.checkInDate }),
...(queryParams.checkOutDate && { checkOutDate: queryParams.checkOutDate }),
...(queryParams.keyword && { keyword: queryParams.keyword }),
...(queryParams.swx && { smallX: queryParams.swx }),
...(queryParams.swy && { smallY: queryParams.swy }),
...(queryParams.nex && { bigX: queryParams.nex }),
...(queryParams.ney && { bigY: queryParams.ney }),
size: 10
});
const { isMapOpen, setHasProducts, hasProducts } = useMapState();
} = useProducts(removeFalsy<Partial<GetProductsRequestParams>>(queryParams, { size: 10 }));

useEffect(() => {
setHasProducts(products.length);
}, [products.length, setHasProducts]);
const { setMapOpen, isMapOpen } = useMapState();

const ref = useIntersect<HTMLDivElement>(async (entry, observer) => {
observer.unobserve(entry.target);
Expand All @@ -77,7 +74,7 @@ const ProductList = () => {
<S.MapContainer>
<KakaoMap products={products} />
</S.MapContainer>
) : !hasProducts ? (
) : products.length === 0 ? (
<NoProduct />
) : (
<>
Expand All @@ -87,9 +84,11 @@ const ProductList = () => {
<ProductCard key={product.id} product={product} />
))}
<div className="observer" ref={ref} />
<GoToMapButton handleClick={setMapOpen} />
</S.ProductCardWrapper>
</>
)}
;
</>
);
};
Expand Down
6 changes: 2 additions & 4 deletions src/pages/products/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ import UpperNavBar from "@components/navBar/upperNavBar";
import SearchBar from "./components/SearchBar";
import * as S from "./styles/style";
import CategoryTab from "./components/CategoryTab";
import GoToMapButton from "./components/ToMapButton";
import { Suspense } from "react";
import { Link, useLocation, useNavigate } from "react-router-dom";
import ProductList from "./components/ProductList";
import { useMapState } from "./stores/mapStore";
import OptionTab from "./components/OptionTap";
import Order from "./components/Order";
import { Suspense } from "react";
import LoadingCircle from "@components/loading";

const Products = () => {
const { isMapOpen, setMapOpen, hasProducts } = useMapState();
const { isMapOpen, setMapOpen } = useMapState();
const location = useLocation();
const navigate = useNavigate();

Expand Down Expand Up @@ -49,7 +48,6 @@ const Products = () => {
<Suspense fallback={<LoadingCircle width="200px" />}>
<ProductList />
</Suspense>
{!isMapOpen && hasProducts && <GoToMapButton handleClick={setMapOpen} />}
</>
);
};
Expand Down

0 comments on commit 35c4c24

Please sign in to comment.