Skip to content

Commit

Permalink
feat: connect with logout, withdraw, refresh api - #54
Browse files Browse the repository at this point in the history
  • Loading branch information
byein committed Jan 18, 2023
1 parent 5f3ba80 commit 1a74a69
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 62 deletions.
108 changes: 78 additions & 30 deletions baedalmate/src/components/atoms/Bottom/MyPageBottom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,74 @@ import {StyleSheet, TouchableOpacity, View} from 'react-native';
import {DARK_GRAY_COLOR} from 'themes/theme';
import {TextKRReg} from 'themes/text';
import {UsePopup} from 'components/utils/usePopup';
import {deactivateAPI, logoutAPI} from 'components/utils/api/Login';
import {useNavigation} from '@react-navigation/native';

export type BtnWithoutTextProps = {
onPress(): void;
};

const MyPageBottom = () => {
const [logoutModal, setLogoutModal] = useState(false);
const handleLogoutModal = () => {
logoutModal ? setLogoutModal(false) : setLogoutModal(true);
const navigation = useNavigation();
const [modal, setModal] = useState(false);
const handleModal = () => {
modal ? setModal(false) : setModal(true);
};

const [modalData, setModalData] = useState<{
title: string;
description: string;
modal: boolean;
handleModal: any;
confirmEvent: any;
choiceCnt: number;
}>();
const logout = async () => {
const result = await logoutAPI();
console.log(result);
if (result.status == 200) {
// handleModal();
setTimeout(() => {
navigation.navigate('Login' as never);
navigation.reset({
index: 0,
routes: [{name: 'Login' as never}],
});
}, 1000);
}
};

const withdraw = async () => {
const result = await deactivateAPI();
console.log(result);
if (result.status == 200) {
// handleModal();
setTimeout(() => {
navigation.navigate('Login' as never);
navigation.reset({
index: 0,
routes: [{name: 'Login' as never}],
});
}, 1000);
}
};
const logoutModalData = {
title: '로그아웃 하시겠습니까?',
description:
'로그아웃시, 다음 앱 접속시에 다시 로그인을 해야 이용 가능합니다.',
modal: logoutModal,
handleModal: handleLogoutModal,
};
const [withdrawModal, setWithdrawModal] = useState(false);
const handleWithdrawModal = () => {
withdrawModal ? setWithdrawModal(false) : setWithdrawModal(true);
modal: modal,
handleModal: handleModal,
confirmEvent: logout,
choiceCnt: 2,
};

const withdrawModalData = {
title: '탈퇴 하시겠습니까?',
description: '회원 탈퇴시, 현재까지 모집글에 참여한 기록이 삭제됩니다.',
modal: withdrawModal,
handleModal: handleWithdrawModal,
modal: modal,
handleModal: handleModal,
confirmEvent: withdraw,
choiceCnt: 2,
};

return (
Expand All @@ -46,16 +88,20 @@ const MyPageBottom = () => {
flex: 1,
}}
onPress={() => {
handleLogoutModal();
setModalData(logoutModalData);
handleModal();
}}>
<UsePopup
title={logoutModalData.title}
description={logoutModalData.description}
modal={logoutModal}
handleModal={handleLogoutModal}
// 추가 필요
confirmEvent={undefined}
/>
{modalData && (
<UsePopup
title={modalData.title}
description={modalData.description}
modal={modal}
handleModal={handleModal}
// 추가 필요
confirmEvent={modalData.confirmEvent}
choiceCnt={modalData.choiceCnt}
/>
)}
<TextKRReg
style={{
paddingVertical: 15,
Expand All @@ -75,21 +121,23 @@ const MyPageBottom = () => {
flex: 1,
}}
onPress={() => {
handleWithdrawModal();
setModalData(withdrawModalData);
handleModal();
}}>
<UsePopup
title={withdrawModalData.title}
description={withdrawModalData.description}
modal={withdrawModal}
handleModal={handleWithdrawModal}
// 추가 필요
confirmEvent={undefined}
/>
{modalData && (
<UsePopup
title={modalData.title}
description={modalData.description}
modal={modal}
handleModal={handleModal}
confirmEvent={modalData.confirmEvent}
choiceCnt={modalData.choiceCnt}
/>
)}
<TextKRReg
style={{
paddingVertical: 15,
textAlign: 'center',
// borderWidth: 1,
borderRadius: 10,
fontSize: 14,
lineHeight: 24,
Expand Down
70 changes: 60 additions & 10 deletions baedalmate/src/components/pages/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import axios from 'axios';
import {url} from '../../../App';
import {getJWTToken} from 'components/utils/api/Recruit';
import {getUserAPI} from 'components/utils/api/User';
import {refreshAPI} from 'components/utils/api/Login';
import AsyncStorage from '@react-native-async-storage/async-storage';
export const userURL = url + '/api/v1/user';
export const recruitListURL = url + '/api/v1/recruit/list';
export const mainRecruitListURL = url + '/api/v1/recruit/main/list';
Expand Down Expand Up @@ -99,8 +101,12 @@ export const getImages = async fileOriginName => {
}
return false;
})
.catch(function (error) {
.catch(async function (error) {
console.log(error);
if (error.response.data.code === 401) {
const result = await refreshAPI();
console.log(result);
}
return false;
});
return BoardListData;
Expand All @@ -111,7 +117,6 @@ const Main: React.FunctionComponent<MainProps> = props => {
//user 관련 state
const [nickname, setNickname] = useState('캡스톤');
const [dormitory, setDormitory] = useState('');
const [profileImage, setProfileImage] = useState('');
//recruit 관련 state
const [mainRecruitList, setMainRecruitList] =
useState<eachMainRecruitListI[]>();
Expand Down Expand Up @@ -206,23 +211,46 @@ const Main: React.FunctionComponent<MainProps> = props => {
setMainRecruitList(response.data.recruitList);

return response.data.recruitList;
} else if (response.status === 401) {
const result = await refreshAPI();
console.log(result);
const tokens = await result.data;
const token = tokens.accessToken;
const refToken = tokens.refreshToken;

AsyncStorage.multiSet([
['@BaedalMate_JWTAccessToken', token],
['@BaedalMate_JWTRefreshToken', refToken],
]);
} else if (response.status === 403) {
props.navigation.navigate('거점 인증');
}
return false;
})
.catch(function (error) {
.catch(async function (error) {
console.log(error);
console.log(error.response.status);
if (error.response.status === 403) {
props.navigation.navigate('거점 인증');
} else if (error.response.status === 401) {
const result = await refreshAPI();
console.log(result);
const tokens = await result.data;
const token = tokens.accessToken;
const refToken = tokens.refreshToken;

AsyncStorage.multiSet([
['@BaedalMate_JWTAccessToken', token],
['@BaedalMate_JWTRefreshToken', refToken],
]);
}
return false;
});
console.log('BoardListData', BoardListData);
return BoardListData;
} catch (error) {
console.log(error);

return false;
}
};
Expand All @@ -243,20 +271,42 @@ const Main: React.FunctionComponent<MainProps> = props => {
// sort: 'deadlineDate',
},
})
.then(function (response) {
.then(async function (response) {
if (response.status === 200) {
console.log(response.data);
setMainTagRecruitList(response.data);
return response.data.recruitList;
} else if (response.status === 403) {
props.navigation.navigate('거점 인증');
} else if (response.status === 401) {
const result = await refreshAPI();
console.log(result);
const tokens = await result.data;
const token = tokens.accessToken;
const refToken = tokens.refreshToken;

AsyncStorage.multiSet([
['@BaedalMate_JWTAccessToken', token],
['@BaedalMate_JWTRefreshToken', refToken],
]);
}
return false;
})
.catch(function (error) {
.catch(async function (error) {
console.log(error);
if (error.response.status === 403) {
props.navigation.navigate('거점 인증');
} else if (error.response.status === 401) {
const result = await refreshAPI();
console.log(result);
const tokens = await result.data;
const token = tokens.accessToken;
const refToken = tokens.refreshToken;

AsyncStorage.multiSet([
['@BaedalMate_JWTAccessToken', token],
['@BaedalMate_JWTRefreshToken', refToken],
]);
}
return false;
});
Expand Down Expand Up @@ -329,11 +379,11 @@ const Main: React.FunctionComponent<MainProps> = props => {
// setTarget(originTarget);
// };

useEffect(() => {
getUserData();
getMainRecruitList();
getMainTagRecruitList();
}, []);
// useEffect(() => {
// getUserData();
// getMainRecruitList();
// getMainTagRecruitList();
// }, []);

// 렌더링 시 유저 정보 받아오기
useEffect(() => {
Expand Down
Loading

0 comments on commit 1a74a69

Please sign in to comment.