import { useState } from "react"; import toast from "react-hot-toast"; import { Card, CardContent, Avatar, Typography, Chip, IconButton, Tooltip, Stack, Box, } from "@mui/material"; import { Edit, Delete, VpnKey, InfoOutlined, AdminPanelSettings, ContentCopy, } from "@mui/icons-material"; import EditUser from "./EditUser/EditUser"; import { IUser } from "../Pages/Admin/Users/interfaces"; import DeleteUser from "./DeleteUser/DeleteUser"; import Modal from "@/app/components/Modal/Modal"; import { useDispatch } from "react-redux"; import { AppDispatch } from "@/app/redux/store"; import { resetPassword } from "@/app/redux/auth/authSlice"; import Confirm from "../../components/Confirm/Confirm"; import { ThunkSuccess } from "@/app/redux/types"; import "./User.scss"; interface Props { user: IUser; } export default function UserRoleCard({ user }: Props) { const [isEditing, setIsEditing] = useState(false); const [showConfirmModal, setShowConfirmModal] = useState(false); const [openDeleteUser, setOpenDeleteUser] = useState(false); const dispatch = useDispatch(); const { username, first_name, last_name, email, groups } = user; const [newPassword, setNewPassword] = useState(null); console.log("[UserRoleCard] - user", user); const handleEditClick = () => { setIsEditing(!isEditing); }; const handleDeleteClick = () => { setOpenDeleteUser(true); }; const handleResetPasswordSubmit = async () => { try { const resultAction = await dispatch( resetPassword({ id: user.id as string }) ); if (resetPassword.fulfilled.match(resultAction)) { setNewPassword( ( resultAction.payload as ThunkSuccess<{ success: boolean; message: string; newPassword: string | null; }> )?.newPassword || null ); toast.success( ( resultAction.payload as ThunkSuccess<{ success: boolean; message: string; newPassword: string | null; }> )?.message || "Password reset successfully" ); } else if (resetPassword.rejected.match(resultAction)) { toast.error( (resultAction.payload as string) || "Failed to reset password" ); } } catch (e: unknown) { const message = e instanceof Error ? e.message : "Unexpected error"; toast.error(message); } }; return ( {/* Header */} {username?.slice(0, 2).toUpperCase()} {username} {first_name} {last_name} {email} {true && ( } label="Admin" size="small" /> )} { setNewPassword(null); setShowConfirmModal(true); }} > {/* Merchants + Roles */} Merchants Roles {groups?.map(role => ( ))}
{isEditing && }
{openDeleteUser && user && ( setOpenDeleteUser(false)} /> )} {showConfirmModal && ( setShowConfirmModal(false)} title={`Reset Password - ${user.first_name}`} > {newPassword && (
{newPassword} { try { await navigator.clipboard.writeText(newPassword); toast.success("Copied to clipboard"); } catch { toast.error("Failed to copy"); } }} size="small" >
)} {!newPassword && ( setShowConfirmModal(false)} onSubmit={handleResetPasswordSubmit} message="Are you sure you want to reset the password for this user?" confirmLabel="Reset Password" cancelLabel="Cancel" /> )}
)}
); }