= (props) => {
)}
- {getRolePrivilegeError ? (
+ {getRoleIsError ? (
'Error'
- ) : getRolePrivilegeLoading ? (
+ ) : getRoleIsLoading ? (
'loading..'
- ) : privileges ? (
+ ) : getRoleData?.data ? (
<>
- {getBadges(privileges)}
+ {getBadges(getRoleData?.data)}
= (props) => {
- {privileges?.[deletePrivilegeIndex] ? (
+ {getRoleData?.data?.[deletePrivilegeIndex] ? (
= (props) => {
centered
className={classes.modalStyle}>
- {getBadge(privileges[deletePrivilegeIndex], deletePrivilegeIndex, false)}
+ {getBadge(getRoleData?.data[deletePrivilegeIndex], deletePrivilegeIndex, false)}
= (props) => {
onSearchChange={(value) => setStreamSearchValue(value)}
onDropdownClose={() => setStreamSearchValue(SelectedStream)}
onDropdownOpen={() => setStreamSearchValue('')}
- data={streams?.map((stream) => ({ value: stream.name, label: stream.name })) ?? []}
+ data={getLogStreamListData?.data?.map((stream) => ({ value: stream.name, label: stream.name })) ?? []}
searchable
label="Select a stream to assign"
required
diff --git a/src/pages/AccessManagement/RoleTR.tsx b/src/pages/AccessManagement/RoleTR.tsx
index 3ed6e6fc..55c3365b 100644
--- a/src/pages/AccessManagement/RoleTR.tsx
+++ b/src/pages/AccessManagement/RoleTR.tsx
@@ -1,5 +1,3 @@
-import { useGetUserRole } from '@/hooks/useGetUserRoles';
-import { usePutUserRole } from '@/hooks/usePutUserRole';
import {
ActionIcon,
Badge,
@@ -20,20 +18,37 @@ import { IconPlus, IconTransform, IconTrash, IconX } from '@tabler/icons-react';
import { FC, useEffect, useState } from 'react';
import { useUsersStyles } from './styles';
import { Prism } from '@mantine/prism';
-import { useDeleteUser } from '@/hooks/useDeleteUser';
-import { usePostUserResetPassword } from '@/hooks/usePostResetPassword';
-import { useGetRoles } from '@/hooks/useGetRoles';
+import { AxiosResponse } from 'axios';
+import { useUser } from '@/hooks/useUser';
+import { useRole } from '@/hooks/useRole';
interface RoleTRProps {
user: {
id: string;
method: string;
};
- getUsersList: () => void;
+ deleteUserMutation: (data: { userName: string }) => void;
+ updateUserPasswordIsError: boolean;
+ getUserRolesIsError: boolean;
+ getUserRolesIsLoading: boolean;
+ updateUserPasswordMutation: (data: { userName: string }) => void;
+ updateUserPasswordIsLoading: boolean;
+ udpateUserPasswordData: AxiosResponse | undefined;
+ resetPasswordError: string;
}
const RoleTR: FC = (props) => {
- const { user, getUsersList } = props;
+ const {
+ user,
+ getUserRolesIsError,
+ getUserRolesIsLoading,
+ updateUserPasswordMutation,
+ updateUserPasswordIsError,
+ updateUserPasswordIsLoading,
+ udpateUserPasswordData,
+ resetPasswordError,
+ deleteUserMutation,
+ } = props;
const [openedDelete, { close: closeDelete, open: openDelete }] = useDisclosure();
const [openedDeleteRole, { close: closeDeleteRole, open: openDeleteRole }] = useDisclosure();
const [openedEditModal, { close: closeEditModal, open: openEditModal }] = useDisclosure();
@@ -44,50 +59,19 @@ const RoleTR: FC = (props) => {
const [SelectedRole, setSelectedRole] = useState('');
const [roleSearchValue, setRoleSearchValue] = useState('');
+ const { getUserRolesData, getUserRolesMutation, updateUserMutation, updateUserIsSuccess } = useUser();
- const { putRole, data: putRoleData, resetData: resetPutRoleData } = usePutUserRole();
- const {
- data: newPassword,
- error: resetPasswordError,
- loading: resetPasswordLoading,
- resetPasswordUser,
- resetData: resetNewPassword,
- } = usePostUserResetPassword();
- const {
- data: userRole,
- error: userRoleError,
- loading: userRoleLoading,
- getRoles,
- resetData: userRoleReset,
- } = useGetUserRole();
- const { data: deletedUser, deleteUserFun: deleteUserAction, resetData: deletedUserReset } = useDeleteUser();
- const { data: roles, getRolesList, resetData: rolesReset } = useGetRoles();
-
- useEffect(() => {
- if (deletedUser) {
- getUsersList();
- deletedUserReset();
- }
- }, [deletedUser]);
+ const { getRolesData } = useRole();
useEffect(() => {
- getRoles(user.id);
- getRolesList();
-
- return () => {
- rolesReset();
- userRoleReset();
- };
+ getUserRolesMutation({ userName: user.id });
}, [user]);
useEffect(() => {
- if (putRoleData) {
- getRoles(user.id);
+ if (updateUserIsSuccess) {
+ getUserRolesMutation({ userName: user.id });
}
- return () => {
- resetPutRoleData();
- };
- }, [putRoleData]);
+ }, [updateUserIsSuccess]);
const removeButton = (role: string) => (
= (props) => {
);
};
- const getBadges = (userRole: any) => {
- if (Object.keys(userRole).length > 0) {
- const Badges = Object.keys(userRole).map((role: any) => {
+
+ const getBadges = () => {
+ if (Object.keys(getUserRolesData?.data).length > 0) {
+ const Badges = Object.keys(getUserRolesData?.data).map((role: any) => {
return {getBadge(role, user.method === 'native' ? true : false)};
});
return Badges;
@@ -130,18 +115,20 @@ const RoleTR: FC = (props) => {
closeDelete();
setUserInput('');
};
+
const handleDelete = () => {
- deleteUserAction(user.id);
+ deleteUserMutation({ userName: user.id });
closeDelete();
setUserInput('');
};
// For Delete Role
const handleRoleDelete = () => {
- let filtered = Object.keys(userRole).filter((role) => role !== deleteRole);
- putRole(user.id, filtered);
+ let filtered = Object.keys(getUserRolesData?.data).filter((role) => role !== deleteRole);
+ updateUserMutation({ userName: user.id, roles: filtered });
closeDeleteRole();
setDeleteRole(null);
+ getUserRolesMutation({ userName: user.id });
};
const handleCloseRoleDelete = () => {
closeDeleteRole();
@@ -149,7 +136,6 @@ const RoleTR: FC = (props) => {
};
// For Edit Role
-
const handleCloseRoleEdit = () => {
closeEditModal();
setSelectedRole('');
@@ -157,29 +143,24 @@ const RoleTR: FC = (props) => {
};
const handleEditUserRole = () => {
-
- let userRoleArray: any = Object.keys(userRole);
+ let userRoleArray: any = Object.keys(getUserRolesData?.data);
if (userRoleArray.includes(SelectedRole) || SelectedRole === '') {
return;
}
userRoleArray.push(SelectedRole);
-
- putRole(user.id, userRoleArray);
+
+ updateUserMutation({ userName: user.id, roles: userRoleArray });
handleCloseRoleEdit();
};
-
-
- //for reset password
-
+ // for reset password
const handleCloseResetPassword = () => {
close();
setUserInput('');
- resetNewPassword();
};
const handleResetPassword = () => {
- resetPasswordUser(UserInput);
+ updateUserPasswordMutation({ userName: UserInput });
};
const { classes } = useUsersStyles();
@@ -188,13 +169,13 @@ const RoleTR: FC = (props) => {
{user.id} |
- {userRoleError ? (
+ {getUserRolesIsError ? (
'Error'
- ) : userRoleLoading ? (
+ ) : getUserRolesIsLoading ? (
'loading..'
- ) : userRole ? (
+ ) : getUserRolesData?.data ? (
<>
- {getBadges(userRole)}
+ {getBadges()}
@@ -223,19 +204,20 @@ const RoleTR: FC = (props) => {
-
-
|
@@ -271,7 +253,7 @@ const RoleTR: FC = (props) => {
- {userRole && deleteRole && userRole[deleteRole] ? (
+ {getUserRolesData?.data && deleteRole && getUserRolesData?.data[deleteRole] ? (
= (props) => {
required
/>
- {resetPasswordError ? (
+ {updateUserPasswordIsError ? (
{resetPasswordError}
- ) : resetPasswordLoading ? (
+ ) : updateUserPasswordIsLoading ? (
loading
- ) : newPassword ? (
+ ) : udpateUserPasswordData?.data ? (
Password
= (props) => {
language="markup"
copyLabel="Copy password to clipboard"
copiedLabel="Password copied to clipboard">
- {newPassword}
+ {udpateUserPasswordData?.data}
Warning this is the only time you are able to see Password
@@ -353,14 +335,16 @@ const RoleTR: FC = (props) => {
)}
- {user.method === "native" ? (
- Reset Password
- ):(
+ {user.method === 'native' ? (
+
+ Reset Password
+
+ ) : (
Cannot reset password for this user
)}
@@ -386,12 +370,11 @@ const RoleTR: FC = (props) => {
onSearchChange={(value) => setRoleSearchValue(value)}
onDropdownClose={() => setRoleSearchValue(SelectedRole)}
onDropdownOpen={() => setRoleSearchValue('')}
- data={roles}
+ data={getRolesData?.data}
searchable
label="Select a role to assign"
required
/>
-
@@ -400,9 +383,13 @@ const RoleTR: FC = (props) => {
color="gray"
className={classes.modalActionBtn}
onClick={handleEditUserRole}
- //if role is already assigned or no role is selected then disable the button
- disabled={userRole && (Object.keys(userRole).includes(SelectedRole) || SelectedRole === '') ? true : false}
- >
+ // if role is already assigned or no role is selected then disable the button
+ disabled={
+ getUserRolesData?.data &&
+ (Object.keys(getUserRolesData?.data).includes(SelectedRole) || SelectedRole === '')
+ ? true
+ : false
+ }>
Create
diff --git a/src/pages/AccessManagement/Roles.tsx b/src/pages/AccessManagement/Roles.tsx
index 87ac8c38..23d0a373 100644
--- a/src/pages/AccessManagement/Roles.tsx
+++ b/src/pages/AccessManagement/Roles.tsx
@@ -4,29 +4,21 @@ import { FC, useEffect, useState } from 'react';
import { useUsersStyles } from './styles';
import { useGetLogStreamList } from '@/hooks/useGetLogStreamList';
import { useHeaderContext } from '@/layouts/MainLayout/Context';
-import { useGetRoles } from '@/hooks/useGetRoles';
import PrivilegeTR from './PrivilegeTR';
import { IconPencil, IconUserPlus } from '@tabler/icons-react';
-import { usePutRole } from '@/hooks/usePutRole';
-import { usePutDefaultRole } from '@/hooks/usePutDefaultRole';
-import { useGetDefaultRole } from '@/hooks/useGetDefaultRole';
+import { useRole } from '@/hooks/useRole';
+
const Roles: FC = () => {
useDocumentTitle('Parseable | Users');
const {
- state: { subCreateUserModalTogle, subInstanceConfig },
+ state: { subInstanceConfig },
} = useHeaderContext();
- useEffect(() => {
- const listener = subCreateUserModalTogle.subscribe(setModalOpen);
- return () => {
- listener();
- };
- }, [subCreateUserModalTogle.get()]);
-
const [modalOpen, setModalOpen] = useState(false);
+
const [defaultRoleModalOpen, setDefaultRoleModalOpen] = useState(false);
const [inputDefaultRole, setInputDefaultRole] = useState('');
- const [defaultRole, setDefaultRole] = useState(null);
+ const [defaultRole, setDefaultRole] = useState(null);
const [oidcActive, setOidcActive] = useState(subInstanceConfig.get()?.oidcActive ?? false);
const [createRoleInput, setCreateRoleInput] = useState('');
const [tagInput, setTagInput] = useState('');
@@ -34,17 +26,25 @@ const Roles: FC = () => {
const [SelectedStream, setSelectedStream] = useState('');
const [streamSearchValue, setStreamSearchValue] = useState('');
- const { data: streams } = useGetLogStreamList();
+ const { getLogStreamListData } = useGetLogStreamList();
- const { data: defaultRoleResponse, getDefaultOidc } = useGetDefaultRole();
- const { data: putDefaultRoleResponse, setDefaultRole: putDefaultRole } = usePutDefaultRole();
- const { data: CreatedRoleResponse, putRolePrivilege, resetData: resetCreateRoleData } = usePutRole();
- const { data: roles, error: rolesError, loading: rolesLoading, getRolesList, resetData: rolesReset } = useGetRoles();
+ const {
+ getDefaultRoleData,
+ getDefaultRoleMutation,
+ updateDefaultRoleMutation,
+ updateDefaultRoleData,
+ updateRoleMutation,
+ getRolesData,
+ getRolesIsLoading,
+ getRolesIsSuccess,
+ getRolesRefetch,
+ deleteRoleMutation,
+ getRoleIsLoading,
+ getRoleIsError,
+ } = useRole();
- const [tableRows, setTableRows] = useState([]);
useEffect(() => {
- getRolesList();
- getDefaultOidc();
+ getDefaultRoleMutation();
const listener = subInstanceConfig.subscribe((value) => {
if (value) {
setOidcActive(value.oidcActive);
@@ -53,59 +53,49 @@ const Roles: FC = () => {
return () => {
listener();
- rolesReset();
};
}, []);
- useEffect(() => {
- if (roles) {
- const getrows = async () => {
- let rows = roles.map((role: any) => {
- return ;
- });
- setTableRows(rows);
- };
-
- getrows();
- }
- if (rolesError) {
- setTableRows(
-
- error |
- ,
- );
- }
- if (rolesLoading) {
- setTableRows(
-
- loading |
- ,
- );
- }
- }, [roles, rolesError, rolesLoading, defaultRole]);
-
- useEffect(() => {
- if (CreatedRoleResponse) {
- getRolesList();
- }
- }, [CreatedRoleResponse]);
+ let rows =
+ getRolesIsSuccess && getRolesData?.data ? (
+ getRolesData?.data.map((role: any) => {
+ return (
+
+ );
+ })
+ ) : getRolesIsLoading ? (
+
+ loading |
+
+ ) : (
+
+ error |
+
+ );
useEffect(() => {
- if (putDefaultRoleResponse) {
- setDefaultRole(putDefaultRoleResponse.data);
+ if (updateDefaultRoleData?.data) {
+ setDefaultRole(updateDefaultRoleData?.data);
}
- }, [putDefaultRoleResponse]);
+ }, [updateDefaultRoleData?.data]);
useEffect(() => {
- if (defaultRoleResponse) {
- setDefaultRole(defaultRoleResponse);
+ if (getDefaultRoleData?.data) {
+ setDefaultRole(getDefaultRoleData?.data);
}
- }, [defaultRoleResponse]);
+ }, [getDefaultRoleData?.data]);
const handleClose = () => {
setCreateRoleInput('');
setModalOpen(false);
- resetCreateRoleData();
setSelectedPrivilege('');
setSelectedStream('');
setStreamSearchValue('');
@@ -125,7 +115,7 @@ const Roles: FC = () => {
});
}
if (selectedPrivilege === 'reader' || selectedPrivilege === 'writer' || selectedPrivilege === 'ingester') {
- if (streams?.find((stream) => stream.name === SelectedStream)) {
+ if (getLogStreamListData?.data?.find((stream) => stream.name === SelectedStream)) {
if (tagInput !== '' && tagInput !== undefined && selectedPrivilege === 'reader') {
userRole?.push({
privilege: selectedPrivilege,
@@ -144,12 +134,12 @@ const Roles: FC = () => {
}
}
}
- putRolePrivilege(createRoleInput, userRole);
+ updateRoleMutation({ userName: createRoleInput, privilege: userRole });
handleClose();
};
const createVaildtion = () => {
- if (roles?.includes(createRoleInput) && createRoleInput.length > 0) {
+ if (getRolesData?.data?.includes(createRoleInput) && createRoleInput.length > 0) {
return true;
}
if (selectedPrivilege !== '') {
@@ -157,13 +147,13 @@ const Roles: FC = () => {
return false;
}
if (selectedPrivilege === 'reader') {
- if (streams?.find((stream) => stream.name === SelectedStream)) {
+ if (getLogStreamListData?.data?.find((stream) => stream.name === SelectedStream)) {
return false;
}
return true;
}
if (selectedPrivilege === 'writer' || selectedPrivilege === 'ingester') {
- if (streams?.find((stream) => stream.name === SelectedStream)) {
+ if (getLogStreamListData?.data?.find((stream) => stream.name === SelectedStream)) {
return false;
}
return true;
@@ -173,14 +163,14 @@ const Roles: FC = () => {
};
const defaultRoleVaildtion = () => {
- if (inputDefaultRole === '' && !roles?.includes(inputDefaultRole)) {
+ if (inputDefaultRole === '' && !getRolesData?.data?.includes(inputDefaultRole)) {
return true;
}
return false;
};
const handleSetDefaultRole = () => {
- putDefaultRole(inputDefaultRole);
+ updateDefaultRoleMutation({ roleName: inputDefaultRole });
handleDefaultRoleModalClose();
};
@@ -192,8 +182,6 @@ const Roles: FC = () => {
Roles
-
-
{
Delete |
|
- {tableRows}
+ {rows}
{
|