Initial commit

This commit is contained in:
Daniel Cojocea committed 2024-06-24 21:32:30 -04:00
1 parent 11c1819951
commit ee08bac389
5 files changed
+63 -32

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,24 @@ 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, 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", "edit-email": "email",
}; };
const [localData, setLocalData] = useState({ const [localData, setLocalData] = useState({
firstname: "", firstname: user.firstname,
lastname: "", lastname: user.lastname,
email: "", email: user.email,
profilePicUrl: ""
}); });
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 +47,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 +67,19 @@ 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);
//TODO - implement delete account function //TODO - implement delete account function
const handleDeleteAccount = () => { const handleDeleteAccount = () => {};
setIsLoading(true);
setTimeout(() => {
setIsLoading(false);
setIsOpen(false);
}, 2000);
};
//TODO - implement save profile function //TODO - implement save profile function
const handleSaveProfile = () => { const handleSaveProfile = (event) => {
setIsLoading(true); event.preventDefault();
setTimeout(() => { dispatch(update(user._id, localData));
setIsLoading(false);
setIsOpen(false);
}, 2000);
}; };
return ( return (
@@ -101,6 +95,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 +120,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}
@@ -152,6 +148,7 @@ const ProfilePanel = () => {
<EmailTextField <EmailTextField
id="edit-email" id="edit-email"
label={null} label={null}
value={localData.email}
placeholder="Enter your email" placeholder="Enter your email"
autoComplete="email" autoComplete="email"
onChange={handleChange} onChange={handleChange}
@@ -179,8 +176,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 +189,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
@@ -217,7 +214,7 @@ const ProfilePanel = () => {
level="primary" level="primary"
label="Save" label="Save"
onClick={handleSaveProfile} onClick={handleSaveProfile}
isLoading={isLoading} isLoading={false}
loadingText="Saving..." loadingText="Saving..."
disabled={Object.keys(errors).length !== 0 && true} disabled={Object.keys(errors).length !== 0 && true}
sx={{ sx={{
@@ -13,6 +13,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="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.
@@ -30,6 +31,7 @@ const EmailTextField = ({
onChange, onChange,
id, id,
label = "Email", label = "Email",
value = undefined,
variant, variant,
placeholder, placeholder,
autoComplete, autoComplete,
@@ -59,6 +61,7 @@ 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}
@@ -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={{
+29 -1
View File
@@ -32,6 +32,28 @@ export const login = createAsyncThunk("auth/login", async (form, thunkApi) => {
} }
}); });
export const update = createAsyncThunk(
"auth/update",
async (user_id, form, thunkApi) => {
try {
const res = await axios.post(`${BASE_URL}/auth/user/${user_id}`, form);
return res.data;
} catch (error) {
console.log(error);
return thunkApi.rejectWithValue(error.response.data);
}
}
);
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;
@@ -72,7 +94,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, handleAuthFulfilled)
.addCase(update.rejected, handleAuthRejected);
}, },
}); });
+3 -3
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;