import * as React from "react"; import { Button, styled, Dialog, DialogTitle, DialogContent, DialogActions, IconButton, Avatar, Box, Divider, } from "@mui/material"; import CloseIcon from "@mui/icons-material/Close"; import EditSquareIcon from "@mui/icons-material/EditSquare"; import "./changeAvatarDialog.css"; import ButtonRotkehlchen from "./ButtonRotkehlchen"; import Username from "./Username"; import "./username.css"; import api from "../api/axios"; import { ChangeEvent, useState } from "react"; import { UserProfile } from "../types/UserProfile"; const BootstrapDialog = styled(Dialog)(({ theme }) => ({ "& .MuiDialogContent-root": { padding: theme.spacing(2), }, "& .MuiDialogActions-root": { padding: theme.spacing(1), }, })); export default function AvatarDialog({ ownAccount, username, setUserData, imageUrl, }: { ownAccount: boolean; username: string; setUserData: React.Dispatch>; imageUrl?: string | null; }) { const [file, setFile] = useState(); const handleFileChange = (e: ChangeEvent) => { if (e.target.files) { setFile(e.target.files[0]); } }; const saveProfilePicture = async () => { try { const formData = new FormData(); console.log("Saving profile picture:", file); if (file) { formData.append("image", file); const response = await api.post( "/profile/uploadProfilePicture", formData ); console.log("Profile picture saved:", response.data); setUserData((prevData) => (prevData ?{ ...prevData, profilePictureUrl: response.data.url } : null)); } setOpen(false); // Close the dialog after saving } catch (error) { console.error("Error saving profile picture:", error); } }; const [open, setOpen] = React.useState(false); const handleClickOpen = () => { setOpen(true); }; const handleClose = () => { setOpen(false); }; return (
{ownAccount ? "Change Profile Picture" : username} ({ position: "absolute", right: 8, top: 8, color: theme.palette.grey[500], })} > {file {ownAccount && (
)}
{ownAccount && (
)}
); }