From 8b8c43c21bde5379825c5ec44ecd81a92425f605 Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Tue, 22 Nov 2022 00:03:25 +0500 Subject: [PATCH] feat(web): add the UI for updating user password --- web/src/components/passwordModal.tsx | 141 ++++++++++++++++++ web/src/containers/Settings/profile.tsx | 182 +++++++++++++++--------- 2 files changed, 254 insertions(+), 69 deletions(-) create mode 100644 web/src/components/passwordModal.tsx diff --git a/web/src/components/passwordModal.tsx b/web/src/components/passwordModal.tsx new file mode 100644 index 0000000..4482367 --- /dev/null +++ b/web/src/components/passwordModal.tsx @@ -0,0 +1,141 @@ +import React, { useEffect, useState } from 'react' + +import { + Grid, + DialogContent, + DialogActions, + Button, + OutlinedInput, + InputAdornment, + IconButton, + FormControl, + InputLabel, + FormHelperText +} from '@mui/material' +import Visibility from '@mui/icons-material/Visibility' +import VisibilityOff from '@mui/icons-material/VisibilityOff' + +import { BootstrapDialogTitle } from './dialogTitle' +import { BootstrapDialog } from './modal' + +type Props = { + open: boolean + setOpen: React.Dispatch> + title: string + updatePassword: (currentPassword: string, newPassword: string) => void +} + +const UpdatePasswordModal = (props: Props) => { + const { open, setOpen, title, updatePassword } = props + const [currentPassword, setCurrentPassword] = useState('') + const [newPassword, setNewPassword] = useState('') + const [hasError, setHasError] = useState(false) + const [errorText, setErrorText] = useState('') + + useEffect(() => { + if (newPassword.length >= 6) { + setErrorText('') + setHasError(false) + } + }, [newPassword]) + + const handleBlur = () => { + if (newPassword.length < 6) { + setErrorText('Password length should be at least 6 characters.') + setHasError(true) + } else { + setErrorText('') + setHasError(false) + } + } + + return ( +
+ setOpen(false)} open={open}> + + {title} + + + + + + + + + + + + + + + + +
+ ) +} + +export default UpdatePasswordModal + +type PasswordInputProps = { + label: string + password: string + setPassword: React.Dispatch> + hasError?: boolean + errorText?: string + handleBlur?: () => void +} + +const PasswordInput = ({ + label, + password, + setPassword, + hasError, + errorText, + handleBlur +}: PasswordInputProps) => { + const [showPassword, setShowPassword] = useState(false) + + return ( + + {label} + setPassword(e.target.value)} + onBlur={handleBlur} + endAdornment={ + + setShowPassword((val) => !val)} + edge="end" + > + {showPassword ? : } + + + } + /> + {errorText && {errorText}} + + ) +} diff --git a/web/src/containers/Settings/profile.tsx b/web/src/containers/Settings/profile.tsx index 06602e7..5f36610 100644 --- a/web/src/containers/Settings/profile.tsx +++ b/web/src/containers/Settings/profile.tsx @@ -17,11 +17,13 @@ import { 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) @@ -36,7 +38,7 @@ const Profile = () => { .finally(() => { setIsLoading(false) }) - }, []) + }, [appContext.userId]) const handleChange = (event: any) => { const { name, value } = event.target @@ -68,82 +70,124 @@ const Profile = () => { }) } + 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" + + + + - + + + + + + + + + } + label="isActive" + /> + } + label="isAdmin" + /> + + + + + + - - - - - - - + + + + + + + + ) }