index.tsx
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React, { useEffect } from 'react';
import { RouteComponentProps } from 'react-router';
import { useRecoilState } from 'recoil';
import * as recoilUtil from '../../util/recoilUtil';
import * as Alert from '../../util/alertMessage';
import * as styled from './HeaderStyled';
import { authApi } from '../../api';
const headerImg = '/static/img/pharmacy.png';
const backButtonWhite = '/static/img/backButtonWhite.png';
const backButtonBlue = '/static/img/backButtonBlue.png';
const logout = '/static/img/logout.png';
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface HeaderProps extends RouteComponentProps {}
const Header = (props : HeaderProps) => {
const [token, setToken] = useRecoilState(recoilUtil.token);
const onLogout = () => {
const logout = async () => {
try {
await authApi.logout();
setToken(null);
Alert.onSuccess('로그인 페이지로 이동합니다.', () => props.history.push('/login'));
} catch(e) {
Alert.onError('알 수 없는 에러가 발생했습니다.', () => props.history.push('/'));
}
};
Alert.onCheck('정말 로그아웃 하시겠습니까?', logout, () => null);
};
const verifyToken = async () => {
try {
const result = await authApi.verifyToken(token);
if(result.statusText !== 'OK') {
setToken(null);
Alert.onError('세션이 만료되었습니다. 다시 로그인하세요.', () => props.history.push('/login'));
}
} catch(e) {
console.log(e);
setToken(null);
Alert.onError('세션이 만료되었습니다. 다시 로그인하세요.', () => props.history.push('/login'));
}
};
const onGoBack = () => {
props.history.goBack();
};
useEffect(() => {
verifyToken();
}, []);
return (
<styled.Container>
<styled.HeaderLeftWrapper>
{
(token && token.length && props.location.pathname !== '/') ?
<styled.Backbutton
onClick = {onGoBack}
>
<styled.BackbuttonImg src = {backButtonBlue}/>
<styled.BackbuttonText>뒤로 가기</styled.BackbuttonText>
</styled.Backbutton> : null
}
</styled.HeaderLeftWrapper>
<styled.HeaderCenterWrapper>
<styled.TitleImg src = {headerImg} />
<styled.Title>SMART MEDICINE BOX for Doctor</styled.Title>
</styled.HeaderCenterWrapper>
<styled.HeaderRightWrapper>
{
token && token.length ?
<styled.LogoutButton
onClick = {onLogout}
>
<styled.LogoutButtonImg src = {logout}/>
<styled.LogoutButtonText>로그아웃</styled.LogoutButtonText>
</styled.LogoutButton> : null
}
</styled.HeaderRightWrapper>
</styled.Container>
)
};
export default Header;