import { useRouter } from "next/router"; import { useEffect, useRef, useState } from "react"; import Input from "../components/Input.jsx"; import Layout from "../components/Layout.js"; import Section from "../components/user/section.jsx"; import { useSessionContext } from "../context/session.context.js"; import { changePassword, changeUser, getUser, logout, logoutRequest, } from "../requests/requests.users.js"; import styles from "../styles/dashboard.module.scss"; import { FaUserAlt } from "react-icons/fa"; import { RiGroupFill, RiExternalLinkFill, RiLockPasswordFill, } from "react-icons/ri"; import Link from "next/link"; import { isBrowser, isEmpty } from "../utils/utils.js"; import { notificationService } from "../services/notification.service.js"; import Input2 from "../components/input2.jsx"; export default function Dashboard() { const router = useRouter(); const [user, setUser] = useState(); const { authData } = useSessionContext(); /* useEffect(() => { if (isBrowser && localStorage.getItem('token') === null) { router.push({ pathname: "/login" }, undefined, { shallow: true }); } else { getUser().then((res) => { setUser({ ...res }); }).catch((err) => { localStorage.clear() router.push('/login') }); } return () => { setUser(); }; }, []); */ const isLoad = useRef(false); useEffect(() => { console.log('AURHDATA', authData) if (localStorage.getItem("token") == null || !authData|| isEmpty(authData)) { router.push({ pathname: "/login" }, undefined, { shallow: true }); } else/* if (authData && !isEmpty(authData)) */ { setUser(authData); } isLoad.current = true; }, [authData]); const [newMdp, setNewMdp] = useState({ password1: "", password2: "", }); const [mdpErrors, setMdpError] = useState({}); const [profilErrors, setProfilErrors] = useState({}); return (

Mon compte

{user && (
} validate={() => { changeUser({ first_name: user.first_name, username: user.username, last_name: user.last_name, email: user.email, }) .then((res) => { setUser(res); setProfilErrors({}); notificationService.success( "Profil", "Données personnelles changées avec succès !" ); }) .catch((err) => { notificationService.error( "Profil", "Échec lors de la mise à jour des données personnelles !" ); setProfilErrors(err.response.data.errors); }); }} validateMsg={"Modifier mon profil"} >
{ setUser({ ...user, username: e.target.value }); }} value={user && user.username} placeholder="Username..." id="username" /> {profilErrors.username && profilErrors.username.map((e) => { return

{e}

; })}
{ setUser({ ...user, email: e.target.value }); }} value={user && user.email} placeholder="E-mail..." id="email" /> {profilErrors.email && profilErrors.email.map((e) => { return

{e}

; })}
{ setUser({ ...user, first_name: e.target.value }); }} value={user && user.first_name} placeholder="Prénom..." id="first_name" /> {profilErrors.first_name && profilErrors.first_name.map((e) => { return

{e}

; })}
{ setUser({ ...user, last_name: e.target.value }); }} value={user && user.last_name} placeholder="Nom..." id="last_name" /> {profilErrors.last_name && profilErrors.last_name.map((e) => { return

{e}

; })}
}>
{user && user.room .sort((a, b) => { return b.status == "membre" ? -1 : 1; }) .map((r) => { return ( {r.name} {r.status && `(${r.status})`} ); })}
{user && user.room.length === 0 && (

Vous n'avez rejoint aucune salle !

)}
} validate={() => { changePassword({ new_password1: newMdp.password1, new_password2: newMdp.password2, }) .then((res) => { notificationService.success( "Mot de passe", "Mot de passe changé avec succès !" ); setMdpError({}); setNewMdp({ password1: "", password2: "" }); }) .catch((err) => { notificationService.error( "Mot de passe", "Erreur lors de la modification du mot de passe ! " ); setMdpError({ ...err.response.data }); }); }} validateMsg={"Modifier mon mot de passe"} >
{ setNewMdp({ ...newMdp, password1: e.target.value }); }} /> {mdpErrors.new_password1 && mdpErrors.new_password1.map((e) => { return

{e}

; })}
{ setNewMdp({ ...newMdp, password2: e.target.value }); }} /> {mdpErrors.new_password2 && mdpErrors.new_password2.map((e) => { return

{e}

; })}
)}
); }