Skip to content

Commit

Permalink
Merge pull request #280 from ttaerrim/map-search-debounce
Browse files Browse the repository at this point in the history
[Modify] 맵 모달 debounce 적용
  • Loading branch information
ttaerrim authored Dec 4, 2023
2 parents 5f152d7 + 43b5a1f commit 0ce19c9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
11 changes: 8 additions & 3 deletions app/frontend/src/components/Modal/MapModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useEffect, useRef, useState } from 'react';

import { RequestCreateMogacoDto } from '@morak/apitype';
import { useQuery } from '@tanstack/react-query';
import { keepPreviousData, useQuery } from '@tanstack/react-query';

import { Button, Input } from '@/components';
import { useDebounce } from '@/hooks';
import { queryKeys } from '@/queries';
import { useModalAtom } from '@/stores';
import { sansBold14, sansRegular12, sansRegular14 } from '@/styles/font.css';
Expand All @@ -30,6 +31,7 @@ type Coord = {
export function MapModal({ saveAddress }: MapModalProps) {
const [open, setOpen] = useModalAtom();
const [searchKeyword, setSearchKeyword] = useState('');
const debouncedSearchKeyword = useDebounce(searchKeyword);
const [selectedAddress, setSelectedAddress] = useState('');
const [selectedCoord, setSelectedCoord] = useState<Coord>({
latitude: null,
Expand All @@ -44,15 +46,18 @@ export function MapModal({ saveAddress }: MapModalProps) {

const { data: tmapResponse } = useQuery({
...queryKeys.tmap.searchAddress({
searchKeyword,
searchKeyword: debouncedSearchKeyword,
}),
enabled: !!searchKeyword,
enabled: !!debouncedSearchKeyword,
placeholderData: keepPreviousData,
});

const addressData = tmapResponse?.searchPoiInfo?.pois?.poi;

const resetSearchKeyword = () => {
setSearchKeyword('');
};

const closeModal = () => {
resetSearchKeyword();
setOpen(false);
Expand Down
1 change: 1 addition & 0 deletions app/frontend/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useDebounce';
export * from './useModal';
export * from './useRouter';
export * from './useSetUserInfo';
20 changes: 20 additions & 0 deletions app/frontend/src/hooks/useDebounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useEffect, useState } from 'react';

const DEFAULT_DELAY = 500;

export function useDebounce<T>(value: T, delay?: number): T {
const [debouncedValue, setDebouncedValue] = useState<T>(value);

useEffect(() => {
const timer = setTimeout(
() => setDebouncedValue(value),
delay || DEFAULT_DELAY,
);

return () => {
clearTimeout(timer);
};
}, [value, delay]);

return debouncedValue;
}

0 comments on commit 0ce19c9

Please sign in to comment.