Skip to content

Commit

Permalink
fix: 응답 완전히 반환 시 요청 보낼 수 있도록 isLoading 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sooyeoniya committed Aug 23, 2024
1 parent ddccd18 commit e9bf111
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function getMessage(leftRatio: number, rightRatio: number, userSelectedOption: C
export default function RushCardCurrentRatio() {
const { userSelectedOption, cardOptions } = useRushGameStateContext();
const { toggleContents } = useToggleContents();
const fetchRushBalance = useFetchRushBalance();
const { fetchRushBalance, isLoadingRushBalance } = useFetchRushBalance();
const [throttle, setThrottle] = useState<boolean>(false);

const leftOptionRatio = getOptionRatio({
Expand All @@ -81,7 +81,7 @@ export default function RushCardCurrentRatio() {
});

const reloadThrottleButton = () => {
if (!throttle) {
if (!throttle && !isLoadingRushBalance) {
fetchRushBalance();
setThrottle(true);
setTimeout(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function SelectedCardCurrentRatio({ onClick }: SelectedCardDetailsProps) {

export default function SelectedCard() {
const { toggleContents, toggle } = useToggleContents({ useDuration: false });
const fetchRushBalance = useFetchRushBalance();
const { fetchRushBalance } = useFetchRushBalance();

const selectedCardToggle = () => {
toggle();
Expand Down
3 changes: 2 additions & 1 deletion client/src/hooks/RushGame/useFetchRushBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function useFetchRushBalance() {
const {
data: rushBalanceData,
isSuccess: isSuccessRushBalance,
isLoading: isLoadingRushBalance,
fetchData: getRushBalance,
} = useFetch<GetRushBalanceResponse, string>((token) => RushAPI.getRushBalance(token));

Expand Down Expand Up @@ -50,5 +51,5 @@ export default function useFetchRushBalance() {
}
}, [isSuccessRushBalance, rushBalanceData]);

return fetchRushBalance;
return { fetchRushBalance, isSuccessRushBalance, isLoadingRushBalance };
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RUSH_ACTION } from "@/types/rushGame.ts";

export function useFetchRushUserParticipationStatus() {
const dispatch = useRushGameDispatchContext();
const fetchRushBalance = useFetchRushBalance();
const { fetchRushBalance } = useFetchRushBalance();

const { data: userParticipatedStatus, fetchData: getRushUserParticipationStatus } = useFetch<
GetRushUserParticipationStatusResponse,
Expand Down
6 changes: 5 additions & 1 deletion client/src/hooks/useFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ export default function useFetch<T, P = void>(fetch: (params: P) => Promise<T>,
const [data, setData] = useState<T | null>(null);
const [isSuccess, setIsSuccess] = useState<boolean>(false);
const [isError, setIsError] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(false);

const { showBoundary } = useErrorBoundary();

const fetchData = async (params?: P) => {
setIsError(false);
setIsSuccess(false);
setIsLoading(true);

try {
const data = await fetch(params as P);
Expand All @@ -22,8 +24,10 @@ export default function useFetch<T, P = void>(fetch: (params: P) => Promise<T>,
if (showError) {
showBoundary(error);
}
} finally {
setIsLoading(false);
}
};

return { data, isSuccess, isError, fetchData };
return { data, isSuccess, isError, isLoading, fetchData };
}

0 comments on commit e9bf111

Please sign in to comment.