Skip to content

[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

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions uniro_frontend/src/hooks/useDebounceMutation.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

되게 어려운 로직임에도 매우 훌륭하게 수행해주셔서 감사합니다!

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,
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
};
}
43 changes: 24 additions & 19 deletions uniro_frontend/src/hooks/useMutationError.tsx
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>]: {
Expand All @@ -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>(
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

커스텀 훅을 생성해두길 잘했다고 생각이 드는 부분입니다....

덕분에 유지보수 중 각 컴포넌트에서는 신경을 쓰지 않아도 된다는 점이 좋슴니다

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand All @@ -72,10 +77,10 @@ export default function useMutationError<TData, TError, TVariables, TContext>(
>
확인
</button>
</div >
</div >
)
}
</div>
</div>
);
};

return [Modal, result];
}
58 changes: 32 additions & 26 deletions uniro_frontend/src/pages/reportForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,33 +113,36 @@ const ReportForm = () => {
}
};

const [ErrorModal, { mutate }] = useMutationError({
mutationFn: () =>
postReport(university?.id ?? 1001, routeId, {
dangerFactors: formData.dangerIssues,
cautionFactors: formData.cautionIssues,
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["report", university?.id ?? 1001, routeId] });
openSuccess();
},
onError: () => {
setErrorTitle("제보에 실패하였습니다");
const [ErrorModal, { mutate, status }] = useMutationError(
{
mutationFn: () =>
postReport(university?.id ?? 1001, routeId, {
dangerFactors: formData.dangerIssues,
cautionFactors: formData.cautionIssues,
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["report", university?.id ?? 1001, routeId] });
openSuccess();
},
onError: () => {
setErrorTitle("제보에 실패하였습니다");
},
},
}, undefined, {
fallback: {
400: {
mainTitle: '불편한 길 제보 실패',
subTitle: ['잘못된 요청입니다.', '잠시 후 다시 시도 부탁드립니다.'],
undefined,
{
fallback: {
400: {
mainTitle: "불편한 길 제보 실패",
subTitle: ["잘못된 요청입니다.", "잠시 후 다시 시도 부탁드립니다."],
},
404: {
mainTitle: "불편한 길 제보 실패",
subTitle: ["해당 경로는 다른 사용자에 의해 삭제되어,", "지도 화면에서 바로 확인할 수 있어요."],
},
},
404: {
mainTitle: '불편한 길 제보 실패',
subTitle: ['해당 경로는 다른 사용자에 의해 삭제되어,', '지도 화면에서 바로 확인할 수 있어요.']
}
onClose: redirectToMap,
},
onClose: redirectToMap

});
);

return (
<div className="flex flex-col h-dvh w-full max-w-[450px] mx-auto relative">
Expand All @@ -158,8 +161,11 @@ const ReportForm = () => {
/>
</div>
<div className="mb-4 w-full px-4">
<Button onClick={mutate} variant={disabled ? "disabled" : "primary"}>
제보하기
<Button
onClick={mutate}
variant={status === "pending" || status === "success" || disabled ? "disabled" : "primary"}
>
{status === "pending" ? "제보하는 중.." : "제보하기"}
</Button>
</div>
<SuccessModal>
Expand Down
Loading