import React, { useState, useEffect, useContext } from 'react' import axios from 'axios' import { Grid, CircularProgress, Card, CardHeader, Divider, CardContent, TextField, CardActions, Button, FormGroup, FormControlLabel, Checkbox } from '@mui/material' import { toast } from 'react-toastify' import { AppContext, ModeType } from '../../context/appContext' import UpdatePasswordModal from '../../components/passwordModal' const Profile = () => { const [isLoading, setIsLoading] = useState(false) const appContext = useContext(AppContext) const [user, setUser] = useState({} as any) const [isPasswordModalOpen, setIsPasswordModalOpen] = useState(false) useEffect(() => { setIsLoading(true) axios .get(`/SASjsApi/user/${appContext.userId}`) .then((res: any) => { setUser(res.data) }) .catch((err) => { console.log(err) }) .finally(() => { setIsLoading(false) }) }, [appContext.userId]) const handleChange = (event: any) => { const { name, value } = event.target setUser({ ...user, [name]: value }) } const handleSubmit = () => { setIsLoading(true) axios .patch(`/SASjsApi/user/${appContext.userId}`, { username: user.username, displayName: user.displayName, autoExec: user.autoExec }) .then((res: any) => { toast.success('User information updated', { theme: 'dark', position: toast.POSITION.BOTTOM_RIGHT }) }) .catch((err) => { toast.error('Failed: ' + err.response?.data || err.text, { theme: 'dark', position: toast.POSITION.BOTTOM_RIGHT }) }) .finally(() => { setIsLoading(false) }) } const updatePassword = (currentPassword: string, newPassword: string) => { setIsLoading(true) setIsPasswordModalOpen(false) axios .patch(`/SASjsApi/auth/updatePassword`, { currentPassword, newPassword }) .then((res: any) => { toast.success('Password updated', { theme: 'dark', position: toast.POSITION.BOTTOM_RIGHT }) }) .catch((err) => { toast.error('Failed: ' + err.response?.data || err.text, { theme: 'dark', position: toast.POSITION.BOTTOM_RIGHT }) }) .finally(() => { setIsLoading(false) }) } return isLoading ? ( ) : ( <> } label="isActive" /> } label="isAdmin" /> ) } export default Profile