Skip to content

Commit

Permalink
Merge pull request #199 from softeerbootcamp4th/fix/#198-reload-pending
Browse files Browse the repository at this point in the history
[Fix] 응답 완전히 반환 시 요청 보낼 수 있도록 isLoading 추가
  • Loading branch information
jhj2713 authored Aug 23, 2024
2 parents ddccd18 + e9bf111 commit 28d7d39
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 28d7d39

Please sign in to comment.