Implemented redux integration - profile panel

This commit is contained in:
Daniel Cojocea committed 2024-06-25 13:01:21 -04:00
1 parent ee08bac389
commit 4edc18a9f4
4 files changed
+63 -22

No files matched your search

@@ -23,18 +23,20 @@ const ProfilePanel = () => {
const theme = useTheme(); const theme = useTheme();
const dispatch = useDispatch(); const dispatch = useDispatch();
const { user, isLoading } = useSelector((state) => state.auth); const { user, authToken, isLoading } = useSelector((state) => state.auth);
console.log(user);
const idToName = { const idToName = {
"edit-first-name": "firstname", "edit-first-name": "firstname",
"edit-last-name": "lastname", "edit-last-name": "lastname",
"edit-email": "email", //Disabled for now, will revisit in the future
// "edit-email": "email",
}; };
const [localData, setLocalData] = useState({ const [localData, setLocalData] = useState({
firstname: user.firstname, firstname: user.firstname,
lastname: user.lastname, lastname: user.lastname,
email: user.email, //Disabled for now, will revisit in the future
profilePicUrl: "" // email: user.email,
//TODO - upload picture
profilePicUrl: "placeholder",
}); });
const [errors, setErrors] = useState({}); const [errors, setErrors] = useState({});
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
@@ -73,13 +75,25 @@ const ProfilePanel = () => {
}, 2000); }, 2000);
}; };
//TODO - implement update profile function //TODO - implement update profile function
const handleUpdatePicture = () => {}; const handleUpdatePicture = () => {
setLocalData((prev) => ({ ...prev, profilePicUrl: file }));
console.log(localData);
};
//TODO - implement delete account function //TODO - implement delete account function
const handleDeleteAccount = () => {}; const handleDeleteAccount = () => {};
//TODO - implement save profile function //TODO - implement save profile function
const handleSaveProfile = (event) => { const handleSaveProfile = (event) => {
event.preventDefault(); 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 ( return (
@@ -141,18 +155,19 @@ const ProfilePanel = () => {
Email Email
</Typography> </Typography>
<Typography variant="h5" component="p"> <Typography variant="h5" component="p">
After updating, you'll receive a confirmation email. This is your current email address — it cannot be changed.
</Typography> </Typography>
</Stack> </Stack>
<Stack> <Stack>
<EmailTextField <EmailTextField
id="edit-email" id="edit-email"
label={null} label={null}
value={localData.email} value={user.email}
placeholder="Enter your email" placeholder="Enter your email"
autoComplete="email" autoComplete="email"
onChange={handleChange} // onChange={handleChange}
error={errors[idToName["edit-email"]] ? true : false} // error={errors[idToName["edit-email"]] ? true : false}
disabled={true}
/> />
{errors[idToName["edit-email"]] ? ( {errors[idToName["edit-email"]] ? (
<Typography variant="h5" component="p" className="input-error"> <Typography variant="h5" component="p" className="input-error">
@@ -214,7 +229,7 @@ const ProfilePanel = () => {
level="primary" level="primary"
label="Save" label="Save"
onClick={handleSaveProfile} onClick={handleSaveProfile}
isLoading={false} isLoading={isLoading}
loadingText="Saving..." loadingText="Saving..."
disabled={Object.keys(errors).length !== 0 && true} disabled={Object.keys(errors).length !== 0 && true}
sx={{ sx={{
@@ -19,7 +19,8 @@ import { useTheme } from "@mui/material";
* @param {React.Element} props.icon - The icon to be displayed in the text field. * @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 {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.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 * @example
* // To use this component, import it and use it in your JSX like this: * // To use this component, import it and use it in your JSX like this:
* <EmailTextField id="email" variant="outlined" placeholder="Enter your email" /> * <EmailTextField id="email" variant="outlined" placeholder="Enter your email" />
@@ -38,6 +39,7 @@ const EmailTextField = ({
icon, icon,
helperText, helperText,
error, error,
disabled
}) => { }) => {
const theme = useTheme(); const theme = useTheme();
@@ -65,6 +67,7 @@ const EmailTextField = ({
variant={variant} variant={variant}
placeholder={placeholder} placeholder={placeholder}
autoComplete={autoComplete} autoComplete={autoComplete}
disabled={disabled}
InputProps={{ InputProps={{
endAdornment: ( endAdornment: (
<InputAdornment position="end"> <InputAdornment position="end">
@@ -81,7 +84,7 @@ const EmailTextField = ({
style={{ fill: "red", width: "16px", height: "16px" }} style={{ fill: "red", width: "16px", height: "16px" }}
/> />
)} )}
{!error && showIcon && ( {!error && showIcon && !disabled && (
<HelpOutlineIcon <HelpOutlineIcon
className="text-field-icon" className="text-field-icon"
style={{ width: "16px", height: "16px" }} style={{ width: "16px", height: "16px" }}
+19 -4
View File
@@ -34,12 +34,21 @@ export const login = createAsyncThunk("auth/login", async (form, thunkApi) => {
export const update = createAsyncThunk( export const update = createAsyncThunk(
"auth/update", "auth/update",
async (user_id, form, thunkApi) => { async (data, thunkApi) => {
const { authToken: token, localData: form } = data;
const user = jwtDecode(token);
try { 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; return res.data;
} catch (error) { } catch (error) {
console.log(error);
return thunkApi.rejectWithValue(error.response.data); return thunkApi.rejectWithValue(error.response.data);
} }
} }
@@ -68,6 +77,12 @@ const handleAuthRejected = (state, action) => {
state.success = action.payload.success; state.success = action.payload.success;
state.msg = action.payload.msg; 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({ const authSlice = createSlice({
name: "auth", name: "auth",
@@ -99,7 +114,7 @@ const authSlice = createSlice({
.addCase(update.pending, (state) => { .addCase(update.pending, (state) => {
state.isLoading = true; state.isLoading = true;
}) })
.addCase(update.fulfilled, handleAuthFulfilled) .addCase(update.fulfilled, handleUpdateFulfilled)
.addCase(update.rejected, handleAuthRejected); .addCase(update.rejected, handleAuthRejected);
}, },
}); });
+12 -4
View File
@@ -171,11 +171,19 @@
} }
} }
.settings .MuiStack-root:has(p.MuiTypography-root.input-error){ .settings .MuiStack-root:has(p.MuiTypography-root.input-error) {
position: relative; position: relative;
margin-bottom: 8px; 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); color: var(--env-var-color-24);
opacity: 0.8; opacity: 0.8;
position: absolute; position: absolute;
@@ -187,10 +195,10 @@
} }
.edit-profile-form__wrapper .MuiFormControl-root, .edit-profile-form__wrapper .MuiFormControl-root,
.edit-password-form__wrapper .MuiFormControl-root{ .edit-password-form__wrapper .MuiFormControl-root {
width: 100%; width: 100%;
} }
.edit-profile-form__wrapper .MuiStack-root:has(.MuiFormControl-root), .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; margin: 0;
} }