Skip to content

Commit

Permalink
[Fix] 현금 프로필 창 에러 해결
Browse files Browse the repository at this point in the history
배포 환경에서 현금 프로필 창 기능 정상작동하도록 해결
에러메시지를 콘솔에 출력
Isseus #70
  • Loading branch information
김병현 authored and 김병현 committed Sep 16, 2023
1 parent f8fc83c commit 8b0a137
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 57 deletions.
18 changes: 10 additions & 8 deletions client/src/components/Profile/cashModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const CashModal: React.FC<{ onClose: () => void }> = ({ onClose }) => {
const createCashButtonText = "현금 생성";
const cashInputPlaceholder = "현금 입력";
const resetButtonText = "리셋";
const refreshButtonText ="새로고침";
// const refreshButtonText ="새로고침";

const dispatch = useDispatch();
const cashId = useSelector((state: RootState) => state.cash.cashId);
Expand All @@ -26,10 +26,12 @@ const CashModal: React.FC<{ onClose: () => void }> = ({ onClose }) => {
const [cashInput, setCashInput] = useState<string>('0');
const [initialAmount, setInitialAmount] = useState<string>('0'); // 현금 생성을 위한 상태 변수



// 현금 정보 재조회 함수
const refreshCashInfo = () => {
cashQuery.refetch(); // 현금 정보를 다시 가져옵니다.
};
// const refreshCashInfo = () => {
// cashQuery.refetch(); // 현금 정보를 다시 가져옵니다.
// };

// 현금 생성 및 cashId 전역 저장 함수
const handleCreateCash = () => {
Expand Down Expand Up @@ -76,7 +78,7 @@ const CashModal: React.FC<{ onClose: () => void }> = ({ onClose }) => {
<p style={{ display: 'inline-block', margin: '20px' }}>
현재 현금: {cashQuery.isLoading ? 'Loading...' : money.toLocaleString()}
</p>
<RefreshButton onClick={refreshCashInfo}>{refreshButtonText}</RefreshButton>
{/* <RefreshButton onClick={refreshCashInfo}>{refreshButtonText}</RefreshButton> */}
</div>
<div>
<CashInput
Expand Down Expand Up @@ -168,9 +170,9 @@ const CashCreationInput = styled.input`

const CreateCashButton = styled(StyledButton)``;

const RefreshButton = styled(StyledButton)`
margin-left:50px;
`;
// const RefreshButton = styled(StyledButton)`
// margin-left:50px;
// `;



58 changes: 9 additions & 49 deletions client/src/hooks/useCash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useQuery, useMutation } from 'react-query';
import axios, { AxiosError } from 'axios';
import axios from 'axios';
import { useDispatch } from 'react-redux';
import { setCashId, setMoney } from '../reducer/cash/cashSlice';
import { useSelector } from 'react-redux';
Expand All @@ -12,7 +12,6 @@ const getAuthHeader = () => {
return {
'Authorization': `${accessToken}`
};

};

export const useCreateCash = () => {
Expand All @@ -21,22 +20,14 @@ export const useCreateCash = () => {
headers: getAuthHeader()
}), {
onSuccess: (res) => {
// 200번 응답 처리
dispatch(setCashId(res.data.cashId));
dispatch(setMoney(res.data.money));
},
onError: (error) => {
const axiosError = error as AxiosError<ErrorResponse>;
if (axiosError?.response?.status === 400 || axiosError?.response?.status === 401) {
// 에러 메시지 출력
const errorMessage = axiosError?.response?.data?.message || 'Unknown error occurred.';
alert(errorMessage);
}
console.error(error);
}
});
}


};

export const useGetCash = () => {
const dispatch = useDispatch();
Expand All @@ -48,63 +39,32 @@ export const useGetCash = () => {

return useQuery('money', queryFn, {
onSuccess: (res) => {
// 200번 응답 처리
dispatch(setCashId(res.data.cashId));
dispatch(setMoney(res.data.money));
},
onError: (error) => {
const axiosError = error as AxiosError<ErrorResponse>;
switch (axiosError?.response?.status) {
case 400:
case 401:
case 404: {
// 중괄호 내에서 변수 선언
const errorMessage = axiosError?.response?.data?.message || 'Unknown error occurred.';
alert(errorMessage);
break;
}
default:
alert('Unknown error occurred.');
break;
}
console.error(error);
},
refetchInterval: false, // 자동 재요청 비활성화
retry: false // 재시도 비활성화
refetchInterval: false,
retry: false,
staleTime: 1000 * 60 * 5
});
}


export const useResetCash = () => {
const cashId = useSelector((state: RootState) => state.cash.cashId); // cashId를 전역 상태에서 가져옵니다.
const cashId = useSelector((state: RootState) => state.cash.cashId);
const dispatch = useDispatch();

return useMutation((data: { money: string }) => axios.patch(`${BASE_URL}/cash/${cashId}`, { "money": data.money }, {
headers: getAuthHeader()
}), {
onSuccess: (data) => {
// 200번 응답 처리
dispatch(setCashId(data.data.cashId));
dispatch(setMoney(data.data.money));
},
onError: (error) => {
const axiosError = error as AxiosError<ErrorResponse>;
switch (axiosError?.response?.status) {
case 400:
case 401:
case 404: {
const errorMessage = axiosError?.response?.data?.message || 'Unknown error occurred.';
alert(errorMessage);
break;
}
default:
alert('Unknown error occurred.');
break;
}
console.error(error);
}
});
}


interface ErrorResponse {
message: string;
}

0 comments on commit 8b0a137

Please sign in to comment.