Skip to content

Commit

Permalink
Merge branch 'detailpage'
Browse files Browse the repository at this point in the history
  • Loading branch information
radiance0718 committed Jun 3, 2024
2 parents 39a6f92 + 9d754e8 commit 1ad9ba4
Show file tree
Hide file tree
Showing 6 changed files with 367 additions and 271 deletions.
259 changes: 56 additions & 203 deletions client/src/Notification/Notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import {
Typography,
Card,
CardContent,
stepButtonClasses,
} from '@mui/material';
import DefaultNavbar from './NotificationpageComponents/DefaultNavbar';
import theme from '../assets/theme';
import routes from '../publicAssets/routes';
import {useNavigate} from 'react-router-dom';
import axios from 'axios';

const TagEnum = {
1: 'normal',
Expand All @@ -23,11 +25,30 @@ const TagEnum = {
};

const NotificationBriefBlock = ({Notification}) => {
const authToken = localStorage.getItem('authToken');
const navigate = useNavigate();
const {id, activateid, senderid, reciverid, title, content, tag, time, read} =
Notification;
const handelClickOnNotification = () => {
console.log('Notification id:', id);
const handelClickOnNotification = async () => {
try {
await axios.put(
`http://10.27.41.93:5000/api/messages/${id}`,
{
read: true,
},
{
headers: {
Authorization: `Bearer ${authToken}`,
Accept: 'application/json',
},
},
{
withCredentials: true,
},
);
} catch (error) {
console.error(error);
}
navigate(`/details/${id}`);
};

Expand Down Expand Up @@ -73,226 +94,58 @@ const NotificationBriefBlock = ({Notification}) => {
};

const Notifications = () => {
const authToken = localStorage.getItem('authToken');
const [notifications, setNotifications] = useState([]);
const [userid, setUserid] = useState('');
const [lastPage, setLastPage] = useState(-1);
const [userName, setUserName] = useState('');
const [currentPage, setCurrentPage] = useState(1);
const [loading, setLoading] = useState(false);

const getUserid = async () => {
// const response = await fetch('/api/userid');
// const data = await response.json();
// setUserid(data.userid);

// test data
fetch('http://10.27.41.93:5000/api/me')
.then((response) => response.json())
.then((data) => {
const userId = data.id; // 获取用户ID
setUserid(userId);
// 然后您可以使用这个用户ID进行后续操作
})
.catch((error) => console.error('请求错误:', error));
setUserid('88');
};

const getNotifications = async (userid, page) => {
const getNotifications = async (page) => {
setLoading(true);
// const response = await fetch(`/api/Notifications/${userid}?page=${page}&limit=15`);
// const data = await response.json();
try {
const response = await axios.get(
`http://10.27.41.93:5000/api/messages?limit=${15 * page}&offset=${0}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
Accept: 'application/json',
},
},
{
withCredentials: true,
},
);

// For now, using test data

const testData = [
{
id: '1',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'New activate relased',
content: 'hello',
tag: '0',
time: '2021-10-10 10:10:10',
read: false,
},
{
id: '2',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'Activate detail changed',
content: 'world!!',
tag: '1',
time: '2021-10-11 10:10:10',
read: false,
},
{
id: '3',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'DDL!!!!!',
content: 'hello',
tag: '2',
time: '2021-10-13 10:10:10',
read: false,
},
{
id: '4',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'New activate relased',
content: 'hello',
tag: '0',
time: '2021-10-10 10:10:10',
read: false,
},
{
id: '5',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'Activate detail changed',
content: 'world!!',
tag: '1',
time: '2021-12-11 10:10:10',
read: false,
},
{
id: '6',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'DDL!!!!!',
content: 'hello',
tag: '2',
time: '2021-10-13 10:10:10',
read: false,
},
{
id: '7',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'New activate relased',
content: 'hello',
tag: '0',
time: '2021-10-10 10:10:10',
read: false,
},
{
id: '8',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'Activate detail changed',
content: 'world!!',
tag: '1',
time: '2021-10-12 10:10:10',
read: false,
},
{
id: '9',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'DDL!!!!!',
content: 'hello',
tag: '2',
time: '2021-10-13 10:10:10',
read: false,
},
{
id: '10',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'New activate relased',
content: 'hello',
tag: '0',
time: '2021-10-10 10:10:10',
read: false,
},
{
id: '11',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'Activate detail changed',
content: 'world!!',
tag: '1',
time: '2021-10-11 10:10:10',
read: false,
},
{
id: '12',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'DDL!!!!!',
content: 'hello',
tag: '2',
time: '2021-10-13 10:10:10',
read: false,
},
{
id: '13',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'New activate relased',
content: 'hello',
tag: '0',
time: '2021-10-13 10:10:10',
read: false,
},
{
id: '14',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'Activate detail changed',
content: 'world!!',
tag: '1',
time: '2021-10-11 10:10:10',
read: false,
},
{
id: '15',
activateid: '1',
senderid: '99',
reciverid: '88',
title: 'DDL!!!!!',
content: 'hello',
tag: '2',
time: '2021-10-13 10:10:10',
read: false,
},
];
setNotifications((prevNotifications) => [
...prevNotifications,
...testData,
]);
setLoading(false);
setNotifications(response.data);
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
};

useEffect(() => {
getUserid().then(() => {
setCurrentPage(1);
if (currentPage === 1) {
setNotifications([]);
getNotifications(userid, currentPage);
});
}, [userid]);
}
getNotifications(currentPage);
}, [currentPage]);

const handleLoadMore = () => {
setCurrentPage((prevPage) => prevPage + 1);
getNotifications(userid, currentPage + 1);
};

return (
<ThemeProvider theme={theme}>
<CssBaseline />
<DefaultNavbar routes={routes} sticky />
<Box
sx={{
width: 1112,
height: 10,
}}
/>
<Container>
<main
style={{
Expand Down
56 changes: 45 additions & 11 deletions client/src/PasswordRecovery/PasswordRecovery.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {useState} from 'react';
import {Box, Button, TextField, Typography, Alert} from '@mui/material';
import axios from 'axios';

const PasswordRecovery = () => {
const [email, setEmail] = useState('');
Expand All @@ -8,33 +9,65 @@ const PasswordRecovery = () => {
const [confirmPassword, setConfirmPassword] = useState('');
const [step, setStep] = useState(1);
const [error, setError] = useState('');
const [successMessage, setSuccessMessage] = useState('');

const handleEmailSubmit = async () => {
// 模拟后端响应
const registeredEmails = ['[email protected]'];
if (!registeredEmails.includes(email)) {
setError('Email is not registered');
return;
try {
const response = await axios.post(
'http://10.27.41.93:5000/api/requestReset',
{
email: email,
},
);
console.log(response.data);
} catch (error) {
if (error.response && error.response.status === 400) {
// 处理 400 错误(电子邮件未找到)
setError('Email not found. Please enter a registered email address.');
console.error('Error 400: Email not found');
return;
} else {
// 处理其他错误
setError('An unexpected error occurred. Please try again later.');
console.error(error);
}
}

setError('');
setStep(2);
// 假设发送邮件
console.log(`Verification code sent to ${email}`);
};

const handlePasswordReset = async () => {
// 模拟后端响应
const correctCode = '123456';
if (code !== correctCode) {
setError('Incorrect verification code');
return;
}
if (newPassword !== confirmPassword) {
setError('Passwords do not match');
return;
}
try {
const response = await axios.post(
'http://10.27.41.93:5000/api/resetPassword',
{
email: email,
token: code,
password: newPassword,
},
);
console.log(response.data);
} catch (error) {
if (error.response && error.response.status === 400) {
setError('Invalid token or token expired.');
console.error('Error 400: Invalid token or token expired');
return;
} else {
setError('An unexpected error occurred. Please try again later.');
console.error(error);
}
}

setError('');
console.log('Password has been reset successfully');
setSuccessMessage('Password has been reset successfully');
};

return (
Expand Down Expand Up @@ -120,6 +153,7 @@ const PasswordRecovery = () => {
)}
</Box>
)}
{successMessage && <div style={{color: 'green'}}>{successMessage}</div>}
</Box>
);
};
Expand Down
Loading

0 comments on commit 1ad9ba4

Please sign in to comment.