Skip to content

Commit

Permalink
fix: 드롭다운 초기값을 쿼리 스트링값으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
suzinxix committed Jun 24, 2024
1 parent 9fa70d3 commit dd1427f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useGetFilteredProducts } from "@/_home/hooks/useGetFilteredProducts";
import { useQueryParams } from "@/app/_home/hooks/useQueryParams";
import { Dropdown } from "@/components/Dropdown";
import { ORDER, DROPDOWN } from "@/components/Dropdown/constants";
import { findOptionByValue } from "@/components/Dropdown/utils";
import { NoData } from "@/components/NoData";
import cn from "@/utils/classNames";
import { createQueryString } from "@/utils/createQueryString";
Expand All @@ -32,13 +33,13 @@ export default function FilteredProducts({ category }: FilteredProductsProps) {
const { data: productData, fetchNextPage, hasNextPage } = useGetFilteredProducts(params.toString());

const { control } = useForm({ mode: "onBlur" });
const order = useWatch({ control, name: "order" });
const watchedOrder = useWatch({ control, name: "order" });

useEffect(() => {
if (order) {
router.push(`/?${createQueryString(QUERY.ORDER, order, searchParams)}`);
if (watchedOrder) {
router.push(`/?${createQueryString(QUERY.ORDER, watchedOrder, searchParams)}`);
}
}, [order, router, searchParams]);
}, [watchedOrder, router, searchParams]);

useEffect(() => {
if (inView && hasNextPage) {
Expand All @@ -47,6 +48,7 @@ export default function FilteredProducts({ category }: FilteredProductsProps) {
}, [inView, fetchNextPage, hasNextPage]);

const keyword = searchParams.get(QUERY.KEYWORD);
const order = searchParams.get(QUERY.ORDER);

return (
<div className={cn(styles.container)}>
Expand All @@ -60,7 +62,7 @@ export default function FilteredProducts({ category }: FilteredProductsProps) {
control={control}
name='order'
variant={DROPDOWN.ORDER}
placeholder={ORDER.PRODUCT[0].option}
placeholder={findOptionByValue(ORDER.PRODUCT, order)}
/>
</div>

Expand Down
6 changes: 6 additions & 0 deletions src/components/Dropdown/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ItemType } from "@/components/Dropdown/type";

export const findOptionByValue = (Items: ItemType[], valueToFind: string | null) => {
const result = Items.find(({ value }) => value === valueToFind);
return result ? result.option : Items[0].option;
};

0 comments on commit dd1427f

Please sign in to comment.