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 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
</Typography>
<Typography variant="h5" component="p">
After updating, you'll receive a confirmation email.
This is your current email address — it cannot be changed.
</Typography>
</Stack>
<Stack>
<EmailTextField
id="edit-email"
label={null}
value={localData.email}
value={user.email}
placeholder="Enter your email"
autoComplete="email"
onChange={handleChange}
error={errors[idToName["edit-email"]] ? true : false}
// onChange={handleChange}
// error={errors[idToName["edit-email"]] ? true : false}
disabled={true}
/>
{errors[idToName["edit-email"]] ? (
<Typography variant="h5" component="p" className="input-error">
@@ -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={{
@@ -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:
* <EmailTextField id="email" variant="outlined" placeholder="Enter your email" />
@@ -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: (
<InputAdornment position="end">
@@ -81,7 +84,7 @@ const EmailTextField = ({
style={{ fill: "red", width: "16px", height: "16px" }}
/>
)}
{!error && showIcon && (
{!error && showIcon && !disabled && (
<HelpOutlineIcon
className="text-field-icon"
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(
"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);
},
});
+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;
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;
}