|
@@ -1,17 +1,64 @@
|
|
-import { Avatar, Box, Button, FormControl, IconButton, InputAdornment, OutlinedInput } from '@mui/material';
|
|
|
|
-import { Edit, Visibility, VisibilityOff } from '@mui/icons-material';
|
|
|
|
|
|
+import { Box, Button, FormControl, IconButton, InputAdornment, OutlinedInput, Snackbar } from '@mui/material';
|
|
|
|
+import { Visibility, VisibilityOff, Save } from '@mui/icons-material';
|
|
import React, { useState } from 'react';
|
|
import React, { useState } from 'react';
|
|
import Title from '../components/Title';
|
|
import Title from '../components/Title';
|
|
import { Stack } from '@mui/system';
|
|
import { Stack } from '@mui/system';
|
|
|
|
+import { useGetUserByIdQuery, useUpdateUserMutation } from '../store/api';
|
|
|
|
+import { useSelector } from 'react-redux';
|
|
|
|
+import Loader from '../components/Loader';
|
|
|
|
+import ImageUploader from '../components/ImageUploader';
|
|
|
|
+import MuiAlert from '@mui/material/Alert';
|
|
|
|
|
|
function EditProfilePage() {
|
|
function EditProfilePage() {
|
|
- const [nick, setNick] = useState('');
|
|
|
|
|
|
+ const userId = useSelector(state => state?.auth?.payload?.sub?.id);
|
|
|
|
+ const { data, isFetching } = useGetUserByIdQuery(userId);
|
|
|
|
+ const [avatarId, setAvatarId] = useState('');
|
|
|
|
+ const [nick, setNick] = useState(data?.UserFindOne?.nick || '');
|
|
const [password, setPassword] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const [confirmPassword, setConfirmPassword] = useState('');
|
|
const [confirmPassword, setConfirmPassword] = useState('');
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
|
|
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
|
|
|
|
+ const [open, setOpen] = useState(false);
|
|
|
|
+ const [snackbarMessage, setSnackbarMessage] = useState('');
|
|
|
|
+ const [snackbarType, setSnackbarType] = useState('');
|
|
const handleClickShowPassword = () => setShowPassword((show) => !show);
|
|
const handleClickShowPassword = () => setShowPassword((show) => !show);
|
|
const handleClickShowConfirmPassword = () => setShowConfirmPassword((show) => !show);
|
|
const handleClickShowConfirmPassword = () => setShowConfirmPassword((show) => !show);
|
|
|
|
+ const [updateUser] = useUpdateUserMutation();
|
|
|
|
+
|
|
|
|
+ const handleClose = (event, reason) => {
|
|
|
|
+ if (reason === 'clickaway') {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ setOpen(false);
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ const onSubmit = () => {
|
|
|
|
+ const user = {
|
|
|
|
+ _id: data.UserFindOne._id,
|
|
|
|
+ nick,
|
|
|
|
+ password: password || undefined,
|
|
|
|
+ avatar: avatarId ? {
|
|
|
|
+ _id: avatarId
|
|
|
|
+ } : undefined
|
|
|
|
+ }
|
|
|
|
+ if (password !== confirmPassword) {
|
|
|
|
+ setSnackbarType('error');
|
|
|
|
+ setSnackbarMessage('Passwords don\'t match!');
|
|
|
|
+ setOpen(true);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ updateUser(user).then(response => {
|
|
|
|
+ setSnackbarType('success');
|
|
|
|
+ setSnackbarMessage('Saved!');
|
|
|
|
+ setOpen(true);
|
|
|
|
+
|
|
|
|
+ }).catch(e => {
|
|
|
|
+ setSnackbarType('error');
|
|
|
|
+ setSnackbarMessage('Ooops! Something went wrong!');
|
|
|
|
+ setOpen(true);
|
|
|
|
+ });
|
|
|
|
+ };
|
|
|
|
|
|
const handleMouseDownConfirmPassword = (event) => {
|
|
const handleMouseDownConfirmPassword = (event) => {
|
|
event.preventDefault();
|
|
event.preventDefault();
|
|
@@ -21,90 +68,84 @@ function EditProfilePage() {
|
|
event.preventDefault();
|
|
event.preventDefault();
|
|
};
|
|
};
|
|
|
|
|
|
- return (
|
|
|
|
- <Box>
|
|
|
|
- <Title>Edit profile</Title>
|
|
|
|
- <Box sx={{ display: 'flex', alignItems: 'center', mb: '40px' }}>
|
|
|
|
- <Avatar sx={{ width: '150px', height: '150px' }} />
|
|
|
|
- <Button sx={{ ml: '20px' }}><Edit /> Edit avatar</Button>
|
|
|
|
- </Box>
|
|
|
|
- <Box component='form' sx={{ display: 'flex', alignItems: 'center', mb: '40px' }}>
|
|
|
|
- <FormControl variant="outlined">
|
|
|
|
- <OutlinedInput
|
|
|
|
- placeholder="New nick"
|
|
|
|
- type="text"
|
|
|
|
- value={nick}
|
|
|
|
- onChange={e => setNick(e.target.value)} />
|
|
|
|
- </FormControl>
|
|
|
|
- <Button sx={{ ml: '20px' }}>Change nick</Button>
|
|
|
|
- </Box>
|
|
|
|
- <Box component='form' sx={{ display: 'flex', alignItems: 'center' }}>
|
|
|
|
- <Stack>
|
|
|
|
- <FormControl variant="outlined" sx={{ mb: '20px' }}>
|
|
|
|
- <OutlinedInput
|
|
|
|
- placeholder="password"
|
|
|
|
- type={showPassword ? 'text' : 'password'}
|
|
|
|
- value={password}
|
|
|
|
- onChange={e => setPassword(e.target.value)}
|
|
|
|
- endAdornment={
|
|
|
|
- <InputAdornment position="end">
|
|
|
|
- <IconButton
|
|
|
|
- onClick={handleClickShowPassword}
|
|
|
|
- onMouseDown={handleMouseDownPassword}
|
|
|
|
- edge="end"
|
|
|
|
- >
|
|
|
|
- {showPassword ? <VisibilityOff /> : <Visibility />}
|
|
|
|
- </IconButton>
|
|
|
|
- </InputAdornment>
|
|
|
|
- } />
|
|
|
|
- </FormControl>
|
|
|
|
- <FormControl variant="outlined" sx={{ mb: '20px' }}>
|
|
|
|
- <OutlinedInput
|
|
|
|
- placeholder="Confirm password"
|
|
|
|
- type={showConfirmPassword ? 'text' : 'password'}
|
|
|
|
- error={password !== confirmPassword}
|
|
|
|
- value={confirmPassword}
|
|
|
|
- onChange={e => setConfirmPassword(e.target.value)}
|
|
|
|
- endAdornment={
|
|
|
|
- <InputAdornment position="end">
|
|
|
|
- <IconButton
|
|
|
|
- onClick={handleClickShowConfirmPassword}
|
|
|
|
- onMouseDown={handleMouseDownConfirmPassword}
|
|
|
|
- edge="end"
|
|
|
|
- >
|
|
|
|
- {showConfirmPassword ? <VisibilityOff /> : <Visibility />}
|
|
|
|
- </IconButton>
|
|
|
|
- </InputAdornment>
|
|
|
|
- } />
|
|
|
|
- </FormControl>
|
|
|
|
- </Stack>
|
|
|
|
- <Button sx={{ ml: '20px' }}>Change password</Button>
|
|
|
|
- </Box>
|
|
|
|
- </Box>
|
|
|
|
- )
|
|
|
|
-}
|
|
|
|
|
|
+ const Alert = React.forwardRef(function Alert(props, ref) {
|
|
|
|
+ return <MuiAlert elevation={6} ref={ref} variant="filled" {...props} />;
|
|
|
|
+ });
|
|
|
|
|
|
-export default EditProfilePage;
|
|
|
|
|
|
+ return (
|
|
|
|
+ <>
|
|
|
|
+ {isFetching ? <Loader /> :
|
|
|
|
+ <Box>
|
|
|
|
+ <Title>Edit profile</Title>
|
|
|
|
+ <Box sx={{ maxWidth: '250px', width: '100%' }}>
|
|
|
|
+ <Box sx={{ display: 'flex', alignItems: 'flex-start', mb: '40px', width: '100%' }}>
|
|
|
|
+ <ImageUploader previousImages={[data.UserFindOne.avatar]} isAvatar={true} onChange={(images) => setAvatarId(images[0]._id)} />
|
|
|
|
+ </Box>
|
|
|
|
+ <Box component='form' sx={{ display: 'flex', alignItems: 'flex-start', mb: '40px', width: '100%' }}>
|
|
|
|
+ <FormControl variant="outlined" sx={{ width: '100%' }}>
|
|
|
|
+ <OutlinedInput
|
|
|
|
+ sx={{ width: '100%' }}
|
|
|
|
+ placeholder="Nick"
|
|
|
|
+ type="text"
|
|
|
|
+ value={nick}
|
|
|
|
+ onChange={e => setNick(e.target.value)} />
|
|
|
|
+ </FormControl>
|
|
|
|
+ </Box>
|
|
|
|
+ <Box component='form' sx={{ display: 'flex', alignItems: 'flex-start', width: '100%' }}>
|
|
|
|
+ <Stack sx={{ width: '100%' }}>
|
|
|
|
+ <FormControl variant="outlined" sx={{ mb: '20px', width: '100%' }}>
|
|
|
|
+ <OutlinedInput
|
|
|
|
+ sx={{ width: '100%' }}
|
|
|
|
+ placeholder="password"
|
|
|
|
+ type={showPassword ? 'text' : 'password'}
|
|
|
|
+ value={password}
|
|
|
|
+ onChange={e => setPassword(e.target.value)}
|
|
|
|
+ endAdornment={
|
|
|
|
+ <InputAdornment position="end">
|
|
|
|
+ <IconButton
|
|
|
|
+ onClick={handleClickShowPassword}
|
|
|
|
+ onMouseDown={handleMouseDownPassword}
|
|
|
|
+ edge="end"
|
|
|
|
+ >
|
|
|
|
+ {showPassword ? <VisibilityOff /> : <Visibility />}
|
|
|
|
+ </IconButton>
|
|
|
|
+ </InputAdornment>
|
|
|
|
+ } />
|
|
|
|
+ </FormControl>
|
|
|
|
+ <FormControl variant="outlined" sx={{ mb: '20px', width: '100%' }}>
|
|
|
|
+ <OutlinedInput
|
|
|
|
+ sx={{ width: '100%' }}
|
|
|
|
+ placeholder="Confirm password"
|
|
|
|
+ type={showConfirmPassword ? 'text' : 'password'}
|
|
|
|
+ error={password !== confirmPassword}
|
|
|
|
+ value={confirmPassword}
|
|
|
|
+ onChange={e => setConfirmPassword(e.target.value)}
|
|
|
|
+ endAdornment={
|
|
|
|
+ <InputAdornment position="end">
|
|
|
|
+ <IconButton
|
|
|
|
+ onClick={handleClickShowConfirmPassword}
|
|
|
|
+ onMouseDown={handleMouseDownConfirmPassword}
|
|
|
|
+ edge="end"
|
|
|
|
+ >
|
|
|
|
+ {showConfirmPassword ? <VisibilityOff /> : <Visibility />}
|
|
|
|
+ </IconButton>
|
|
|
|
+ </InputAdornment>
|
|
|
|
+ } />
|
|
|
|
+ </FormControl>
|
|
|
|
+ </Stack>
|
|
|
|
+ </Box>
|
|
|
|
+ <Button onClick={() => onSubmit()} variant="contained" sx={{ width: '100%' }}>Save changes</Button>
|
|
|
|
|
|
-{/*
|
|
|
|
|
|
+ <Snackbar open={open} autoHideDuration={3000} onClose={handleClose}>
|
|
|
|
+ <Alert onClose={handleClose} severity={snackbarType} sx={{ width: '100%' }}>
|
|
|
|
+ {snackbarMessage}
|
|
|
|
+ </Alert>
|
|
|
|
+ </Snackbar>
|
|
|
|
+ </Box>
|
|
|
|
+ </Box>}
|
|
|
|
+ </>
|
|
|
|
|
|
-const ShowNick = () => {
|
|
|
|
- const {data, isFetching} = useGetUserByIdQuery('632205aeb74e1f5f2ec1a320')
|
|
|
|
- if (isFetching){
|
|
|
|
- return <h1>Loading</h1>
|
|
|
|
- }
|
|
|
|
- console.log(data)
|
|
|
|
- return (
|
|
|
|
- <h1>NICK: {data && data.UserFindOne && data.UserFindOne.nick}</h1>
|
|
|
|
- )
|
|
|
|
|
|
+ )
|
|
}
|
|
}
|
|
- const SetNick = () => {
|
|
|
|
- const [nick, setNickState] = useState('')
|
|
|
|
- const dispatch = useDispatch()
|
|
|
|
- return (
|
|
|
|
- <div>
|
|
|
|
- <input value={nick} onChange={e => setNickState(e.target.value)}/>
|
|
|
|
- <button onClick={() => dispatch(actionSetNick(nick))}>Save</button>
|
|
|
|
- </div>
|
|
|
|
- )
|
|
|
|
-} */}
|
|
|
|
|
|
+
|
|
|
|
+export default EditProfilePage;
|