-
Notifications
You must be signed in to change notification settings - Fork 2
[UNI-195] Post 요청에 대한 debounce 적용 #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { QueryClient, useMutation, UseMutationOptions } from "@tanstack/react-query"; | ||
import { useCallback, useRef } from "react"; | ||
|
||
export function useDebounceMutation<TData, TError, TVariables, TContext>( | ||
options: UseMutationOptions<TData, TError, TVariables, TContext>, | ||
delay: number, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delay같은 경우는 default값을 부여하는것이 좋을 것 같습니다! |
||
immediate: boolean, | ||
queryClient?: QueryClient, | ||
) { | ||
const mutation = useMutation<TData, TError, TVariables, TContext>(options, queryClient); | ||
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
|
||
const debouncedMutate = useCallback( | ||
(...args: Parameters<typeof mutation.mutate>) => { | ||
const later = () => { | ||
timeoutRef.current = null; | ||
if (!immediate) { | ||
mutation.mutate(...args); | ||
} | ||
}; | ||
|
||
const callNow = immediate && !timeoutRef.current; | ||
|
||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current); | ||
} | ||
timeoutRef.current = setTimeout(later, delay); | ||
|
||
if (callNow) { | ||
mutation.mutate(...args); | ||
} | ||
}, | ||
[mutation.mutate, delay, immediate], | ||
); | ||
|
||
return { | ||
...mutation, | ||
mutate: debouncedMutate, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { QueryClient, useMutation, UseMutationOptions, UseMutationResult } from "@tanstack/react-query"; | ||
import { NotFoundError, BadRequestError, ERROR_STATUS } from "../constant/error"; | ||
import { useEffect, useState } from "react"; | ||
import { useEffect, useRef, useState } from "react"; | ||
import { useDebounceMutation } from "./useDebounceMutation"; | ||
|
||
type Fallback = { | ||
[K in Exclude<ERROR_STATUS, ERROR_STATUS.INTERNAL_ERROR>]: { | ||
|
@@ -12,11 +13,11 @@ type Fallback = { | |
type HandleError = { | ||
fallback: Fallback; | ||
onClose?: () => void; | ||
} | ||
}; | ||
|
||
type UseMutationErrorReturn<TData, TError, TVariables, TContext> = [ | ||
React.FC, | ||
UseMutationResult<TData, TError, TVariables, TContext> | ||
UseMutationResult<TData, TError, TVariables, TContext>, | ||
]; | ||
|
||
export default function useMutationError<TData, TError, TVariables, TContext>( | ||
|
@@ -25,45 +26,49 @@ export default function useMutationError<TData, TError, TVariables, TContext>( | |
handleError?: HandleError, | ||
): UseMutationErrorReturn<TData, TError, TVariables, TContext> { | ||
const [isOpen, setOpen] = useState<boolean>(false); | ||
const result = useMutation<TData, TError, TVariables, TContext>(options, queryClient); | ||
const result = useDebounceMutation<TData, TError, TVariables, TContext>(options, 1000, true, queryClient); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 커스텀 훅을 생성해두길 잘했다고 생각이 드는 부분입니다.... 덕분에 유지보수 중 각 컴포넌트에서는 신경을 쓰지 않아도 된다는 점이 좋슴니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런것까지 보셨던건가요?? 만들어놓길 잘한 것 같습니다 |
||
|
||
const { isError, error } = result; | ||
|
||
useEffect(() => { | ||
setOpen(isError) | ||
}, [isError]) | ||
setOpen(isError); | ||
}, [isError]); | ||
|
||
const close = () => { | ||
if (handleError?.onClose) handleError?.onClose(); | ||
setOpen(false); | ||
} | ||
}; | ||
|
||
const Modal: React.FC = () => { | ||
if (!isOpen || !handleError || !error) return null; | ||
|
||
const { fallback } = handleError; | ||
|
||
let title: { mainTitle: string, subTitle: string[] } = { | ||
let title: { mainTitle: string; subTitle: string[] } = { | ||
mainTitle: "", | ||
subTitle: [], | ||
} | ||
}; | ||
|
||
if (error instanceof NotFoundError) { | ||
title = fallback[404] ?? title; | ||
} | ||
else if (error instanceof BadRequestError) { | ||
} else if (error instanceof BadRequestError) { | ||
title = fallback[400] ?? title; | ||
} | ||
else throw error; | ||
} else throw error; | ||
|
||
return ( | ||
<div className="fixed inset-0 flex items-center justify-center bg-[rgba(0,0,0,0.2)] z-100"> | ||
< div className="w-full max-w-[365px] flex flex-col bg-gray-100 rounded-400 overflow-hidden" > | ||
<div className="flex flex-col justify-center space-y-1 py-[25px]"> | ||
<p className="text-kor-body1 font-bold text-system-red">{title.mainTitle}</p> | ||
<div className="space-y-0"> | ||
{title.subTitle.map((_subtitle, index) => | ||
<p key={`error-modal-subtitle-${index}`} className="text-kor-body3 font-regular text-gray-700">{_subtitle}</p>)} | ||
{title.subTitle.map((_subtitle, index) => ( | ||
<p | ||
key={`error-modal-subtitle-${index}`} | ||
className="text-kor-body3 font-regular text-gray-700" | ||
> | ||
{_subtitle} | ||
</p> | ||
))} | ||
</div> | ||
</div> | ||
<button | ||
|
@@ -72,10 +77,10 @@ export default function useMutationError<TData, TError, TVariables, TContext>( | |
> | ||
확인 | ||
</button> | ||
</div > | ||
</div > | ||
) | ||
} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
return [Modal, result]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
되게 어려운 로직임에도 매우 훌륭하게 수행해주셔서 감사합니다!