import React, { useRef } from "react"; import { AsyncWidget } from "../widgets/AsyncWidget"; import { ReplacePasswordResponse, User, UserApi } from "../api/UserApi"; import { Alert, Box, Button, Card, CardActions, CardContent, Checkbox, CircularProgress, FormControlLabel, TextField, Typography, } from "@mui/material"; import { TimeWidget, formatDate } from "../widgets/TimeWidget"; import { ServerApi } from "../api/ServerApi"; import { PasswordInput } from "../widgets/PasswordInput"; import { normalize } from "path/win32"; export function ProfileRoute(): React.ReactElement { const [user, setUser] = React.useState(null); const load = async () => { const u = await UserApi.GetUserInfo(); setUser(u); }; const counter = useRef(0); return ( (
Profil (counter.current += 1)} /> {user?.has_password && }
)} /> ); } function ProfileSettingsCard(p: { user: User; onUpdate: () => {} }) { const [newName, setNewName] = React.useState(p.user.name); const [error, setError] = React.useState(null); const [success, setSuccess] = React.useState(null); const updateProfile = async () => { try { setSuccess(null); setError(null); await UserApi.UpdateProfile(newName); p.onUpdate(); setSuccess("Informations du profil enregistrées avec succès !"); } catch (e) { console.error(e); setError("Echec de la mise à jour du profil !"); } }; return ( <> {error && {error}} {success && {success}} Paramètres du compte setNewName(e.target.value)} inputProps={{ maxLength: ServerApi.Config.constraints.user_name_len.max, }} /> } label="Compte administrateur" /> ); } function ChangePasswordCard(): React.ReactElement { const [loading, setLoading] = React.useState(false); const [error, setError] = React.useState(null); const [success, setSuccess] = React.useState(null); const [oldPassword, setOldpassword] = React.useState(""); const [newPassword, setNewpassword] = React.useState(""); const [confirmNewPassword, setConfirmNewpassword] = React.useState(""); const isValid = ServerApi.CheckPassword(newPassword) === null && oldPassword.length > 0 && confirmNewPassword === newPassword; const updatePassword = async (e: React.FormEvent) => { e.preventDefault(); if (!isValid || loading) return; setLoading(true); setSuccess(null); setError(null); try { const result = await UserApi.ReplacePassword(oldPassword, newPassword); switch (result) { case ReplacePasswordResponse.Error: setError("Echec du changement de mot de passe !"); break; case ReplacePasswordResponse.Success: setSuccess("Mot de passe changé avec succès !"); break; case ReplacePasswordResponse.InvalidOldPassword: setError("Ancien mot de passe saisi invalide !"); break; case ReplacePasswordResponse.InvalidNewPassword: setError("Nouveau mot de passe saisi invalide !"); break; case ReplacePasswordResponse.TooManyRequests: setError( "Trop de tentatives de changement de mot de passe, veuillez réessayer ultérieurement !" ); break; } } catch (e) { console.error(e); setError("Echec de la mise à jour du mot de passe !"); } setLoading(false); }; return ( <> {error && {error}} {success && {success}} Changement du mot de passe setOldpassword(e.target.value)} /> setNewpassword(n)} label={"Nouveau mot de passe"} /> setConfirmNewpassword(e.target.value)} /> {" "} ); }