Skip to content

Commit

Permalink
update(OH2-443): Refactor user / group reset/cancel operations
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveGT96 committed Nov 29, 2024
1 parent 4733480 commit 47c5618
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 47 deletions.
78 changes: 62 additions & 16 deletions src/components/accessories/admin/users/editGroup/EditGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import TextField from "../../../textField/TextField";
import { PATHS } from "../../../../../consts";
import { usePermission } from "../../../../../libraries/permissionUtils/usePermission";

import { Cancel } from "@mui/icons-material";
import { CircularProgress } from "@mui/material";
import CheckboxField from "components/accessories/checkboxField/CheckboxField";
import { PermissionDTO } from "generated/models/PermissionDTO";
Expand Down Expand Up @@ -45,6 +46,8 @@ export const EditGroup = () => {

const [openResetConfirmation, setOpenResetConfirmation] = useState(false);

const [openCancelConfirmation, setOpenCancelConfirmation] = useState(false);

const update = useAppSelector((state) => state.usergroups.update);
const permissions = useAppSelector((state) => state.permissions.getAll);
const group = useAppSelector((state) => state.usergroups.currentGroup);
Expand Down Expand Up @@ -133,10 +136,29 @@ export const EditGroup = () => {
};
}, []);

const handleResetConfirmation = () => {
const handleResetConfirmationDialog = useCallback(
(value: boolean) => () => {
setOpenResetConfirmation(value);
},
[setOpenResetConfirmation]
);

const handleCancelConfirmationDialog = useCallback(
(value: boolean) => () => {
setOpenCancelConfirmation(value);
},
[setOpenCancelConfirmation]
);

const handleResetConfirmation = useCallback(() => {
setOpenResetConfirmation(false);
resetForm();
}, [navigate, resetForm, setOpenResetConfirmation]);

const handleCancelConfirmation = useCallback(() => {
setOpenCancelConfirmation(false);
navigate(-1);
};
}, [navigate, setOpenCancelConfirmation]);

const handleCheckboxChange = useCallback(
(fieldName: string) => (value: boolean) => {
Expand Down Expand Up @@ -168,10 +190,24 @@ export const EditGroup = () => {
permissions.isLoading ? (
<CircularProgress style={{ marginLeft: "50%", position: "relative" }} />
) : (
<div className="newGroupForm">
<form className="newGroupForm__form" onSubmit={handleSubmit}>
<div className="editGroupForm">
<div className="editGroupForm__header">
<div className="editGroupForm__actions">
<Button
dataCy="cancel-form"
onClick={handleCancelConfirmationDialog(true)}
type="button"
variant="contained"
color="primary"
>
<Cancel fontSize="small" />
{t("common.discard")}
</Button>
</div>
</div>
<form className="editGroupForm__form" onSubmit={handleSubmit}>
<div className="row start-sm center-xs">
<div className="newGroupForm__item fullWidth">
<div className="editGroupForm__item fullWidth">
<TextField
field={getFieldProps("code")}
theme="regular"
Expand All @@ -183,7 +219,7 @@ export const EditGroup = () => {
disabled
/>
</div>
<div className="newGroupForm__item fullWidth">
<div className="editGroupForm__item fullWidth">
<TextField
field={getFieldProps("desc")}
theme="regular"
Expand All @@ -194,7 +230,7 @@ export const EditGroup = () => {
/>
</div>
</div>
<div className="newGroupForm__item fullWidth">
<div className="editGroupForm__item fullWidth">
<CheckboxField
fieldName={"deleted"}
checked={!!values.deleted}
Expand All @@ -212,7 +248,7 @@ export const EditGroup = () => {
/>
)}

<div className="newGroupForm__item fullWidth">
<div className="editGroupForm__item fullWidth">
{isPermissionEditorAvailable &&
updatedPermissionsStack.length > 0 && (
<p>
Expand Down Expand Up @@ -249,7 +285,7 @@ export const EditGroup = () => {
)}
</div>

<div className="newGroupForm__buttonSet">
<div className="editGroupForm__buttonSet">
<div className="submit_button">
<Button
type="submit"
Expand All @@ -265,26 +301,36 @@ export const EditGroup = () => {
</div>
<div className="reset_button">
<Button
dataCy="cancel-form"
dataCy="reset-form"
type="reset"
variant="text"
disabled={update.isLoading}
onClick={() => setOpenResetConfirmation(true)}
disabled={update.isLoading || !dirty}
onClick={handleResetConfirmationDialog(true)}
>
{t("common.cancel")}
{t("common.reset")}
</Button>
</div>
</div>
</form>
<ConfirmationDialog
isOpen={openResetConfirmation}
isOpen={openCancelConfirmation}
title={t("common.cancel")}
info={t("common.resetform")}
info={t("common.cancelMessage")}
icon={warningIcon}
primaryButtonLabel={t("common.ok")}
secondaryButtonLabel={t("common.discard")}
handlePrimaryButtonClick={handleCancelConfirmation}
handleSecondaryButtonClick={handleCancelConfirmationDialog(false)}
/>
<ConfirmationDialog
isOpen={openResetConfirmation}
title={t("common.reset")}
info={t("common.resetform")}
icon={warningIcon}
primaryButtonLabel={t("common.ok")}
secondaryButtonLabel={t("common.back")}
handlePrimaryButtonClick={handleResetConfirmation}
handleSecondaryButtonClick={() => setOpenResetConfirmation(false)}
handleSecondaryButtonClick={handleResetConfirmationDialog(false)}
/>
<ConfirmationDialog
isOpen={update.hasSucceeded}
Expand Down
33 changes: 30 additions & 3 deletions src/components/accessories/admin/users/editGroup/styles.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@import "../../../../../styles/variables";
@import "../../../../../../node_modules/susy/sass/susy";

.newGroupForm {
.editGroupForm {
display: inline-block;
flex-direction: column;
align-items: center;
Expand All @@ -15,7 +15,7 @@
justify-content: space-between;
}

.newGroupForm__item {
.editGroupForm__item {
margin: 7px 0px;
padding: 0px 15px;
width: 50%;
Expand Down Expand Up @@ -57,7 +57,34 @@
}
}

.newGroupForm__buttonSet {
.editGroupForm__header {
display: flex;
align-items: center;
flex-direction: row;
justify-content: space-between;
flex-wrap: wrap;
margin-bottom: 25px;
.editGroupForm__actions {
@include susy-media($smartphone_small) {
display: block;
}
& {
.MuiButton-label {
font-size: smaller;
letter-spacing: 1px;
font-weight: 600;
}
button {
@include susy-media($smartphone_small) {
width: 100%;
margin-top: 0px;
}
}
}
}
}

.editGroupForm__buttonSet {
display: flex;
margin-top: 25px;
padding: 0px 15px;
Expand Down
65 changes: 56 additions & 9 deletions src/components/accessories/admin/users/editUser/EditUserForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ConfirmationDialog from "../../../confirmationDialog/ConfirmationDialog";
import InfoBox from "../../../infoBox/InfoBox";
import TextField from "../../../textField/TextField";

import { Cancel } from "@mui/icons-material";
import CheckboxField from "components/accessories/checkboxField/CheckboxField";
import { PATHS } from "../../../../../consts";
import "./styles.scss";
Expand Down Expand Up @@ -47,6 +48,8 @@ export const EditUserForm = ({

const [openResetConfirmation, setOpenResetConfirmation] = useState(false);

const [openCancelConfirmation, setOpenCancelConfirmation] = useState(false);

const handleFormSubmit = (values: UserDTO & { passwd2: string }) => {
const { passwd2, ...userDTO } = values;
if (userDTO.passwd === undefined) {
Expand All @@ -65,6 +68,7 @@ export const EditUserForm = ({
errors,
touched,
values,
resetForm,
setFieldTouched,
setFieldValue,
} = useFormik<UserDTO & { passwd2: string }>({
Expand All @@ -73,10 +77,29 @@ export const EditUserForm = ({
onSubmit: handleFormSubmit,
});

const handleResetConfirmation = () => {
const handleResetConfirmationDialog = useCallback(
(value: boolean) => () => {
setOpenResetConfirmation(value);
},
[setOpenResetConfirmation]
);

const handleCancelConfirmationDialog = useCallback(
(value: boolean) => () => {
setOpenCancelConfirmation(value);
},
[setOpenCancelConfirmation]
);

const handleResetConfirmation = useCallback(() => {
setOpenResetConfirmation(false);
resetForm();
}, [navigate, resetForm, setOpenResetConfirmation]);

const handleCancelConfirmation = useCallback(() => {
setOpenCancelConfirmation(false);
navigate(-1);
};
}, [navigate, setOpenCancelConfirmation]);

const handleCheckboxChange = useCallback(
(fieldName: string) => (value: boolean) => {
Expand All @@ -87,6 +110,20 @@ export const EditUserForm = ({

return (
<div className="editUserForm">
<div className="editUserForm__header">
<div className="editUserForm__actions">
<Button
dataCy="cancel-form"
onClick={handleCancelConfirmationDialog(true)}
type="button"
variant="contained"
color="primary"
>
<Cancel fontSize="small" />
{t("common.discard")}
</Button>
</div>
</div>
<form className="editUserForm__form" onSubmit={handleSubmit}>
<div className="row start-sm center-xs">
<div className="editUserForm__item fullWidth">
Expand Down Expand Up @@ -199,26 +236,36 @@ export const EditUserForm = ({
</div>
<div className="reset_button">
<Button
dataCy="cancel-form"
dataCy="reset-form"
type="reset"
variant="text"
disabled={isLoading}
onClick={() => setOpenResetConfirmation(true)}
disabled={isLoading || !dirty}
onClick={handleResetConfirmationDialog(true)}
>
{t("common.cancel")}
{t("common.reset")}
</Button>
</div>
</div>
</form>
<ConfirmationDialog
isOpen={openResetConfirmation}
isOpen={openCancelConfirmation}
title={t("common.cancel")}
info={t("common.resetform")}
info={t("common.cancelMessage")}
icon={warningIcon}
primaryButtonLabel={t("common.ok")}
secondaryButtonLabel={t("common.discard")}
handlePrimaryButtonClick={handleCancelConfirmation}
handleSecondaryButtonClick={handleCancelConfirmationDialog(false)}
/>
<ConfirmationDialog
isOpen={openResetConfirmation}
title={t("common.reset")}
info={t("common.resetform")}
icon={warningIcon}
primaryButtonLabel={t("common.ok")}
secondaryButtonLabel={t("common.back")}
handlePrimaryButtonClick={handleResetConfirmation}
handleSecondaryButtonClick={() => setOpenResetConfirmation(false)}
handleSecondaryButtonClick={handleResetConfirmationDialog(false)}
/>
<ConfirmationDialog
isOpen={hasSucceeded}
Expand Down
27 changes: 27 additions & 0 deletions src/components/accessories/admin/users/editUser/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@
}
}

.editUserForm__header {
display: flex;
align-items: center;
flex-direction: row;
justify-content: space-between;
flex-wrap: wrap;
margin-bottom: 25px;
.editUserForm__actions {
@include susy-media($smartphone_small) {
display: block;
}
& {
.MuiButton-label {
font-size: smaller;
letter-spacing: 1px;
font-weight: 600;
}
button {
@include susy-media($smartphone_small) {
width: 100%;
margin-top: 0px;
}
}
}
}
}

.editUserForm__buttonSet {
display: flex;
margin-top: 25px;
Expand Down
Loading

0 comments on commit 47c5618

Please sign in to comment.