Merge pull request #191 from bluewave-labs/feat/profile-settings-redux

Integrated redux, #173 and #182
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-06-25 17:26:06 -04:00
commit 140b8eecba
5 files changed
+124 -42

No files matched your search

@@ -8,6 +8,8 @@ import EmailTextField from "../../TextFields/Email/EmailTextField";
import StringTextField from "../../TextFields/Text/TextField"; import StringTextField from "../../TextFields/Text/TextField";
import Avatar from "../../Avatar"; import Avatar from "../../Avatar";
import { editProfileValidation } from "../../../Validation/validation"; import { editProfileValidation } from "../../../Validation/validation";
import { useDispatch, useSelector } from "react-redux";
import { update } from "../../../Features/Auth/authSlice";
/** /**
* ProfilePanel component displays a form for editing user profile information * ProfilePanel component displays a form for editing user profile information
@@ -19,22 +21,26 @@ import { editProfileValidation } from "../../../Validation/validation";
const ProfilePanel = () => { const ProfilePanel = () => {
const theme = useTheme(); const theme = useTheme();
//TODO - use redux loading state const dispatch = useDispatch();
//!! - currently all loading buttons are tied to the same state
const [isLoading, setIsLoading] = useState(false);
//for testing, will tweak when I implement redux slice const { user, authToken, isLoading } = useSelector((state) => state.auth);
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: "", firstname: user.firstname,
lastname: "", lastname: user.lastname,
email: "", //Disabled for now, will revisit in the future
// email: user.email,
//TODO - upload picture
profilePicUrl: "placeholder",
}); });
const [errors, setErrors] = useState({}); const [errors, setErrors] = useState({});
const [isOpen, setIsOpen] = useState(false);
const [loadingPfp, setLoadingPfp] = useState(false);
const handleChange = (event) => { const handleChange = (event) => {
const { value, id } = event.target; const { value, id } = event.target;
@@ -43,7 +49,7 @@ const ProfilePanel = () => {
...prev, ...prev,
[name]: value, [name]: value,
})); }));
const validation = editProfileValidation.validate( const validation = editProfileValidation.validate(
{ [name]: value }, { [name]: value },
{ abortEarly: false } { abortEarly: false }
@@ -63,29 +69,31 @@ const ProfilePanel = () => {
//TODO - implement delete profile picture function //TODO - implement delete profile picture function
const handleDeletePicture = () => { const handleDeletePicture = () => {
setIsLoading(true); setLoadingPfp(true);
setTimeout(() => { setTimeout(() => {
setIsLoading(false); setLoadingPfp(false);
}, 2000); }, 2000);
}; };
//TODO - implement update profile function //TODO - implement update profile function
const handleUpdatePicture = () => {}; const handleUpdatePicture = () => {
const [isOpen, setIsOpen] = useState(false); setLocalData((prev) => ({ ...prev, profilePicUrl: file }));
//TODO - implement delete account function console.log(localData);
const handleDeleteAccount = () => {
setIsLoading(true);
setTimeout(() => {
setIsLoading(false);
setIsOpen(false);
}, 2000);
}; };
//TODO - implement delete account function
const handleDeleteAccount = () => {};
//TODO - implement save profile function //TODO - implement save profile function
const handleSaveProfile = () => { const handleSaveProfile = (event) => {
setIsLoading(true); event.preventDefault();
setTimeout(() => { if (
setIsLoading(false); localData.firstname === user.firstname &&
setIsOpen(false); localData.lastname === user.lastname &&
}, 2000); localData.profilePicUrl === user.profilePicUrl
) {
//TODO - add toast(profile data is unchanged) and maybe disable button
return;
}
dispatch(update({ authToken, localData }));
}; };
return ( return (
@@ -101,6 +109,7 @@ const ProfilePanel = () => {
<StringTextField <StringTextField
id="edit-first-name" id="edit-first-name"
label={null} label={null}
value={localData.firstname}
placeholder="Enter your first name" placeholder="Enter your first name"
autoComplete="given-name" autoComplete="given-name"
onChange={handleChange} onChange={handleChange}
@@ -125,6 +134,7 @@ const ProfilePanel = () => {
<StringTextField <StringTextField
id="edit-last-name" id="edit-last-name"
label={null} label={null}
value={localData.lastname}
placeholder="Enter your last name" placeholder="Enter your last name"
autoComplete="family-name" autoComplete="family-name"
onChange={handleChange} onChange={handleChange}
@@ -145,17 +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={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">
@@ -179,8 +191,8 @@ const ProfilePanel = () => {
{/* TODO - Update placeholder values with redux data */} {/* TODO - Update placeholder values with redux data */}
<Avatar <Avatar
src="/static/images/avatar/2.jpg" src="/static/images/avatar/2.jpg"
firstName="Jackie" firstName={localData.firstname}
lastName="Dawn" lastName={localData.lastname}
sx={{ sx={{
width: "64px", width: "64px",
height: "64px", height: "64px",
@@ -192,7 +204,7 @@ const ProfilePanel = () => {
level="tertiary" level="tertiary"
label="Delete" label="Delete"
onClick={handleDeletePicture} onClick={handleDeletePicture}
isLoading={isLoading} isLoading={loadingPfp}
/> />
{/* TODO - modal popup for update pfp? */} {/* TODO - modal popup for update pfp? */}
<Button <Button
@@ -13,12 +13,14 @@ import { useTheme } from "@mui/material";
* @param {function} props.onChange - The function to call when the text field changes. * @param {function} props.onChange - The function to call when the text field changes.
* @param {string} props.id - The id of the text field. * @param {string} props.id - The id of the text field.
* @param {string} [props.label="Email"] - The label of the text field. * @param {string} [props.label="Email"] - The label of the text field.
* @param {string} props.value - The value of the text field.
* @param {string} props.variant - The variant of the text field. * @param {string} props.variant - The variant of the text field.
* @param {string} props.placeholder - The placeholder of the text field. * @param {string} props.placeholder - The placeholder of the text field.
* @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" />
@@ -30,12 +32,14 @@ const EmailTextField = ({
onChange, onChange,
id, id,
label = "Email", label = "Email",
value = undefined,
variant, variant,
placeholder, placeholder,
autoComplete, autoComplete,
icon, icon,
helperText, helperText,
error, error,
disabled
}) => { }) => {
const theme = useTheme(); const theme = useTheme();
@@ -59,9 +63,11 @@ const EmailTextField = ({
error={error} error={error}
className="email-text-field-input" className="email-text-field-input"
id={id} id={id}
value={value}
variant={variant} variant={variant}
placeholder={placeholder} placeholder={placeholder}
autoComplete={autoComplete} autoComplete={autoComplete}
disabled={disabled}
InputProps={{ InputProps={{
endAdornment: ( endAdornment: (
<InputAdornment position="end"> <InputAdornment position="end">
@@ -78,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" }}
@@ -10,6 +10,7 @@ import { useTheme } from "@mui/material";
* @param {function} props.onChange - The function to call when the text field changes. * @param {function} props.onChange - The function to call when the text field changes.
* @param {string} props.id - The ID of the text field. * @param {string} props.id - The ID of the text field.
* @param {string} props.label - The label text for the text field. * @param {string} props.label - The label text for the text field.
* @param {string} props.value - The value of the text field.
* @param {string} props.variant - The variant of the text field. * @param {string} props.variant - The variant of the text field.
* @param {string} props.placeholder - The placeholder text for the text field. * @param {string} props.placeholder - The placeholder text for the text field.
* @param {ReactNode} props.icon - The icon to display in the text field. * @param {ReactNode} props.icon - The icon to display in the text field.
@@ -22,6 +23,7 @@ const StringTextField = ({
autoComplete, autoComplete,
id, id,
label, label,
value = undefined,
variant, variant,
placeholder, placeholder,
icon, icon,
@@ -51,6 +53,7 @@ const StringTextField = ({
className="email-text-field-input" className="email-text-field-input"
id={id} id={id}
variant={variant} variant={variant}
value={value}
placeholder={placeholder} placeholder={placeholder}
autoComplete={autoComplete} autoComplete={autoComplete}
InputProps={{ InputProps={{
+54 -1
View File
@@ -38,6 +38,40 @@ export const login = createAsyncThunk("auth/login", async (form, thunkApi) => {
} }
}); });
export const update = createAsyncThunk(
"auth/update",
async (data, thunkApi) => {
const { authToken: token, localData: form } = data;
const user = jwtDecode(token);
try {
//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) {
if (error.response && error.response.data) {
return thunkApi.rejectWithValue(error.response.data);
}
return thunkApi.rejectWithValue(error.message);
}
}
);
export const deleteAccount = createAsyncThunk(
"auth/delete",
async (form, thunkApi) => {
try {
//TODO
} catch (error) {}
}
);
const handleAuthFulfilled = (state, action) => { const handleAuthFulfilled = (state, action) => {
state.isLoading = false; state.isLoading = false;
state.success = action.payload.success; state.success = action.payload.success;
@@ -54,6 +88,19 @@ const handleAuthRejected = (state, action) => {
? action.payload.msg ? action.payload.msg
: "Failed to login or register"; : "Failed to login or register";
}; };
const handleUpdateFulfilled = (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
state.user = action.payload.data;
};
const handleUpdateRejected = (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to update profile data.";
};
const authSlice = createSlice({ const authSlice = createSlice({
name: "auth", name: "auth",
@@ -80,7 +127,13 @@ const authSlice = createSlice({
state.isLoading = true; state.isLoading = true;
}) })
.addCase(login.fulfilled, handleAuthFulfilled) .addCase(login.fulfilled, handleAuthFulfilled)
.addCase(login.rejected, handleAuthRejected); .addCase(login.rejected, handleAuthRejected)
// Update thunk
.addCase(update.pending, (state) => {
state.isLoading = true;
})
.addCase(update.fulfilled, handleUpdateFulfilled)
.addCase(update.rejected, handleUpdateRejected);
}, },
}); });
+15 -7
View File
@@ -32,11 +32,11 @@
.announcement-tile { .announcement-tile {
border-color: var(--env-var-color-29); border-color: var(--env-var-color-29);
} }
[class$="-form"] { .settings [class$="-form"] {
width: inherit; width: inherit;
} }
[class$="__wrapper"], .settings [class$="__wrapper"],
[class$="__wrapper compact"] { .settings [class$="__wrapper compact"] {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
justify-content: space-between; justify-content: space-between;
@@ -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;
} }