Added account parent route and fixed links
This commit is contained in:
1 parent
fd8a0d455c
commit
62d545df06
9 files changed
+304
-289
No files matched your search
@@ -0,0 +1,220 @@
|
||||
import TabPanel from "@mui/lab/TabPanel";
|
||||
import React, { useState } from "react";
|
||||
import AnnouncementsDualButtonWithIcon from "../../Announcements/AnnouncementsDualButtonWithIcon/AnnouncementsDualButtonWithIcon";
|
||||
import { useTheme } from "@emotion/react";
|
||||
import { Box, Divider, Stack, Typography } from "@mui/material";
|
||||
import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined";
|
||||
import WarningAmberOutlinedIcon from "@mui/icons-material/WarningAmberOutlined";
|
||||
import ButtonSpinner from "../../ButtonSpinner";
|
||||
import PasswordTextField from "../../TextFields/Password/PasswordTextField";
|
||||
import { editPasswordValidation } from "../../../Validation/validation";
|
||||
|
||||
/**
|
||||
* PasswordPanel component manages the form for editing password.
|
||||
*
|
||||
* @returns {JSX.Element}
|
||||
*/
|
||||
|
||||
const PasswordPanel = () => {
|
||||
const theme = useTheme();
|
||||
//TODO - use redux loading state
|
||||
//!! - currently all loading buttons are tied to the same state
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
|
||||
//for testing, will tweak when I implement redux slice
|
||||
const idToName = {
|
||||
"edit-current-password": "password",
|
||||
//TBD - form field naming
|
||||
"edit-new-password": "newpassword",
|
||||
"edit-confirm-password": "confirm",
|
||||
};
|
||||
const [localData, setLocalData] = useState({
|
||||
password: "",
|
||||
newpassword: "",
|
||||
confirm: "",
|
||||
});
|
||||
const [errors, setErrors] = useState({});
|
||||
|
||||
const handleChange = (event) => {
|
||||
const { value, id } = event.target;
|
||||
const name = idToName[id];
|
||||
setLocalData((prev) => ({
|
||||
...prev,
|
||||
[name]: value,
|
||||
}));
|
||||
|
||||
const validation = editPasswordValidation.validate(
|
||||
{ [name]: value },
|
||||
{ abortEarly: false, context: { newpassword: localData.newpassword } }
|
||||
);
|
||||
|
||||
setErrors((prev) => {
|
||||
const updatedErrors = { ...prev };
|
||||
|
||||
if (validation.error) {
|
||||
updatedErrors[name] = validation.error.details[0].message;
|
||||
} else {
|
||||
delete updatedErrors[name];
|
||||
}
|
||||
return updatedErrors;
|
||||
});
|
||||
};
|
||||
|
||||
const handleSubmit = (event) => {
|
||||
event.preventDefault();
|
||||
setIsLoading(true);
|
||||
|
||||
const { error } = editPasswordValidation.validate(localData, {
|
||||
abortEarly: false,
|
||||
context: { newpassword: localData.newpassword },
|
||||
});
|
||||
|
||||
if (error) {
|
||||
const newErrors = {};
|
||||
error.details.forEach((err) => {
|
||||
newErrors[err.path[0]] = err.message;
|
||||
});
|
||||
setErrors(newErrors);
|
||||
setIsLoading(false);
|
||||
} else {
|
||||
//TODO - submit logic
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
setIsLoading(false);
|
||||
}, 2000);
|
||||
};
|
||||
|
||||
return (
|
||||
<TabPanel value="password">
|
||||
<form
|
||||
onSubmit={handleSubmit}
|
||||
className="edit-password-form"
|
||||
noValidate
|
||||
spellCheck="false"
|
||||
>
|
||||
<div className="edit-password-form__wrapper">
|
||||
<AnnouncementsDualButtonWithIcon
|
||||
icon={
|
||||
<InfoOutlinedIcon
|
||||
style={{ fill: theme.palette.secondary.main }}
|
||||
/>
|
||||
}
|
||||
subject="SSO login"
|
||||
body="Since you logged in via SSO, you cannot reset or modify your password."
|
||||
/>
|
||||
</div>
|
||||
<div className="edit-password-form__wrapper">
|
||||
<Stack>
|
||||
<Typography variant="h4" component="h1">
|
||||
Current password
|
||||
</Typography>
|
||||
</Stack>
|
||||
<Stack>
|
||||
<PasswordTextField
|
||||
id="edit-current-password"
|
||||
label={null}
|
||||
placeholder="Enter your current password"
|
||||
autoComplete="current-password"
|
||||
visibility={showPassword}
|
||||
setVisibility={setShowPassword}
|
||||
onChange={handleChange}
|
||||
error={errors[idToName["edit-current-password"]] ? true : false}
|
||||
/>
|
||||
{errors[idToName["edit-current-password"]] ? (
|
||||
<Typography variant="h5" component="p" className="input-error">
|
||||
{errors[idToName["edit-current-password"]]}
|
||||
</Typography>
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
</Stack>
|
||||
</div>
|
||||
<div className="edit-password-form__wrapper">
|
||||
<Stack>
|
||||
<Typography variant="h4" component="h1">
|
||||
New password
|
||||
</Typography>
|
||||
</Stack>
|
||||
<Stack>
|
||||
<PasswordTextField
|
||||
id="edit-new-password"
|
||||
label={null}
|
||||
placeholder="Enter your new password"
|
||||
autoComplete="new-password"
|
||||
visibility={showPassword}
|
||||
setVisibility={setShowPassword}
|
||||
onChange={handleChange}
|
||||
error={errors[idToName["edit-new-password"]] ? true : false}
|
||||
/>
|
||||
{errors[idToName["edit-new-password"]] ? (
|
||||
<Typography variant="h5" component="p" className="input-error">
|
||||
{errors[idToName["edit-new-password"]]}
|
||||
</Typography>
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
</Stack>
|
||||
</div>
|
||||
<div className="edit-password-form__wrapper">
|
||||
<Stack>
|
||||
<Typography variant="h4" component="h1">
|
||||
Confirm new password
|
||||
</Typography>
|
||||
</Stack>
|
||||
<Stack>
|
||||
<PasswordTextField
|
||||
id="edit-confirm-password"
|
||||
label={null}
|
||||
placeholder="Reenter your new password"
|
||||
autoComplete="new-password"
|
||||
visibility={showPassword}
|
||||
setVisibility={setShowPassword}
|
||||
onChange={handleChange}
|
||||
error={errors[idToName["edit-confirm-password"]] ? true : false}
|
||||
/>
|
||||
{errors[idToName["edit-confirm-password"]] ? (
|
||||
<Typography variant="h5" component="p" className="input-error">
|
||||
{errors[idToName["edit-confirm-password"]]}
|
||||
</Typography>
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
</Stack>
|
||||
</div>
|
||||
<div className="edit-password-form__wrapper">
|
||||
<Stack></Stack>
|
||||
<Box sx={{ flex: 1 }}>
|
||||
<AnnouncementsDualButtonWithIcon
|
||||
icon={<WarningAmberOutlinedIcon style={{ fill: "#f79009" }} />}
|
||||
body="New password must contain at least 8 characters and must have at least one uppercase letter, one number and one symbol."
|
||||
/>
|
||||
</Box>
|
||||
</div>
|
||||
<Divider aria-hidden="true" width="0" />
|
||||
<Stack direction="row" justifyContent="flex-end">
|
||||
<Box width="fit-content">
|
||||
<ButtonSpinner
|
||||
level="primary"
|
||||
label="Save"
|
||||
onClick={handleSubmit}
|
||||
isLoading={isLoading}
|
||||
loadingText="Saving..."
|
||||
disabled={Object.keys(errors).length !== 0 && true}
|
||||
sx={{
|
||||
paddingX: "40px",
|
||||
}}
|
||||
/>
|
||||
</Box>
|
||||
</Stack>
|
||||
</form>
|
||||
</TabPanel>
|
||||
);
|
||||
};
|
||||
|
||||
PasswordPanel.propTypes = {
|
||||
// No props are being passed to this component, hence no specific PropTypes are defined.
|
||||
};
|
||||
|
||||
export default PasswordPanel;
|
||||
@@ -0,0 +1,438 @@
|
||||
import { useTheme } from "@emotion/react";
|
||||
import { useState } from "react";
|
||||
import TabPanel from "@mui/lab/TabPanel";
|
||||
import { Box, Divider, Modal, Stack, Typography } from "@mui/material";
|
||||
import ButtonSpinner from "../../ButtonSpinner";
|
||||
import Button from "../../Button";
|
||||
import EmailTextField from "../../TextFields/Email/EmailTextField";
|
||||
import StringTextField from "../../TextFields/Text/TextField";
|
||||
import Avatar from "../../Avatar";
|
||||
import { editProfileValidation } from "../../../Validation/validation";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import { update } from "../../../Features/Auth/authSlice";
|
||||
import ImageField from "../../TextFields/Image";
|
||||
import ImageIcon from "@mui/icons-material/Image";
|
||||
import ProgressUpload from "../../ProgressBars";
|
||||
|
||||
/**
|
||||
* ProfilePanel component displays a form for editing user profile information
|
||||
* and allows for actions like updating profile picture, credentials,
|
||||
* and deleting account.
|
||||
*
|
||||
* @returns {JSX.Element}
|
||||
*/
|
||||
|
||||
const ProfilePanel = () => {
|
||||
const theme = useTheme();
|
||||
const dispatch = useDispatch();
|
||||
|
||||
const { user, authToken, isLoading } = useSelector((state) => state.auth);
|
||||
const idToName = {
|
||||
"edit-first-name": "firstname",
|
||||
"edit-last-name": "lastname",
|
||||
//Disabled for now, will revisit in the future
|
||||
// "edit-email": "email",
|
||||
};
|
||||
const [localData, setLocalData] = useState({
|
||||
firstname: user.firstname,
|
||||
lastname: user.lastname,
|
||||
//Disabled for now, will revisit in the future
|
||||
// email: user.email,
|
||||
//TODO - upload picture
|
||||
profilePicUrl: "placeholder",
|
||||
});
|
||||
const [errors, setErrors] = useState({});
|
||||
const [isOpen, setIsOpen] = useState("");
|
||||
const isModalOpen = (name) => isOpen === name;
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const handleChange = (event) => {
|
||||
const { value, id } = event.target;
|
||||
const name = idToName[id];
|
||||
setLocalData((prev) => ({
|
||||
...prev,
|
||||
[name]: value,
|
||||
}));
|
||||
|
||||
const validation = editProfileValidation.validate(
|
||||
{ [name]: value },
|
||||
{ abortEarly: false }
|
||||
);
|
||||
|
||||
setErrors((prev) => {
|
||||
const updatedErrors = { ...prev };
|
||||
|
||||
if (validation.error) {
|
||||
updatedErrors[name] = validation.error.details[0].message;
|
||||
} else {
|
||||
delete updatedErrors[name];
|
||||
}
|
||||
return updatedErrors;
|
||||
});
|
||||
};
|
||||
|
||||
//TODO - add setPicture state to localData
|
||||
const [picture, setPicture] = useState();
|
||||
const [uploadProgress, setUploadProgress] = useState(0);
|
||||
const [isUploading, setIsUploading] = useState(false);
|
||||
const handlePicture = (event) => {
|
||||
const pic = event.target.files[0];
|
||||
console.log(pic);
|
||||
//TODO - add setPicture state to localData
|
||||
setPicture({ name: pic.name, type: pic.type, size: formatBytes(pic.size) });
|
||||
setIsUploading(true);
|
||||
|
||||
setTimeout(() => {
|
||||
//TODO - add setPicture state to localData
|
||||
setPicture((prev) => ({ ...prev, src: URL.createObjectURL(pic) }));
|
||||
}, 1500);
|
||||
|
||||
let progress = 0;
|
||||
const interval = setInterval(() => {
|
||||
progress += 10;
|
||||
setUploadProgress(progress);
|
||||
if (progress >= 100) {
|
||||
clearInterval(interval);
|
||||
}
|
||||
}, 150);
|
||||
};
|
||||
const formatBytes = (bytes) => {
|
||||
if (bytes === 0) return "0 Bytes";
|
||||
const k = 1024;
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " MB";
|
||||
};
|
||||
const handleCancelUpload = () => {
|
||||
//TODO - add setPicture state to localData
|
||||
setPicture(null);
|
||||
setUploadProgress(0);
|
||||
setIsUploading(false);
|
||||
};
|
||||
const handleClosePictureModal = () => {
|
||||
setIsOpen("");
|
||||
setUploadProgress(0);
|
||||
setIsUploading(false);
|
||||
};
|
||||
|
||||
//TODO - revisit once localData is set up properly
|
||||
const handleDeletePicture = () => {
|
||||
setLoading(true);
|
||||
setTimeout(() => {
|
||||
//TODO - add setPicture state to localData
|
||||
setPicture(null);
|
||||
setLoading(false);
|
||||
}, 2000);
|
||||
};
|
||||
//TODO - implement update profile function
|
||||
const handleUpdatePicture = () => {
|
||||
setIsOpen("");
|
||||
setUploadProgress(0);
|
||||
setIsUploading(false);
|
||||
};
|
||||
//TODO - implement delete account function
|
||||
const handleDeleteAccount = () => {};
|
||||
//TODO - implement save profile function
|
||||
const handleSaveProfile = (event) => {
|
||||
event.preventDefault();
|
||||
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 }));
|
||||
//TODO - add toast confirmation
|
||||
};
|
||||
|
||||
return (
|
||||
<TabPanel value="profile">
|
||||
<form className="edit-profile-form" noValidate spellCheck="false">
|
||||
<div className="edit-profile-form__wrapper">
|
||||
<Stack>
|
||||
<Typography variant="h4" component="h1">
|
||||
First name
|
||||
</Typography>
|
||||
</Stack>
|
||||
<Stack>
|
||||
<StringTextField
|
||||
id="edit-first-name"
|
||||
label={null}
|
||||
value={localData.firstname}
|
||||
placeholder="Enter your first name"
|
||||
autoComplete="given-name"
|
||||
onChange={handleChange}
|
||||
error={errors[idToName["edit-first-name"]] ? true : false}
|
||||
/>
|
||||
{errors[idToName["edit-first-name"]] ? (
|
||||
<Typography variant="h5" component="p" className="input-error">
|
||||
{errors[idToName["edit-first-name"]]}
|
||||
</Typography>
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
</Stack>
|
||||
</div>
|
||||
<div className="edit-profile-form__wrapper">
|
||||
<Stack>
|
||||
<Typography variant="h4" component="h1">
|
||||
Last name
|
||||
</Typography>
|
||||
</Stack>
|
||||
<Stack>
|
||||
<StringTextField
|
||||
id="edit-last-name"
|
||||
label={null}
|
||||
value={localData.lastname}
|
||||
placeholder="Enter your last name"
|
||||
autoComplete="family-name"
|
||||
onChange={handleChange}
|
||||
error={errors[idToName["edit-last-name"]] ? true : false}
|
||||
/>
|
||||
{errors[idToName["edit-last-name"]] ? (
|
||||
<Typography variant="h5" component="p" className="input-error">
|
||||
{errors[idToName["edit-last-name"]]}
|
||||
</Typography>
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
</Stack>
|
||||
</div>
|
||||
<div className="edit-profile-form__wrapper">
|
||||
<Stack>
|
||||
<Typography variant="h4" component="h1">
|
||||
Email
|
||||
</Typography>
|
||||
<Typography variant="h5" component="p">
|
||||
This is your current email address — it cannot be changed.
|
||||
</Typography>
|
||||
</Stack>
|
||||
<Stack>
|
||||
<EmailTextField
|
||||
id="edit-email"
|
||||
label={null}
|
||||
value={user.email}
|
||||
placeholder="Enter your email"
|
||||
autoComplete="email"
|
||||
// onChange={handleChange}
|
||||
// error={errors[idToName["edit-email"]] ? true : false}
|
||||
disabled={true}
|
||||
/>
|
||||
{errors[idToName["edit-email"]] ? (
|
||||
<Typography variant="h5" component="p" className="input-error">
|
||||
{errors[idToName["edit-email"]]}
|
||||
</Typography>
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
</Stack>
|
||||
</div>
|
||||
<div className="edit-profile-form__wrapper">
|
||||
<Stack>
|
||||
<Typography variant="h4" component="h1">
|
||||
Your photo
|
||||
</Typography>
|
||||
<Typography variant="h5" component="p">
|
||||
This photo will be displayed in your profile page.
|
||||
</Typography>
|
||||
</Stack>
|
||||
<Stack className="row-stack" direction="row" alignItems="center">
|
||||
<Avatar
|
||||
src={picture ? picture.src : "/static/images/avatar/2.jpg"}
|
||||
firstName={user?.firstname}
|
||||
lastName={user?.lastname}
|
||||
sx={{
|
||||
width: "64px",
|
||||
height: "64px",
|
||||
border: "none",
|
||||
mr: "8px",
|
||||
}}
|
||||
/>
|
||||
<ButtonSpinner
|
||||
level="tertiary"
|
||||
label="Delete"
|
||||
onClick={handleDeletePicture}
|
||||
isLoading={loading}
|
||||
/>
|
||||
<Button
|
||||
level="tertiary"
|
||||
label="Update"
|
||||
onClick={() => setIsOpen("picture")}
|
||||
sx={{
|
||||
color: theme.palette.primary.main,
|
||||
}}
|
||||
/>
|
||||
</Stack>
|
||||
</div>
|
||||
<Divider
|
||||
aria-hidden="true"
|
||||
className="short-divider"
|
||||
width="0"
|
||||
sx={{ marginY: theme.spacing(1) }}
|
||||
/>
|
||||
<Stack direction="row" justifyContent="flex-end">
|
||||
<Box width="fit-content">
|
||||
<ButtonSpinner
|
||||
level="primary"
|
||||
label="Save"
|
||||
onClick={handleSaveProfile}
|
||||
isLoading={isLoading}
|
||||
loadingText="Saving..."
|
||||
disabled={Object.keys(errors).length !== 0 && true}
|
||||
sx={{
|
||||
paddingX: "40px",
|
||||
}}
|
||||
/>
|
||||
</Box>
|
||||
</Stack>
|
||||
</form>
|
||||
<Divider aria-hidden="true" />
|
||||
<form className="delete-profile-form" noValidate spellCheck="false">
|
||||
<div className="delete-profile-form__wrapper">
|
||||
<Stack direction="column" gap="15px">
|
||||
<Typography variant="h4" component="h1">
|
||||
Delete account
|
||||
</Typography>
|
||||
<Typography variant="h5" component="p">
|
||||
Note that deleting your account will remove all data from our
|
||||
system. This is permanent and non-recoverable.
|
||||
</Typography>
|
||||
<Box sx={{ mt: theme.spacing(1) }}>
|
||||
<Button
|
||||
level="error"
|
||||
label="Delete account"
|
||||
onClick={() => setIsOpen("delete")}
|
||||
/>
|
||||
</Box>
|
||||
</Stack>
|
||||
</div>
|
||||
</form>
|
||||
{/* TODO - Update ModalPopup Component with @mui for reusability */}
|
||||
<Modal
|
||||
aria-labelledby="modal-delete-account"
|
||||
aria-describedby="delete-account-confirmation"
|
||||
open={isModalOpen("delete")}
|
||||
onClose={() => setIsOpen("")}
|
||||
disablePortal
|
||||
>
|
||||
<Stack
|
||||
gap="10px"
|
||||
sx={{
|
||||
position: "absolute",
|
||||
top: "50%",
|
||||
left: "50%",
|
||||
transform: "translate(-50%, -50%)",
|
||||
width: 400,
|
||||
bgcolor: "white",
|
||||
border: "solid 1px #f2f2f2",
|
||||
borderRadius: `${theme.shape.borderRadius}px`,
|
||||
boxShadow: 24,
|
||||
p: "30px",
|
||||
"&:focus": {
|
||||
outline: "none",
|
||||
},
|
||||
}}
|
||||
>
|
||||
<Typography id="modal-delete-account" variant="h4" component="h1">
|
||||
Really delete this account?
|
||||
</Typography>
|
||||
<Typography
|
||||
id="delete-account-confirmation"
|
||||
variant="h5"
|
||||
component="p"
|
||||
>
|
||||
If you delete your account, you will no longer be able to sign in,
|
||||
and all of your data will be deleted. Deleting your account is
|
||||
permanent and non-recoverable action.
|
||||
</Typography>
|
||||
<Stack direction="row" gap="10px" mt="10px" justifyContent="flex-end">
|
||||
<Button
|
||||
level="tertiary"
|
||||
label="Cancel"
|
||||
onClick={() => setIsOpen("")}
|
||||
/>
|
||||
<ButtonSpinner
|
||||
level="error"
|
||||
label="Delete account"
|
||||
onClick={handleDeleteAccount}
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
</Stack>
|
||||
</Stack>
|
||||
</Modal>
|
||||
<Modal
|
||||
aria-labelledby="modal-update-picture"
|
||||
aria-describedby="update-profile-picture"
|
||||
open={isModalOpen("picture")}
|
||||
onClose={handleClosePictureModal}
|
||||
disablePortal
|
||||
>
|
||||
<Stack
|
||||
sx={{
|
||||
position: "absolute",
|
||||
top: "50%",
|
||||
left: "50%",
|
||||
transform: "translate(-50%, -50%)",
|
||||
width: 400,
|
||||
bgcolor: "white",
|
||||
border: "solid 1px #f2f2f2",
|
||||
borderRadius: `${theme.shape.borderRadius}px`,
|
||||
boxShadow: 24,
|
||||
p: "30px",
|
||||
"&:focus": {
|
||||
outline: "none",
|
||||
},
|
||||
}}
|
||||
>
|
||||
<Typography id="modal-update-picture" variant="h4" component="h1">
|
||||
Upload Image
|
||||
</Typography>
|
||||
<ImageField
|
||||
id="update-profile-picture"
|
||||
picture={picture?.src}
|
||||
onChange={handlePicture}
|
||||
/>
|
||||
{isUploading ? (
|
||||
<ProgressUpload
|
||||
icon={<ImageIcon />}
|
||||
label={picture?.name}
|
||||
size={picture?.size}
|
||||
progress={uploadProgress}
|
||||
onClick={handleCancelUpload}
|
||||
/>
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
<Stack direction="row" mt="20px" gap="10px" justifyContent="flex-end">
|
||||
<Button
|
||||
level="secondary"
|
||||
label="Edit"
|
||||
disabled
|
||||
sx={{ mr: "auto" }}
|
||||
/>
|
||||
<ButtonSpinner
|
||||
level="tertiary"
|
||||
label="Remove"
|
||||
onClick={handleCancelUpload}
|
||||
isLoading={loading}
|
||||
/>
|
||||
<ButtonSpinner
|
||||
level="primary"
|
||||
label="Update"
|
||||
onClick={handleUpdatePicture}
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
</Stack>
|
||||
</Stack>
|
||||
</Modal>
|
||||
</TabPanel>
|
||||
);
|
||||
};
|
||||
|
||||
ProfilePanel.propTypes = {
|
||||
// No props are being passed to this component, hence no specific PropTypes are defined.
|
||||
};
|
||||
|
||||
export default ProfilePanel;
|
||||
@@ -0,0 +1,532 @@
|
||||
import { useTheme } from "@emotion/react";
|
||||
import TabPanel from "@mui/lab/TabPanel";
|
||||
import {
|
||||
Box,
|
||||
Checkbox,
|
||||
Container,
|
||||
Divider,
|
||||
MenuItem,
|
||||
Modal,
|
||||
Select,
|
||||
Stack,
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableRow,
|
||||
TextField,
|
||||
Typography,
|
||||
} from "@mui/material";
|
||||
import ButtonSpinner from "../../ButtonSpinner";
|
||||
import Button from "../../Button";
|
||||
import { useState } from "react";
|
||||
|
||||
/**
|
||||
* TeamPanel component manages the organization and team members,
|
||||
* providing functionalities like renaming the organization, managing team members,
|
||||
* and inviting new members.
|
||||
*
|
||||
* @returns {JSX.Element}
|
||||
*/
|
||||
|
||||
const teamColumns = [
|
||||
{
|
||||
id: "checkbox",
|
||||
label: "",
|
||||
sx: { minWidth: "20px", width: "40px" },
|
||||
},
|
||||
{
|
||||
id: "name",
|
||||
label: "NAME",
|
||||
sx: { fontSize: "12px" },
|
||||
},
|
||||
{ id: "email", label: "EMAIL", sx: { fontSize: "12px" } },
|
||||
{ id: "role", label: "ROLE", sx: { fontSize: "12px" } },
|
||||
];
|
||||
//for testing, will be removed later
|
||||
const teamConfig = [
|
||||
{
|
||||
id: 0,
|
||||
isChecked: false,
|
||||
name: "John Connor",
|
||||
email: "john@domain.com",
|
||||
type: "admin",
|
||||
role: "Administrator",
|
||||
createdAt: "10/4/2022",
|
||||
},
|
||||
{
|
||||
id: 1,
|
||||
isChecked: false,
|
||||
name: "Adam McFadden",
|
||||
email: "adam@domain.com",
|
||||
type: "member",
|
||||
role: "Member",
|
||||
createdAt: "10/4/2022",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
isChecked: false,
|
||||
name: "Cris Cross",
|
||||
email: "cris@domain.com",
|
||||
type: "member",
|
||||
role: "Member",
|
||||
createdAt: "10/4/2022",
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
isChecked: false,
|
||||
name: "Prince",
|
||||
email: "prince@domain.com",
|
||||
type: "member",
|
||||
role: "Member",
|
||||
createdAt: "10/4/2022",
|
||||
},
|
||||
];
|
||||
const actionsConfig = [
|
||||
{
|
||||
value: "bulk",
|
||||
label: "Bulk actions",
|
||||
},
|
||||
];
|
||||
const roleConfig = [
|
||||
{
|
||||
value: "role",
|
||||
label: "Change role to",
|
||||
},
|
||||
];
|
||||
|
||||
const TeamPanel = () => {
|
||||
const theme = useTheme();
|
||||
//TODO - use redux loading state
|
||||
//!! - currently all loading buttons are tied to the same state
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [orgStates, setOrgStates] = useState({
|
||||
name: "Bluewave Labs",
|
||||
isLoading: false,
|
||||
isOpen: false,
|
||||
newName: "",
|
||||
});
|
||||
const toggleOrgModal = (state) => {
|
||||
setOrgStates((prev) => ({
|
||||
...prev,
|
||||
isOpen: state,
|
||||
}));
|
||||
};
|
||||
const toggleOrgLoading = (state) => {
|
||||
setOrgStates((prev) => ({
|
||||
...prev,
|
||||
isLoading: state,
|
||||
}));
|
||||
};
|
||||
const handleRenameOrg = () => {
|
||||
toggleOrgLoading(true);
|
||||
|
||||
setTimeout(() => {
|
||||
setOrgStates((prev) => ({
|
||||
...prev,
|
||||
name: prev.newName !== "" ? prev.newName : prev.name,
|
||||
isLoading: false,
|
||||
isOpen: false,
|
||||
newName: "",
|
||||
}));
|
||||
}, 2000);
|
||||
};
|
||||
|
||||
const [teamStates, setTeamStates] = useState({
|
||||
members: teamConfig,
|
||||
filter: "",
|
||||
});
|
||||
const handleCheckCell = (id) => {
|
||||
const updatedTeamStates = [...teamStates.members];
|
||||
updatedTeamStates[id] = {
|
||||
...updatedTeamStates[id],
|
||||
isChecked: !updatedTeamStates[id].isChecked,
|
||||
};
|
||||
setTeamStates((prev) => ({
|
||||
...prev,
|
||||
members: updatedTeamStates,
|
||||
}));
|
||||
};
|
||||
const handleFilter = (filter) => {
|
||||
setTeamStates((prev) => ({
|
||||
...prev,
|
||||
filter: filter,
|
||||
}));
|
||||
};
|
||||
//TODO - implement select action function
|
||||
const handleSelectActionType = () => {
|
||||
setIsLoading(true);
|
||||
setTimeout(() => {
|
||||
setIsLoading(false);
|
||||
}, 2000);
|
||||
};
|
||||
//TODO - implement select role function
|
||||
const handleSelectRoleType = () => {
|
||||
setIsLoading(true);
|
||||
setTimeout(() => {
|
||||
setIsLoading(false);
|
||||
}, 2000);
|
||||
};
|
||||
//TODO - implement save team function
|
||||
const handleSaveTeam = () => {
|
||||
setIsLoading(true);
|
||||
setTimeout(() => {
|
||||
setIsLoading(false);
|
||||
}, 2000);
|
||||
};
|
||||
const [toggleInviteModal, setToggleInviteModal] = useState(false);
|
||||
const handleInviteMember = () => {};
|
||||
const handleMembersQuery = (type) => {
|
||||
let count = 0;
|
||||
teamStates.members.forEach((member) => {
|
||||
type === "" ? count++ : member.type === type ? count++ : "";
|
||||
});
|
||||
return count;
|
||||
};
|
||||
return (
|
||||
<TabPanel value="team">
|
||||
<form className="edit-organization-form">
|
||||
<div
|
||||
className="edit-organization-form__wrapper"
|
||||
>
|
||||
<Stack>
|
||||
<Typography variant="h4" component="h1">
|
||||
Organization name
|
||||
</Typography>
|
||||
</Stack>
|
||||
<Stack
|
||||
id="org-name-flex-container"
|
||||
className="row-stack"
|
||||
direction="row"
|
||||
justifyContent="flex-end"
|
||||
gap="8px"
|
||||
>
|
||||
<ButtonSpinner
|
||||
level="tertiary"
|
||||
label={!orgStates.isLoading ? orgStates.name : ""}
|
||||
disabled
|
||||
onClick={() => toggleOrgModal(true)}
|
||||
isLoading={orgStates.isLoading}
|
||||
/>
|
||||
<Button
|
||||
level="primary"
|
||||
label="Rename"
|
||||
sx={{ paddingX: "30px" }}
|
||||
onClick={() => toggleOrgModal(true)}
|
||||
/>
|
||||
</Stack>
|
||||
</div>
|
||||
<Divider
|
||||
aria-hidden="true"
|
||||
className="short-divider"
|
||||
sx={{ marginY: theme.spacing(4) }}
|
||||
/>
|
||||
</form>
|
||||
<Modal
|
||||
aria-labelledby="modal-edit-org-name"
|
||||
aria-describedby="edit-organization-name"
|
||||
open={orgStates.isOpen}
|
||||
onClose={() => toggleOrgModal(false)}
|
||||
disablePortal
|
||||
>
|
||||
<Stack
|
||||
gap="20px"
|
||||
sx={{
|
||||
position: "absolute",
|
||||
top: "50%",
|
||||
left: "50%",
|
||||
transform: "translate(-50%, -50%)",
|
||||
width: 400,
|
||||
bgcolor: "white",
|
||||
border: "solid 1px #f2f2f2",
|
||||
borderRadius: `${theme.shape.borderRadius}px`,
|
||||
boxShadow: 24,
|
||||
p: "30px",
|
||||
"&:focus": {
|
||||
outline: "none",
|
||||
},
|
||||
}}
|
||||
>
|
||||
<Typography id="modal-edit-org-name" variant="h4" component="h1">
|
||||
Rename this organization?
|
||||
</Typography>
|
||||
<TextField
|
||||
id="edit-organization-name"
|
||||
placeholder={orgStates.name}
|
||||
spellCheck="false"
|
||||
value={orgStates.newName}
|
||||
onChange={(event) =>
|
||||
setOrgStates((prev) => ({
|
||||
...prev,
|
||||
newName: event.target.value,
|
||||
}))
|
||||
}
|
||||
></TextField>
|
||||
<Stack direction="row" gap="10px" mt="10px" justifyContent="flex-end">
|
||||
<Button
|
||||
level="tertiary"
|
||||
label="Cancel"
|
||||
onClick={() => toggleOrgModal(false)}
|
||||
/>
|
||||
<ButtonSpinner
|
||||
level="primary"
|
||||
label="Rename"
|
||||
onClick={handleRenameOrg}
|
||||
isLoading={orgStates.isLoading}
|
||||
sx={{ paddingX: "30px" }}
|
||||
/>
|
||||
</Stack>
|
||||
</Stack>
|
||||
</Modal>
|
||||
<form className="edit-team-form" noValidate spellCheck="false">
|
||||
<div className="edit-team-form__wrapper">
|
||||
<Typography variant="h4" component="h1">
|
||||
Team members
|
||||
</Typography>
|
||||
</div>
|
||||
<div className="edit-team-form__wrapper compact">
|
||||
<Stack
|
||||
direction="row"
|
||||
gap="20px"
|
||||
alignItems="center"
|
||||
sx={{ fontSize: "14px" }}
|
||||
>
|
||||
<Box onClick={() => handleFilter("")}>
|
||||
All
|
||||
<span className="members-query">
|
||||
<span>{handleMembersQuery("")}</span>
|
||||
</span>
|
||||
</Box>
|
||||
<Box onClick={() => handleFilter("admin")}>
|
||||
Administrator
|
||||
<span className="members-query">
|
||||
<span>{handleMembersQuery("admin")}</span>
|
||||
</span>
|
||||
</Box>
|
||||
<Box onClick={() => handleFilter("member")}>
|
||||
Member
|
||||
<span className="members-query">
|
||||
<span>{handleMembersQuery("member")}</span>
|
||||
</span>
|
||||
</Box>
|
||||
</Stack>
|
||||
<Button
|
||||
level="primary"
|
||||
label="Invite a team member"
|
||||
sx={{ paddingX: "30px" }}
|
||||
onClick={() => setToggleInviteModal(true)}
|
||||
/>
|
||||
</div>
|
||||
<div className="edit-team-form__wrapper compact">
|
||||
<Container
|
||||
disableGutters
|
||||
sx={{
|
||||
border: `1px solid ${theme.palette.section.borderColor}`,
|
||||
borderRadius: `${theme.shape.borderRadius}px`,
|
||||
borderBottom: "none",
|
||||
}}
|
||||
>
|
||||
<Stack direction="row" gap="40px" p="20px">
|
||||
<Stack
|
||||
direction="row"
|
||||
gap="10px"
|
||||
alignItems="center"
|
||||
className="table-stack"
|
||||
>
|
||||
<Select
|
||||
id="select-actions"
|
||||
value="bulk"
|
||||
inputProps={{ id: "select-actions-input" }}
|
||||
>
|
||||
{actionsConfig.map((action) => (
|
||||
<MenuItem
|
||||
value={action.value}
|
||||
key={action.value}
|
||||
sx={{ fontSize: "13px" }}
|
||||
>
|
||||
{action.label}
|
||||
</MenuItem>
|
||||
))}
|
||||
</Select>
|
||||
<ButtonSpinner
|
||||
level="secondary"
|
||||
label="Apply"
|
||||
onClick={handleSelectActionType}
|
||||
isLoading={isLoading}
|
||||
sx={{
|
||||
bgcolor: "#fafafa",
|
||||
}}
|
||||
/>
|
||||
</Stack>
|
||||
<Stack direction="row" gap="10px" alignItems="center">
|
||||
<Select
|
||||
id="select-role"
|
||||
value="role"
|
||||
inputProps={{ id: "select-role-input" }}
|
||||
>
|
||||
{roleConfig.map((role) => (
|
||||
<MenuItem
|
||||
value={role.value}
|
||||
key={role.value}
|
||||
sx={{ fontSize: "13px" }}
|
||||
>
|
||||
{role.label}
|
||||
</MenuItem>
|
||||
))}
|
||||
</Select>
|
||||
<ButtonSpinner
|
||||
level="secondary"
|
||||
label="Apply"
|
||||
onClick={handleSelectRoleType}
|
||||
isLoading={isLoading}
|
||||
sx={{
|
||||
bgcolor: "#fafafa",
|
||||
}}
|
||||
/>
|
||||
</Stack>
|
||||
</Stack>
|
||||
<Table
|
||||
sx={{
|
||||
borderTop: `1px solid ${theme.palette.section.borderColor}`,
|
||||
tableLayout: "fixed",
|
||||
}}
|
||||
>
|
||||
<TableHead>
|
||||
<TableRow
|
||||
sx={{
|
||||
bgcolor: "#fafafa",
|
||||
}}
|
||||
>
|
||||
{teamColumns.map((cell) => (
|
||||
<TableCell
|
||||
key={cell.id}
|
||||
sx={{
|
||||
...cell.sx,
|
||||
color: theme.palette.otherColors.slateGray,
|
||||
}}
|
||||
>
|
||||
{cell.label}
|
||||
</TableCell>
|
||||
))}
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{teamStates.members.map((cell) =>
|
||||
teamStates.filter === "" ||
|
||||
teamStates.filter === cell.type ? (
|
||||
<TableRow key={cell.id}>
|
||||
<TableCell align="center">
|
||||
<Checkbox
|
||||
id={`${cell.id}-${cell.name}`}
|
||||
checked={cell.isChecked}
|
||||
onChange={() => handleCheckCell(cell.id)}
|
||||
inputProps={{ "aria-label": "controlled" }}
|
||||
/>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Stack direction="column">
|
||||
<Box
|
||||
sx={{
|
||||
color: theme.palette.otherColors.blackish,
|
||||
verticalAlign: "top",
|
||||
}}
|
||||
>
|
||||
{cell.name}
|
||||
</Box>
|
||||
<Box>Created at {cell.createdAt}</Box>
|
||||
</Stack>
|
||||
</TableCell>
|
||||
<TableCell>{cell.email}</TableCell>
|
||||
<TableCell>{cell.role}</TableCell>
|
||||
</TableRow>
|
||||
) : (
|
||||
""
|
||||
)
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</Container>
|
||||
</div>
|
||||
<Divider aria-hidden="true" width="0" />
|
||||
<Stack direction="row" justifyContent="flex-end">
|
||||
<Box width="fit-content">
|
||||
<ButtonSpinner
|
||||
level="primary"
|
||||
label="Save"
|
||||
onClick={handleSaveTeam}
|
||||
isLoading={isLoading}
|
||||
loadingText="Saving..."
|
||||
sx={{
|
||||
paddingX: "40px",
|
||||
}}
|
||||
/>
|
||||
</Box>
|
||||
</Stack>
|
||||
</form>
|
||||
<Modal
|
||||
aria-labelledby="modal-invite-member"
|
||||
aria-describedby="invite-member-to-team"
|
||||
open={toggleInviteModal}
|
||||
onClose={() => setToggleInviteModal(false)}
|
||||
disablePortal
|
||||
>
|
||||
<Stack
|
||||
gap="10px"
|
||||
sx={{
|
||||
position: "absolute",
|
||||
top: "50%",
|
||||
left: "50%",
|
||||
transform: "translate(-50%, -50%)",
|
||||
width: 400,
|
||||
bgcolor: "white",
|
||||
border: "solid 1px #f2f2f2",
|
||||
borderRadius: `${theme.shape.borderRadius}px`,
|
||||
boxShadow: 24,
|
||||
p: "30px",
|
||||
"&:focus": {
|
||||
outline: "none",
|
||||
},
|
||||
}}
|
||||
>
|
||||
<Typography id="modal-invite-member" variant="h4" component="h1">
|
||||
Invite new team member
|
||||
</Typography>
|
||||
<Typography id="invite-member-to-team" variant="h5" component="p">
|
||||
When you add a new team member, they will get access to all
|
||||
monitors.
|
||||
</Typography>
|
||||
<TextField
|
||||
id="input-team-member"
|
||||
spellCheck="false"
|
||||
// value={orgStates.newName}
|
||||
// onChange={(event) =>
|
||||
// setOrgStates((prev) => ({
|
||||
// ...prev,
|
||||
// newName: event.target.value,
|
||||
// }))
|
||||
// }
|
||||
></TextField>
|
||||
<Stack direction="row" gap="10px" mt="10px" justifyContent="flex-end">
|
||||
<Button
|
||||
level="tertiary"
|
||||
label="Cancel"
|
||||
onClick={() => setToggleInviteModal(false)}
|
||||
/>
|
||||
<ButtonSpinner
|
||||
level="primary"
|
||||
label="Send invite"
|
||||
onClick={handleInviteMember}
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
</Stack>
|
||||
</Stack>
|
||||
</Modal>
|
||||
</TabPanel>
|
||||
);
|
||||
};
|
||||
|
||||
TeamPanel.propTypes = {
|
||||
// No props are being passed to this component, hence no specific PropTypes are defined.
|
||||
};
|
||||
|
||||
export default TeamPanel;
|
||||
Reference in new issue
Block a user