Skip to content

Commit

Permalink
Merge pull request #240 from SOPT-all/develop
Browse files Browse the repository at this point in the history
[DEPLOY] dev to main
  • Loading branch information
seong-hui authored Jan 25, 2025
2 parents 42c9af1 + 36228fd commit 613a197
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
37 changes: 26 additions & 11 deletions src/components/filter/priceSlider/PriceSlider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import debounce from '@hooks/debounce';
import { useAtom } from 'jotai';
import React from 'react';
import React, { useCallback, useState } from 'react';
import { priceAtom } from 'src/store/store';

import * as styles from './priceSlider.css';
Expand All @@ -8,21 +9,35 @@ const PriceSlider = () => {
const MIN_PRICE = 0;
const MAX_PRICE = 30;
const [price, setPrice] = useAtom(priceAtom);
const [localPrice, setLocalPrice] = useState(price);

// ๋””๋ฐ”์šด์Šค ์ฒ˜๋ฆฌํ•œ price ์—…๋ฐ์ดํŠธ ํ•ธ๋“ค๋Ÿฌ
const handleDebounceSetPrice = useCallback(
debounce((updatedPrice: { minPrice: number; maxPrice: number }) => {
setPrice(updatedPrice);
}, 300),
[],
);

const handleMinChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const value = Math.min(Number(event.target.value), price.maxPrice - 1);
const updatedPrice = { ...price, minPrice: value };
setPrice(updatedPrice);
const value = Math.min(Number(event.target.value), localPrice.maxPrice - 1);
const updatedLocalPrice = { ...localPrice, minPrice: value };

setLocalPrice(updatedLocalPrice); // ๋กœ์ปฌ ์ƒํƒœ ์—…๋ฐ์ดํŠธ
handleDebounceSetPrice(updatedLocalPrice); // ๋””๋ฐ”์šด์Šค๋œ ์—…๋ฐ์ดํŠธ
};

const handleMaxChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const value = Math.max(Number(event.target.value), price.minPrice + 1);
const updatedPrice = { ...price, maxPrice: value };
setPrice(updatedPrice);
const value = Math.max(Number(event.target.value), localPrice.minPrice + 1);
const updatedLocalPrice = { ...localPrice, maxPrice: value };

setLocalPrice(updatedLocalPrice); // ๋กœ์ปฌ ์ƒํƒœ ์—…๋ฐ์ดํŠธ
handleDebounceSetPrice(updatedLocalPrice); // ๋””๋ฐ”์šด์Šค๋œ ์—…๋ฐ์ดํŠธ
};

const getTrackStyle = () => ({
left: `${((price.minPrice - MIN_PRICE) / (MAX_PRICE - MIN_PRICE)) * 100}%`,
width: `${((price.maxPrice - price.minPrice) / (MAX_PRICE - MIN_PRICE)) * 100}%`,
left: `${((localPrice.minPrice - MIN_PRICE) / (MAX_PRICE - MIN_PRICE)) * 100}%`,
width: `${((localPrice.maxPrice - localPrice.minPrice) / (MAX_PRICE - MIN_PRICE)) * 100}%`,
});

return (
Expand All @@ -41,15 +56,15 @@ const PriceSlider = () => {
type="range"
min={MIN_PRICE}
max={MAX_PRICE}
value={price.minPrice}
value={localPrice.minPrice}
onChange={handleMinChange}
className={styles.thumb}
/>
<input
type="range"
min={MIN_PRICE}
max={MAX_PRICE}
value={price.maxPrice}
value={localPrice.maxPrice}
onChange={handleMaxChange}
className={styles.thumb}
/>
Expand Down
9 changes: 4 additions & 5 deletions src/hooks/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const debounce = <T extends (...args: unknown[]) => void>(
const debounce = <T extends (...args: Parameters<T>) => void>(
callback: T,
delay: number,
): ((...args: Parameters<T>) => void) => {
let timer: ReturnType<typeof setTimeout> | null = null; // ๋ช…ํ™•ํ•œ ํƒ€์ž… ์ง€์ •
const callbackRef = callback; // ์ตœ์‹  ์ฝœ๋ฐฑ์„ ์ฐธ์กฐํ•˜๋Š” ๋ณ€์ˆ˜
let timer: ReturnType<typeof setTimeout> | null = null;

return (...args: Parameters<T>) => {
if (timer) clearTimeout(timer);
if (timer) clearTimeout(timer); // ๊ธฐ์กด ํƒ€์ด๋จธ ์ œ๊ฑฐ
timer = setTimeout(() => {
callbackRef(...args); // ์ตœ์‹  callback ํ˜ธ์ถœ
callback(...args); // ์ตœ์‹  callback ํ˜ธ์ถœ
}, delay);
};
};
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useFilter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import useFetchFilteredList from '@apis/filter';
import { fetchFilteredCount } from '@apis/filter/axios';
import { useQuery } from '@tanstack/react-query';
import { useQuery, keepPreviousData } from '@tanstack/react-query';
import { useAtom } from 'jotai';
import { useNavigate } from 'react-router-dom';
import queryClient from 'src/queryClient';
Expand All @@ -21,7 +21,7 @@ const useFilter = () => {

const getGroupedFilters = () => filterListInstance.getGroupedStates();

const { data: totalCount = 0 } = useQuery({
const { data: totalCount } = useQuery({
queryKey: ['filteredCount', price, content],
queryFn: async () => {
const groupedFilters = getGroupedFilters();
Expand All @@ -36,6 +36,7 @@ const useFilter = () => {
return response.count;
},
staleTime: 0,
placeholderData: keepPreviousData,
});

// ํ•„ํ„ฐ ์ƒํƒœ ํ† ๊ธ€
Expand Down

0 comments on commit 613a197

Please sign in to comment.