Skip to content

Commit

Permalink
Fix loading reset password and message unread
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenny DUONG committed Jan 29, 2024
1 parent ef90c65 commit e05f84c
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 86 deletions.
6 changes: 4 additions & 2 deletions front/src/Components/ChatComponent/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ const ChatComponent = ({
}, [user])

useEffect(() => {
if (!socketMessage) return
if (!socketMessage)
return

if (socketMessage.from !== user.login) return
if (socketMessage.from !== user.login)
return

setMessages((prev) => {
if (!prev) {
Expand Down
81 changes: 39 additions & 42 deletions front/src/Components/Navbar/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ const NOTIFICATION_ROUTE = '/dashboard/notifications'
const MESSAGE_ROUTE = '/dashboard/messages'

const checkIfLoggedInRoute = (path) => {
if (LOGGED_IN_ROUTES.some((route) => path.startsWith(route))) {
if (LOGGED_IN_ROUTES.some((route) => path.startsWith(route)))
return true
}
return false
}

const checkIfOnboardedRoute = (path) => {
if (ONBOARDED_ROUTES.some((route) => path.startsWith(route))) {
if (ONBOARDED_ROUTES.some((route) => path.startsWith(route)))
return true
}
return false
}

Expand All @@ -61,19 +59,18 @@ const Navbar = () => {
const onNotif = currentLocation.pathname.startsWith(NOTIFICATION_ROUTE)
const onMessage = currentLocation.pathname.startsWith(MESSAGE_ROUTE)

if (
notif.type === 'like' ||
notif.type === 'unlike' ||
notif.type === 'visit' ||
notif.type === 'interested'
) {
if (!onNotif) setNewNotification((prev) => prev + 1)
if (notif.type === 'like' || notif.type === 'unlike' || notif.type === 'visit' || notif.type === 'interested') {
if (!onNotif)
setNewNotification((prev) => prev + 1)
} else if (notif.type === 'message' && !onMessage) {
setNewMessage((prev) => prev + 1)
} else if (notif.type === 'match') {
if (!onNotif) setNewMessage((prev) => prev + 1)
if (!onMessage) setNewNotification((prev) => prev + 1)
if (!onNotif)
setNewMessage((prev) => prev + 1)
if (!onMessage)
setNewNotification((prev) => prev + 1)
}

}

useEffect(() => {
Expand All @@ -82,7 +79,8 @@ const Navbar = () => {
}, [location])

useEffect(() => {
if (!onboarded) return
if (!onboarded)
return

const userInfos = sessionStorage.getItem('user_infos')
if (userInfos) {
Expand All @@ -106,9 +104,7 @@ const Navbar = () => {
const toggleDropdown = () => setIsDropdownOpen(!isDropdownOpen)

const handleLogout = () => {
dispatch({
type: USER_STATE_ACTIONS.LOG_OUT,
})
dispatch({type: USER_STATE_ACTIONS.LOG_OUT})
ApiService.get('/user/logout').then(() => {
navigate('/login')
})
Expand All @@ -117,12 +113,8 @@ const Navbar = () => {
// Effet pour gérer le clic à l'extérieur du menu déroulant
useEffect(() => {
const handleOutsideClick = (event) => {
if (
dropdownRef.current &&
!dropdownRef.current.contains(event.target)
) {
if ( dropdownRef.current && !dropdownRef.current.contains(event.target))
setIsDropdownOpen(false) // Ferme le menu si un clic en dehors est détecté
}
}
document.addEventListener('mousedown', handleOutsideClick)
return () => {
Expand All @@ -131,25 +123,35 @@ const Navbar = () => {
}, [])

useEffect(() => {
if (!onboarded) return
if (!onboarded)
return

ApiService.get('/notifications/count').then((data) => {
const messageCount = data.filter(
(notif) => notif.type === 'message' || notif.type === 'match'
).length
const notifCount = data.filter(
(notif) =>
notif.type === 'like' ||
notif.type === 'unlike' ||
notif.type === 'match' ||
notif.type === 'visit'
const messageCount = data.filter((notif) => notif.type === 'message' || notif.type === 'match').length
const notifCount = data.filter( (notif) =>
notif.type === 'like' ||
notif.type === 'unlike' ||
notif.type === 'match' ||
notif.type === 'visit'
).length

const onNotif = location.pathname.startsWith(NOTIFICATION_ROUTE)
if (!onNotif) setNewNotification(notifCount)
if (!onNotif)
setNewNotification(notifCount)

const onMessage = location.pathname.startsWith(MESSAGE_ROUTE)
if (!onMessage) setNewMessage(messageCount)
if (!onMessage)
setNewMessage(messageCount)
})
}, [onboarded])

const handleClickMessage = () => {
setNewMessage(0)
ApiService.put("/notifications/read/").catch(() => {
console.log("error")
})
}

return (
<div id="navbar">
<Link to={loggedIn ? '/dashboard' : '/'}>
Expand All @@ -169,21 +171,16 @@ const Navbar = () => {
)}
{loggedIn && onboarded && (
<div className="navbar-menu">
<Link
to="/dashboard/notifications"
onClick={() => setNewNotification(0)}
>
<Link to="/dashboard/notifications" onClick={() => setNewNotification(0)}>
<Badge badgeContent={newNotification} color="error">
<Notifications
fontSize="large"
sx={{ color: 'white' }}
></Notifications>
</Badge>
</Link>
<Link
to="/dashboard/messages"
onClick={() => setNewMessage(0)}
>

<Link to="/dashboard/messages" onClick={handleClickMessage} >
<Badge badgeContent={newMessage} color="error">
<Chat
fontSize="large"
Expand Down
19 changes: 3 additions & 16 deletions front/src/Components/Notifications/Notifications.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,10 @@ function Notifications() {
)}
{notifications &&
notifications.map((notification, index) => (
<div
key={index}
className={
'notification-list' +
(notification.read ? ' read' : '')
}
>
<div key={index} className={'notification-list' + (notification.read ? ' read' : '')} >
<div className="notification-list_content">
<div className="notification-list_img">
<img
src={ApiService.getImgPath(
notification.imgA
)}
alt={notification.name}
/>
<img src={ApiService.getImgPath(notification.imgA)} alt={notification.name} />
</div>

<div className="notification-list_detail">
Expand All @@ -88,9 +77,7 @@ function Notifications() {
</p>
<p>
<small>
{formatTimeDifference(
notification.created_at
)}
{formatTimeDifference(notification.created_at)}
</small>
</p>
</div>
Expand Down
58 changes: 38 additions & 20 deletions front/src/Pages/ResetPassword/Components/ChangePassword.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ChangePassword = ({ login, token }) => {
const [confirmPassword, setConfirmPassword] = useState('')
const [passwordError, setPasswordError] = useState('')
const [loading, setLoading] = useState(false)
const [confirmChange, setConfirmChange] = useState(false)

useEffect(() => {
const res = checkPassword(newPassword)
Expand All @@ -33,16 +34,31 @@ const ChangePassword = ({ login, token }) => {
login,
hashedPassword,
})
.then((data) => {
console.log('response: ', data)
})
.catch((error) => {
console.log(error)
})
.then((data) => {
console.log('response: ', data)
setLoading(false)
setConfirmChange(true)
})
.catch((error) => {
console.log(error)
switch (error.status) {
case 401:
setPasswordError('Invalid token.')
break
case 404:
setPasswordError('User not found.')
break
case 400:
setPasswordError('Missing field.')
break
default:
setPasswordError('Something went wrong.')
}
setLoading(false)
})
}

const passwordsMatch =
newPassword && confirmPassword && newPassword === confirmPassword
const passwordsMatch = newPassword && confirmPassword && newPassword === confirmPassword

return (
<div>
Expand All @@ -68,20 +84,22 @@ const ChangePassword = ({ login, token }) => {
type="password"
/>
<label className="password-error">
{newPassword &&
confirmPassword &&
passwordError === '' &&
!passwordsMatch &&
'Passwords do not match.'}
{ passwordError === '' && !passwordsMatch && 'Passwords do not match.'}
</label>
<label className='password-change'>
{confirmChange && 'Password changed successfully.'}
</label>
</div>
<button
type="submit"
disabled={!passwordsMatch || loading}
className="btn signup-btn reset-password-btn"
>
{loading ? <CircularProgress /> : 'Change password'}
</button>
{!confirmChange ? (
<button type="submit" disabled={!passwordsMatch || loading} className="btn signup-btn reset-password-btn">
{loading ? <CircularProgress /> : 'Change password'}
</button>
) : (
<button className="btn signup-btn reset-password-btn pink">
Go to login
</button>
)}

</form>
</div>
)
Expand Down
9 changes: 7 additions & 2 deletions front/src/Pages/ResetPassword/Components/SendMail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ const SendMail = () => {
const [emailSent, setEmailSent] = useState(false)
const [email, setEmail] = useState('')
const [emailList, setEmailList] = useState([])
const [alreadySent, setAlreadySent] = useState(false)

const handleChange = (e) => {
setEmail(e)
}

const handleSubmit = (e) => {
e.preventDefault()
setEmailSent(true)
setEmailList([...emailList, email])

ApiService.get('/user/reset-password/' + email)
.then((data) => {
console.log(data)
setEmailSent(true)
})
.catch((error) => {
console.log(error)
setAlreadySent(true)
})
}

Expand Down Expand Up @@ -51,8 +53,11 @@ const SendMail = () => {
disabled={emailSent}
>
Send an email to reset your password
</button>
</button>

<p className="send-mail-label">{emailSent && 'Email sent'}</p>
<p className="send-mail-label">{alreadySent && 'Email already sent'}</p>

</div>
)
}
Expand Down
10 changes: 10 additions & 0 deletions front/src/Pages/ResetPassword/ResetPassword.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ label.password-error {
min-height: 24px;
}

label.password-change {
color: green;
font-size: 0.8rem;
margin: 0;
width: 100%;
text-align: center;
display: block;
min-height: 24px;
}

.send-mail-label {
margin: 0;
width: 100%;
Expand Down
15 changes: 14 additions & 1 deletion server/app/handlers/notifications.handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class {
else res.json(notifications);
}

static async readNotification(req, res) {
static async readConvFromLogin(req, res) {
const login = req.decodedUser._login;
const login2 = req.params.login;
const notif = new Notifications({ trigger_login: login2 });
Expand All @@ -42,6 +42,19 @@ export default class {
res.status(200).send({ count: data?.affectedRows });
}

static async readMessages(req, res) {
const login = req.decodedUser._login;
const notif = new Notifications({ login: login });
notif.created_at = undefined;

const data = await Notifications.updateNotifications(notif, {
type: "message",
login: login,
});

res.status(200).send({ count: data?.affectedRows });
}

static async deleteNotification(req, res) {
const login = req.params.login;
const login2 = req.params.login2;
Expand Down
5 changes: 3 additions & 2 deletions server/app/handlers/user.handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,9 @@ export default class {
user.password = password;
user.token = crypto.randomBytes(64).toString("base64url");
user.token += "_mail_timestamp_" + Date.now();
await User.updateByLogin(login, user);

const userChange = await User.updateByLogin(login, user);
console.log(userChange)

res.status(200).send({
message: "SUCCESS",
});
Expand Down
Loading

0 comments on commit e05f84c

Please sign in to comment.