mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-09-11 22:06:29 +02:00
create a logout component
This commit is contained in:
31
src/components/LogoutButton/LogoutButton.tsx
Normal file
31
src/components/LogoutButton/LogoutButton.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
import React, { FC, memo, useCallback } from "react"
|
||||
import { useDispatch, useSelector } from "react-redux"
|
||||
import { unsetJWT } from "../../services/auth"
|
||||
import { isUserConnected, logoutUser } from "../../store/auth"
|
||||
import styles from "./styles.module.scss"
|
||||
|
||||
const LogoutButton: FC = (): JSX.Element | null => {
|
||||
const dispatch = useDispatch()
|
||||
const connected = useSelector(isUserConnected)
|
||||
|
||||
const onClick = useCallback(
|
||||
(event: React.SyntheticEvent): void => {
|
||||
event.preventDefault()
|
||||
unsetJWT()
|
||||
dispatch(logoutUser())
|
||||
},
|
||||
[dispatch]
|
||||
)
|
||||
|
||||
if (!connected) return null
|
||||
|
||||
return (
|
||||
<div key="logout" className={styles.logoutButton}>
|
||||
<button type="button" onClick={onClick}>
|
||||
Se déconnecter
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default memo(LogoutButton)
|
8
src/components/LogoutButton/styles.module.scss
Normal file
8
src/components/LogoutButton/styles.module.scss
Normal file
@@ -0,0 +1,8 @@
|
||||
.logoutButton {
|
||||
text-align: center;
|
||||
|
||||
button {
|
||||
font-weight: normal;
|
||||
font-size: 0.9em;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user