Skip to content

Commit f90b4ae

Browse files
committed
- modal window into a separate component
-profiles
1 parent 403229e commit f90b4ae

16 files changed

+409
-125
lines changed

.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
REACT_APP_API_URL_LOCAL=http://localhost:8000
2-
REACT_APP_JWT_SECRET=cvhg^&T*Rd6878rVCGFHGHJVfytchj
2+
REACT_APP_JWT_SECRET=asd

package-lock.json

+84-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
"react": "17.0.2",
1616
"react-colorful": "^5.5.1",
1717
"react-dom": "17.0.2",
18-
"react-redux": "^7.2.6",
18+
"react-redux": "^7.2.9",
19+
"react-router-dom": "^6.23.1",
1920
"react-scripts": "^5.0.0",
20-
"redux": "^4.1.2",
21+
"redux": "^4.2.1",
2122
"redux-devtools-extension": "^2.13.8",
22-
"redux-thunk": "^2.4.1",
23+
"redux-thunk": "^2.4.2",
2324
"uuid": "^8.3.2"
2425
},
2526
"devDependencies": {
26-
"axios": "1.6.0",
27+
"axios": "^1.6.0",
2728
"prettier": "2.5.1"
2829
},
2930
"scripts": {

src/App.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1+
import React, { useEffect } from 'react';
12
import './styles.css';
23
import Field from './components/Field/Field.js';
3-
import { connect } from 'react-redux';
4+
import { connect, useDispatch } from 'react-redux';
45
import { Header } from './components/Header';
6+
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
7+
import Register from './components/Auth/Register';
8+
import Login from './components/Auth/Login';
9+
import Profile from './components/Profile/Profile';
10+
import { refreshToken } from './components/redux/actions';
511

612
const App = (props) => {
13+
const dispatch = useDispatch();
14+
15+
useEffect(() => {
16+
const interval = setInterval(() => {
17+
dispatch(refreshToken());
18+
}, 30 * 60 * 1000); // Каждые 30 минут
19+
20+
return () => clearInterval(interval);
21+
}, [dispatch]);
22+
723
return (
8-
<div className="App">
9-
<Header username={props.username} />
10-
<Field />
11-
</div>
24+
<Router>
25+
<div className="App">
26+
<Header username={props.username} />
27+
<Routes>
28+
<Route path="/" element={<Field />} />
29+
<Route path="/register" element={<Register />} />
30+
<Route path="/login" element={<Login />} />
31+
<Route path="/profile" element={<Profile />} /> {/* Добавьте маршрут профиля */}
32+
</Routes>
33+
</div>
34+
</Router>
1235
);
1336
};
1437

src/components/Auth/Login.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import React, { useState } from 'react';
2-
import axios from 'axios';
3-
import CryptoJS from 'crypto-js';
2+
import { useDispatch, useSelector } from 'react-redux';
3+
import { loginUser } from '../redux/actions';
4+
import { useNavigate } from 'react-router-dom';
45

56
const Login = () => {
67
const [username, setUsername] = useState('');
78
const [password, setPassword] = useState('');
89
const [message, setMessage] = useState('');
10+
const dispatch = useDispatch();
11+
const navigate = useNavigate();
12+
const authError = useSelector((state) => state.authError);
913

10-
const handleSubmit = async (e) => {
14+
const handleSubmit = (e) => {
1115
e.preventDefault();
12-
try {
13-
const encryptedPassword = CryptoJS.AES.encrypt(password, process.env.REACT_APP_JWT_SECRET).toString();
14-
const response = await axios.post(`${process.env.REACT_APP_API_URL_LOCAL}/users/login`, { username, password: encryptedPassword });
15-
localStorage.setItem('token', response.data.token);
16-
setMessage('Login successful');
17-
} catch (error) {
18-
setMessage(error.response ? error.response.data.error : error.message);
19-
}
16+
const userData = { username, password };
17+
dispatch(loginUser(userData, () => navigate('/profile')));
2018
};
2119

2220
return (
@@ -42,7 +40,8 @@ const Login = () => {
4240
<label className="form-label">Password</label>
4341
</div>
4442
<button type="submit" className="submit-button">Login</button>
45-
{message && <p>{message}</p>}
43+
{message && <p className="error-message">{message}</p>}
44+
{authError && <p className="error-message">{authError}</p>}
4645
</form>
4746
);
4847
};

src/components/Auth/LoginModal.js

+5-23
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
1-
import React, { useEffect } from 'react';
1+
import React from 'react';
2+
import Modal from '../Modal/Modal';
23
import FlipCard from './FlipCard';
34

45
const LoginModal = ({ isOpen, onClose }) => {
5-
const handleOutsideClick = (e) => {
6-
if (e.target.className === 'modal-overlay') {
7-
onClose();
8-
}
9-
};
10-
11-
useEffect(() => {
12-
if (isOpen) {
13-
document.addEventListener('mousedown', handleOutsideClick);
14-
} else {
15-
document.removeEventListener('mousedown', handleOutsideClick);
16-
}
17-
return () => {
18-
document.removeEventListener('mousedown', handleOutsideClick);
19-
};
20-
}, [isOpen]);
21-
22-
if (!isOpen) return null;
23-
246
return (
25-
<div className="modal-overlay">
26-
<FlipCard onClose={onClose} />
27-
</div>
7+
<Modal isOpen={isOpen} onClose={onClose}>
8+
<FlipCard onClose={onClose} />
9+
</Modal>
2810
);
2911
};
3012

0 commit comments

Comments
 (0)