Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Policy selector fix #2123

Merged
merged 3 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions portal-ui/src/screens/Console/Policies/PolicySelectors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ import api from "../../../common/api";
import TableWrapper from "../Common/TableWrapper/TableWrapper";
import SearchBox from "../Common/SearchBox";
import { setModalErrorSnackMessage } from "../../../systemSlice";
import { useAppDispatch } from "../../../store";
import { AppState, useAppDispatch } from "../../../store";
import { setSelectedPolicies } from "../Users/AddUsersSlice";
import { useSelector } from "react-redux";

interface ISelectPolicyProps {
classes: any;
Expand Down Expand Up @@ -84,9 +85,11 @@ const PolicySelectors = ({
const [loading, isLoading] = useState<boolean>(false);
const [filter, setFilter] = useState<string>("");

const currentPolicies = useSelector((state: AppState) => state.createUser.selectedPolicies);

const fetchPolicies = useCallback(() => {
isLoading(true);

api
.invoke("GET", `/api/v1/policies?limit=1000`)
.then((res: PolicyList) => {
Expand All @@ -112,11 +115,12 @@ const PolicySelectors = ({
}, [loading, fetchPolicies]);

const selectionChanged = (e: React.ChangeEvent<HTMLInputElement>) => {

const targetD = e.target;
const value = targetD.value;
const checked = targetD.checked;

let elements: string[] = [...selectedPolicy]; // We clone the checkedUsers array
let elements: string[] = [...currentPolicies]; // We clone the checkedUsers array

if (checked) {
// If the user has checked this field we need to push this to checkedUsersList
Expand All @@ -127,7 +131,7 @@ const PolicySelectors = ({
}
// remove empty values
elements = elements.filter((element) => element !== "");

dispatch(setSelectedPolicies(elements));
};

Expand Down Expand Up @@ -162,7 +166,7 @@ const PolicySelectors = ({
<TableWrapper
columns={[{ label: "Policy", elementKey: "name" }]}
onSelect={selectionChanged}
selectedItems={selectedPolicy}
selectedItems={currentPolicies}
isLoading={loading}
records={filteredRecords}
entityName="Policies"
Expand Down
10 changes: 7 additions & 3 deletions portal-ui/src/screens/Console/Policies/SetPolicy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ import PolicySelectors from "./PolicySelectors";
import PredefinedList from "../Common/FormComponents/PredefinedList/PredefinedList";
import { encodeURLString } from "../../../common/utils";
import { setModalErrorSnackMessage } from "../../../systemSlice";
import { useAppDispatch } from "../../../store";
import { AppState, useAppDispatch } from "../../../store";

import { useSelector } from "react-redux";

interface ISetPolicyProps {
classes: any;
Expand All @@ -46,6 +48,8 @@ interface ISetPolicyProps {
open: boolean;
}



const styles = (theme: Theme) =>
createStyles({
...modalBasic,
Expand All @@ -72,7 +76,7 @@ const SetPolicy = ({
const [loading, setLoading] = useState<boolean>(false);
const [actualPolicy, setActualPolicy] = useState<string[]>([]);
const [selectedPolicy, setSelectedPolicy] = useState<string[]>([]);

const currentPolicies = useSelector((state: AppState) => state.createUser.selectedPolicies);
const setPolicyAction = () => {
let users = null;
let groups = null;
Expand All @@ -88,7 +92,7 @@ const SetPolicy = ({

api
.invoke("PUT", `/api/v1/set-policy-multi`, {
name: selectedPolicy,
name: currentPolicies,
groups: groups,
users: users,
})
Expand Down
7 changes: 5 additions & 2 deletions portal-ui/src/screens/Console/Users/SetUserPolicies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import ModalWrapper from "../Common/ModalWrapper/ModalWrapper";
import api from "../../../common/api";
import PolicySelectors from "../Policies/PolicySelectors";
import { setModalErrorSnackMessage } from "../../../systemSlice";
import { useAppDispatch } from "../../../store";
import { AppState, useAppDispatch } from "../../../store";
import { useSelector } from "react-redux";

interface ISetUserPoliciesProps {
classes: any;
Expand Down Expand Up @@ -60,6 +61,8 @@ const SetUserPolicies = ({
const [actualPolicy, setActualPolicy] = useState<string[]>([]);
const [selectedPolicy, setSelectedPolicy] = useState<string[]>([]);

const statePolicies = useSelector((state: AppState) => state.createUser.selectedPolicies);

const SetUserPoliciesAction = () => {
let entity = "user";
let value = selectedUser;
Expand All @@ -68,7 +71,7 @@ const SetUserPolicies = ({

api
.invoke("PUT", `/api/v1/set-policy`, {
name: selectedPolicy,
name: statePolicies,
entityName: value,
entityType: entity,
})
Expand Down