Skip to content

Commit 35513aa

Browse files
authored
Merge pull request #98 from gbowne1/gbowne1-add-initial-login-ui
Adding initial login UI and dropdown icon state change
2 parents 8a02f79 + 4740dc4 commit 35513aa

2 files changed

Lines changed: 80 additions & 9 deletions

File tree

src/components/Dropdown/Dropdown.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as React from 'react';
1+
import React, { useState } from 'react';
2+
import { useNavigate } from 'react-router-dom';
23
import Box from '@mui/material/Box';
34
import Avatar from '@mui/material/Avatar';
45
import Menu from '@mui/material/Menu';
@@ -8,25 +9,32 @@ import Divider from '@mui/material/Divider';
89
import IconButton from '@mui/material/IconButton';
910
import Tooltip from '@mui/material/Tooltip';
1011
import Settings from '@mui/icons-material/Settings';
11-
import Logout from '@mui/icons-material/Logout';
12+
import LogoutIcon from '@mui/icons-material/Logout';
1213
import LoginIcon from '@mui/icons-material/Login';
1314
import HelpIcon from '@mui/icons-material/Help';
1415
import MessageIcon from '@mui/icons-material/Message';
1516

1617
export default function Dropdown() {
17-
const [anchorEl, setAnchorEl] = React.useState(null);
18-
const [logIn, setLogIn] = React.useState(true);
18+
const [anchorEl, setAnchorEl] = useState(null);
19+
const [loggedIn, setLoggedIn] = useState(true); // Change the state name to 'loggedIn'
1920
const open = Boolean(anchorEl);
21+
const navigate = useNavigate();
22+
2023
const handleClick = (event) => {
2124
setAnchorEl(event.currentTarget);
2225
};
26+
2327
const handleClose = () => {
2428
setAnchorEl(null);
2529
};
2630

2731
const handleAccount = () => {
28-
logIn ? setLogIn(false) : setLogIn(true);
32+
setLoggedIn(!loggedIn); // Toggle the loggedIn state
33+
if (!loggedIn) {
34+
navigate('/login'); // Navigate to the login route
35+
}
2936
};
37+
3038
return (
3139
<React.Fragment>
3240
<Box
@@ -98,10 +106,10 @@ export default function Dropdown() {
98106
</ListItemIcon>
99107
Settings
100108
</MenuItem>
101-
{logIn ? (
109+
{loggedIn ? ( // Use the 'loggedIn' state to conditionally render the Logout/Login menu item
102110
<MenuItem onClick={handleAccount}>
103111
<ListItemIcon>
104-
<Logout fontSize='small' />
112+
<LogoutIcon fontSize='small' />
105113
</ListItemIcon>
106114
Logout
107115
</MenuItem>

src/pages/Login/Login.jsx

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,70 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
2+
import axios from 'axios';
3+
import { IconButton, InputAdornment, TextField } from '@mui/material';
4+
import { Visibility, VisibilityOff } from '@mui/icons-material';
25

36
function Login() {
4-
return <div>Login</div>;
7+
const [usernameOrEmail, setUsernameOrEmail] = useState('');
8+
const [password, setPassword] = useState('');
9+
const [showPassword, setShowPassword] = useState(false);
10+
const [rememberMe, setRememberMe] = useState(false);
11+
12+
const handleLogin = async () => {
13+
try {
14+
const response = await axios.post('/api/login', {
15+
usernameOrEmail,
16+
password,
17+
rememberMe,
18+
});
19+
if (response.status === 200) {
20+
window.location.href = '/'; // Redirect to the desired page
21+
}
22+
} catch (error) {
23+
console.error(error);
24+
}
25+
};
26+
27+
return (
28+
<div>
29+
<TextField
30+
label='Username or Email'
31+
value={usernameOrEmail}
32+
onChange={(e) => setUsernameOrEmail(e.target.value)}
33+
/>
34+
<TextField
35+
label='Password'
36+
type={showPassword ? 'text' : 'password'}
37+
value={password}
38+
onChange={(e) => setPassword(e.target.value)}
39+
InputProps={{
40+
endAdornment: (
41+
<InputAdornment position='end'>
42+
<IconButton
43+
onClick={() => setShowPassword(!showPassword)}
44+
onMouseDown={(e) => e.preventDefault()}
45+
>
46+
{showPassword ? (
47+
<VisibilityOff />
48+
) : (
49+
<Visibility />
50+
)}
51+
</IconButton>
52+
</InputAdornment>
53+
),
54+
}}
55+
/>
56+
<label>
57+
<input
58+
type='checkbox'
59+
checked={rememberMe}
60+
onChange={(e) => setRememberMe(e.target.checked)}
61+
/>
62+
Remember Me
63+
</label>
64+
<a href='/forgot-password'>Forgot Password</a>
65+
<button onClick={handleLogin}>Log In</button>
66+
</div>
67+
);
568
}
669

770
export default Login;

0 commit comments

Comments
 (0)