diff --git a/Client/src/Components/TabPanels/ProfileSettings/ProfilePanel.jsx b/Client/src/Components/TabPanels/ProfileSettings/ProfilePanel.jsx index ccf602993..18b47e15b 100644 --- a/Client/src/Components/TabPanels/ProfileSettings/ProfilePanel.jsx +++ b/Client/src/Components/TabPanels/ProfileSettings/ProfilePanel.jsx @@ -23,18 +23,20 @@ const ProfilePanel = () => { const theme = useTheme(); const dispatch = useDispatch(); - const { user, isLoading } = useSelector((state) => state.auth); - console.log(user); + const { user, authToken, isLoading } = useSelector((state) => state.auth); const idToName = { "edit-first-name": "firstname", "edit-last-name": "lastname", - "edit-email": "email", + //Disabled for now, will revisit in the future + // "edit-email": "email", }; const [localData, setLocalData] = useState({ firstname: user.firstname, lastname: user.lastname, - email: user.email, - profilePicUrl: "" + //Disabled for now, will revisit in the future + // email: user.email, + //TODO - upload picture + profilePicUrl: "placeholder", }); const [errors, setErrors] = useState({}); const [isOpen, setIsOpen] = useState(false); @@ -73,13 +75,25 @@ const ProfilePanel = () => { }, 2000); }; //TODO - implement update profile function - const handleUpdatePicture = () => {}; + const handleUpdatePicture = () => { + setLocalData((prev) => ({ ...prev, profilePicUrl: file })); + console.log(localData); + }; //TODO - implement delete account function const handleDeleteAccount = () => {}; //TODO - implement save profile function const handleSaveProfile = (event) => { event.preventDefault(); - dispatch(update(user._id, localData)); + if ( + localData.firstname === user.firstname && + localData.lastname === user.lastname && + localData.profilePicUrl === user.profilePicUrl + ) { + //TODO - add toast(profile data is unchanged) and maybe disable button + return; + } + + dispatch(update({ authToken, localData })); }; return ( @@ -141,18 +155,19 @@ const ProfilePanel = () => { Email - After updating, you'll receive a confirmation email. + This is your current email address — it cannot be changed. {errors[idToName["edit-email"]] ? ( @@ -214,7 +229,7 @@ const ProfilePanel = () => { level="primary" label="Save" onClick={handleSaveProfile} - isLoading={false} + isLoading={isLoading} loadingText="Saving..." disabled={Object.keys(errors).length !== 0 && true} sx={{ diff --git a/Client/src/Components/TextFields/Email/EmailTextField.jsx b/Client/src/Components/TextFields/Email/EmailTextField.jsx index e782cffed..1e199e7da 100644 --- a/Client/src/Components/TextFields/Email/EmailTextField.jsx +++ b/Client/src/Components/TextFields/Email/EmailTextField.jsx @@ -19,7 +19,8 @@ import { useTheme } from "@mui/material"; * @param {React.Element} props.icon - The icon to be displayed in the text field. * @param {string} props.helperText - The helper text to be displayed below the text field. * @param {boolean} props.error - If true, the text field will indicate an error state. - * + * @param {boolean} props.disabled - If true, the text field will be disabled and cannot be interacted with. + * * @example * // To use this component, import it and use it in your JSX like this: * @@ -38,6 +39,7 @@ const EmailTextField = ({ icon, helperText, error, + disabled }) => { const theme = useTheme(); @@ -65,6 +67,7 @@ const EmailTextField = ({ variant={variant} placeholder={placeholder} autoComplete={autoComplete} + disabled={disabled} InputProps={{ endAdornment: ( @@ -81,7 +84,7 @@ const EmailTextField = ({ style={{ fill: "red", width: "16px", height: "16px" }} /> )} - {!error && showIcon && ( + {!error && showIcon && !disabled && ( { export const update = createAsyncThunk( "auth/update", - async (user_id, form, thunkApi) => { + async (data, thunkApi) => { + const { authToken: token, localData: form } = data; + const user = jwtDecode(token); try { - const res = await axios.post(`${BASE_URL}/auth/user/${user_id}`, form); + //1.5s delay to show loading spinner + await new Promise((resolve) => setTimeout(resolve, 1500)); + + const res = await axios.post(`${BASE_URL}/auth/user/${user._id}`, form, { + headers: { + Authorization: `Bearer ${token}`, + "Content-Type": "application/json", + }, + }); return res.data; } catch (error) { - console.log(error); return thunkApi.rejectWithValue(error.response.data); } } @@ -68,6 +77,12 @@ const handleAuthRejected = (state, action) => { state.success = action.payload.success; state.msg = action.payload.msg; }; +const handleUpdateFulfilled = (state, action) => { + state.isLoading = false; + state.success = action.payload.success; + state.msg = action.payload.msg; + state.user = action.payload.data; +} const authSlice = createSlice({ name: "auth", @@ -99,7 +114,7 @@ const authSlice = createSlice({ .addCase(update.pending, (state) => { state.isLoading = true; }) - .addCase(update.fulfilled, handleAuthFulfilled) + .addCase(update.fulfilled, handleUpdateFulfilled) .addCase(update.rejected, handleAuthRejected); }, }); diff --git a/Client/src/Pages/Settings/index.css b/Client/src/Pages/Settings/index.css index 291602858..0769c9117 100644 --- a/Client/src/Pages/Settings/index.css +++ b/Client/src/Pages/Settings/index.css @@ -171,11 +171,19 @@ } } -.settings .MuiStack-root:has(p.MuiTypography-root.input-error){ +.settings .MuiStack-root:has(p.MuiTypography-root.input-error) { position: relative; margin-bottom: 8px; } -.settings p.MuiTypography-root.input-error{ +.settings .text-field-icon { + position: absolute; + top: 50%; + transform: translateY(-50%); + width: 18px !important; + height: 18px !important; + opacity: 0.8; +} +.settings p.MuiTypography-root.input-error { color: var(--env-var-color-24); opacity: 0.8; position: absolute; @@ -187,10 +195,10 @@ } .edit-profile-form__wrapper .MuiFormControl-root, -.edit-password-form__wrapper .MuiFormControl-root{ +.edit-password-form__wrapper .MuiFormControl-root { width: 100%; } .edit-profile-form__wrapper .MuiStack-root:has(.MuiFormControl-root), -.edit-password-form__wrapper .MuiStack-root:has(.MuiFormControl-root){ +.edit-password-form__wrapper .MuiStack-root:has(.MuiFormControl-root) { margin: 0; } \ No newline at end of file