import { Button, Flex, Stack, Text, CloseButton, Dialog, Portal, HStack, Box, Avatar, Card, Grid, } from "@chakra-ui/react"; import { PasswordInput } from "@/components/ui/password-input"; import { RotateCcwKey } from "lucide-react"; import MyAlert from "./myChakra/MyAlert"; import { API_BASE } from "@/config/api.config"; import { useUserContext } from "@/states/Context"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import Cookies from "js-cookie"; type UserDialogueProps = { setUserDialog: (value: boolean) => void; fullname: string; randomColor: string[]; }; export const UserDialogue = (props: UserDialogueProps) => { const userData = useUserContext(); const { t } = useTranslation(); // Error handling states const [isMsg, setIsMsg] = useState(false); const [msgStatus, setMsgStatus] = useState<"error" | "success">("error"); const [msgTitle, setMsgTitle] = useState(""); const [msgDescription, setMsgDescription] = useState(""); const [oldPassword, setOldPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); // Dialog control const [isPwOpen, setPwOpen] = useState(false); const changePassword = async () => { if (newPassword !== confirmPassword) { setMsgTitle(t("err_pw_change")); setMsgDescription(t("pw_mismatch")); setMsgStatus("error"); setIsMsg(true); return; } const response = await fetch(`${API_BASE}/api/users/change-password`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${Cookies.get("token")}`, }, body: JSON.stringify({ oldPassword, newPassword }), }); if (!response.ok) { setMsgTitle(t("err_pw_change")); setMsgDescription(t("pw_mismatch")); setMsgStatus("error"); setIsMsg(true); return; } setMsgTitle(t("pw_success")); setMsgDescription(t("pw_success_desc")); setMsgStatus("success"); setIsMsg(true); setOldPassword(""); setNewPassword(""); setConfirmPassword(""); }; return ( {t("user-info-desc")} {t("first-name")}: {userData.first_name} {t("last-name")}: {userData.last_name} {t("username")}: {userData.username} {t("role")}: {userData.role} {t("admin-status")}: {userData.is_admin ? t("yes") : t("no")} {/* Passwort-Dialog (kontrolliert) */} setPwOpen(e.open)}> {t("change-password")}
{ e.preventDefault(); changePassword(); }} > setOldPassword(e.target.value)} placeholder={t("old-password")} /> setNewPassword(e.target.value)} placeholder={t("new-password")} /> setConfirmPassword(e.target.value)} placeholder={t("confirm-password")} /> {isMsg && ( )}
); };