Skip to content

Commit e602895

Browse files
committed
refactor(tsx): convert Login view
1 parent 1ee0a53 commit e602895

File tree

1 file changed

+56
-50
lines changed

1 file changed

+56
-50
lines changed
Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import React, { useState } from 'react';
2-
import { useNavigate } from 'react-router-dom';
3-
// @material-ui/core components
1+
import React, { useState, FormEvent } from 'react';
2+
import { useNavigate, Navigate } from 'react-router-dom';
43
import FormControl from '@material-ui/core/FormControl';
54
import InputLabel from '@material-ui/core/InputLabel';
6-
// core components
75
import GridItem from '../../components/Grid/GridItem';
86
import GridContainer from '../../components/Grid/GridContainer';
97
import Input from '@material-ui/core/Input';
@@ -12,70 +10,83 @@ import Card from '../../components/Card/Card';
1210
import CardHeader from '../../components/Card/CardHeader';
1311
import CardBody from '../../components/Card/CardBody';
1412
import CardFooter from '../../components/Card/CardFooter';
15-
import axios from 'axios';
13+
import axios, { AxiosError } from 'axios';
1614
import logo from '../../assets/img/git-proxy.png';
1715
import { Badge, CircularProgress, Snackbar } from '@material-ui/core';
1816
import { getCookie } from '../../utils';
1917
import { useAuth } from '../../auth/AuthProvider';
2018

21-
const loginUrl = `${import.meta.env.VITE_API_URI}/api/auth/login`;
19+
interface LoginResponse {
20+
username: string;
21+
password: string;
22+
}
2223

23-
export default function UserProfile() {
24-
const [username, setUsername] = useState('');
25-
const [password, setPassword] = useState('');
26-
const [message, setMessage] = useState('');
27-
const [, setGitAccountError] = useState(false);
28-
const [isLoading, setIsLoading] = useState(false);
24+
const loginUrl = `${import.meta.env.VITE_API_URI}/api/auth/login`;
2925

26+
const Login: React.FC = () => {
3027
const navigate = useNavigate();
3128
const { refreshUser } = useAuth();
3229

33-
function validateForm() {
30+
const [username, setUsername] = useState<string>('');
31+
const [password, setPassword] = useState<string>('');
32+
const [message, setMessage] = useState<string>('');
33+
const [success, setSuccess] = useState<boolean>(false);
34+
const [gitAccountError, setGitAccountError] = useState<boolean>(false);
35+
const [isLoading, setIsLoading] = useState<boolean>(false);
36+
37+
function validateForm(): boolean {
3438
return (
3539
username.length > 0 && username.length < 100 && password.length > 0 && password.length < 200
3640
);
3741
}
3842

39-
function handleOIDCLogin() {
43+
function handleOIDCLogin(): void {
4044
window.location.href = `${import.meta.env.VITE_API_URI}/api/auth/oidc`;
4145
}
4246

43-
function handleSubmit(event) {
47+
function handleSubmit(event: FormEvent): void {
48+
event.preventDefault();
4449
setIsLoading(true);
50+
4551
axios
46-
.post(
52+
.post<LoginResponse>(
4753
loginUrl,
48-
{
49-
username: username,
50-
password: password,
51-
},
54+
{ username, password },
5255
{
5356
withCredentials: true,
5457
headers: {
5558
'Content-Type': 'application/json',
56-
'X-CSRF-TOKEN': getCookie('csrf'),
59+
'X-CSRF-TOKEN': getCookie('csrf') || '',
5760
},
5861
},
5962
)
60-
.then(function () {
63+
.then(() => {
6164
window.sessionStorage.setItem('git.proxy.login', 'success');
6265
setMessage('Success!');
63-
setIsLoading(false);
64-
refreshUser().then(() => navigate('/dashboard/repo'));
66+
setSuccess(true);
67+
refreshUser();
6568
})
66-
.catch(function (error) {
67-
if (error.response.status === 307) {
69+
.catch((error: AxiosError) => {
70+
if (error.response?.status === 307) {
6871
window.sessionStorage.setItem('git.proxy.login', 'success');
6972
setGitAccountError(true);
70-
} else if (error.response.status === 403) {
73+
} else if (error.response?.status === 403) {
7174
setMessage('You do not have the correct access permissions...');
7275
} else {
7376
setMessage('You entered an invalid username or password...');
7477
}
78+
})
79+
.finally(() => {
7580
setIsLoading(false);
7681
});
82+
}
7783

78-
event.preventDefault();
84+
if (gitAccountError) {
85+
return <Navigate to='/dashboard/profile' />;
86+
}
87+
88+
if (success) {
89+
return <Navigate to='/dashboard/repo' />;
7990
}
8091

8192
return (
@@ -91,43 +102,36 @@ export default function UserProfile() {
91102
<GridItem xs={12} sm={10} md={6} lg={4} xl={3}>
92103
<Card>
93104
<CardHeader color='primary'>
94-
<div
95-
style={{
96-
textAlign: 'center',
97-
marginRight: '10px',
98-
marginTop: '12px',
99-
marginBottom: '12px',
100-
}}
101-
>
105+
<div style={{ textAlign: 'center', margin: '12px 10px' }}>
102106
<img
103-
style={{ verticalAlign: 'middle', filter: 'brightness(0) invert(1)' }}
104-
width={'150px'}
105107
src={logo}
106108
alt='logo'
109+
width={150}
110+
style={{ verticalAlign: 'middle', filter: 'brightness(0) invert(1)' }}
107111
data-test='git-proxy-logo'
108112
/>
109113
</div>
110114
</CardHeader>
111115
<CardBody>
112116
<GridContainer>
113-
<GridItem xs={6} sm={6} md={6}>
114-
<FormControl>
115-
<InputLabel>Username</InputLabel>
117+
<GridItem xs={12} sm={12} md={12}>
118+
<FormControl fullWidth>
119+
<InputLabel htmlFor='username'>Username</InputLabel>
116120
<Input
117121
id='username'
118-
type='username'
122+
type='text'
119123
value={username}
120124
onChange={(e) => setUsername(e.target.value)}
121-
autoFocus={true}
125+
autoFocus
122126
data-test='username'
123127
/>
124128
</FormControl>
125129
</GridItem>
126130
</GridContainer>
127131
<GridContainer>
128-
<GridItem xs={6} sm={6} md={6}>
129-
<FormControl>
130-
<InputLabel>Password</InputLabel>
132+
<GridItem xs={12} sm={12} md={12}>
133+
<FormControl fullWidth>
134+
<InputLabel htmlFor='password'>Password</InputLabel>
131135
<Input
132136
id='password'
133137
type='password'
@@ -162,9 +166,9 @@ export default function UserProfile() {
162166
)}
163167
</CardFooter>
164168
</Card>
165-
<div style={{ textAlign: 'center', opacity: 0.9, fontSize: '12px' }}>
166-
<Badge overlap='rectangular' color='error' badgeContent={'NEW'} />{' '}
167-
<span style={{ paddingLeft: '20px' }}>
169+
<div style={{ textAlign: 'center', opacity: 0.9, fontSize: 12, marginTop: 20 }}>
170+
<Badge overlap='rectangular' color='error' badgeContent='NEW' />
171+
<span style={{ paddingLeft: 20 }}>
168172
View our <a href='/dashboard/push'>open source activity feed</a> or{' '}
169173
<a href='/dashboard/repo'>scroll through projects</a> we contribute to
170174
</span>
@@ -173,4 +177,6 @@ export default function UserProfile() {
173177
</GridContainer>
174178
</form>
175179
);
176-
}
180+
};
181+
182+
export default Login;

0 commit comments

Comments
 (0)