diff --git a/src/components/SearchBar/SearchBar.tsx b/src/components/SearchBar/SearchBar.tsx index 4c9e992..dfb44c9 100644 --- a/src/components/SearchBar/SearchBar.tsx +++ b/src/components/SearchBar/SearchBar.tsx @@ -6,7 +6,7 @@ * All rights reserved. */ -import React from "react"; +import React, {ChangeEvent} from "react"; import {FormControl, InputAdornment, InputBase, withStyles} from "@material-ui/core"; import Stylable from "../../interfaces/Stylable"; import styles from "./styles"; @@ -14,7 +14,7 @@ import CloseIcon from "@material-ui/icons/Close"; import SearchIcon from "@material-ui/icons/Search"; interface SearchBarProps extends Stylable { - + onChange?(event: ChangeEvent): void; } const SearchBar = React.forwardRef((props: SearchBarProps, ref) => { @@ -22,6 +22,7 @@ const SearchBar = React.forwardRef((props: SearchBarProps, ref) => { classes, className, style, + onChange, } = props; @@ -29,6 +30,7 @@ const SearchBar = React.forwardRef((props: SearchBarProps, ref) => { diff --git a/src/layout/MonitorLayout/MonitorLayout.tsx b/src/layout/MonitorLayout/MonitorLayout.tsx index 6a049b3..ad558d2 100644 --- a/src/layout/MonitorLayout/MonitorLayout.tsx +++ b/src/layout/MonitorLayout/MonitorLayout.tsx @@ -154,7 +154,7 @@ const MonitorLayout = React.forwardRef((props: MonitorLayoutProps, ref: Ref { const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler - // .on(401, () => {logout()}) + .on(401, () => {logout()}) .on(404, "User not found") .handle(err); }) diff --git a/src/views/OrganizationPageView/LocalComponents/DialogUser/DialogUser.tsx b/src/views/OrganizationPageView/LocalComponents/DialogUser/DialogUser.tsx index 26b2e76..a49941f 100644 --- a/src/views/OrganizationPageView/LocalComponents/DialogUser/DialogUser.tsx +++ b/src/views/OrganizationPageView/LocalComponents/DialogUser/DialogUser.tsx @@ -34,6 +34,8 @@ import UserData from "../../../../interfaces/UserData"; import Role from "../../../../interfaces/Role"; import SearchIcon from "@material-ui/icons/Search"; import CloseIcon from "@material-ui/icons/Close"; +import SearchBar from "../../../../components/SearchBar/SearchBar"; +import useAuth from "../../../../hooks/useAuth"; interface DialogUserProps extends Stylable { user: UserData | null; @@ -64,16 +66,31 @@ const DialogUser = React.forwardRef((props: DialogUserProps, ref: Ref) => { onRemoveRole, } = props; + const confirm = useConfirm(); + const {getUser} = useAuth(); + const loggedUser = getUser(); + + + // const [user, setUser] = useState(userOrg); const [isAddRoleToUserButtonActive, setIsAddRoleToUserButtonActive] = useState(null); const [isRemoveRoleFromUserButtonActive, setIsRemoveRoleFromUserButtonActive] = useState(null); const [filterRoles, setFilterRoles] = useState(roles); const [searchValue, setSearchValue] = useState(""); + useEffect(() => { setFilterRoles(roles.filter(role => role.name.toLowerCase().includes(searchValue))); }, [searchValue]); + useEffect(() => { + setFilterRoles(roles); + }, [roles]); + + // useEffect(() => { + // setUser(userOrg); + // }, [userOrg]); + function handleSearch(event: React.ChangeEvent) { setSearchValue(event.target.value.toLowerCase()); } @@ -164,23 +181,26 @@ const DialogUser = React.forwardRef((props: DialogUserProps, ref: Ref) => { - - - - - - - - - + + + + {/**/} + {/* */} + {/* */} + {/* */} + {/* */} + {/* */} + {/* */} + {/* */} + {/**/} {filterRoles.length !== 0 ? filterRoles.map(role => { return ( @@ -247,7 +267,7 @@ const DialogUser = React.forwardRef((props: DialogUserProps, ref: Ref) => { onClick={() => confirm(async () => onRemove([user?.id]), {title: `are you sure you want to remove user: ${user?.username} ?`})} > - Remove User from Organization + {loggedUser?.id === user?.id ? "Leave from Organization" : "Remove User from Organization"} diff --git a/src/views/OrganizationPageView/LocalComponents/PluginComponent/PluginComponent.tsx b/src/views/OrganizationPageView/LocalComponents/PluginComponent/PluginComponent.tsx index 4c16a7a..5ae1041 100644 --- a/src/views/OrganizationPageView/LocalComponents/PluginComponent/PluginComponent.tsx +++ b/src/views/OrganizationPageView/LocalComponents/PluginComponent/PluginComponent.tsx @@ -20,7 +20,10 @@ import Plugin from "../../../../interfaces/Plugin"; */ interface PluginComponentProps extends Stylable { plugin: Plugin, + can?: boolean, + invokeDialog(): void, + setCurrentPlugin(id: number): void, } @@ -37,19 +40,25 @@ const PluginComponent = React.forwardRef((props: PluginComponentProps, ref: Ref< plugin, invokeDialog, setCurrentPlugin, + can = true, } = props; return ( - {invokeDialog(); setCurrentPlugin(plugin.id)}}> + { + invokeDialog(); + setCurrentPlugin(plugin.id); + }}> + {can && + } diff --git a/src/views/OrganizationPageView/LocalComponents/TopicWithButton/TopicWithButton.tsx b/src/views/OrganizationPageView/LocalComponents/TopicWithButton/TopicWithButton.tsx index 0dd80bc..b979eff 100644 --- a/src/views/OrganizationPageView/LocalComponents/TopicWithButton/TopicWithButton.tsx +++ b/src/views/OrganizationPageView/LocalComponents/TopicWithButton/TopicWithButton.tsx @@ -30,6 +30,7 @@ import List from "@material-ui/core/List"; */ interface TopicWithButtonProps extends Stylable { children?: string, + can?: boolean; onClick?(): void, } @@ -46,6 +47,7 @@ const TopicWithButton = React.forwardRef((props: TopicWithButtonProps, ref: Ref< className, children, onClick, + can = true } = props; return ( @@ -53,6 +55,7 @@ const TopicWithButton = React.forwardRef((props: TopicWithButtonProps, ref: Ref< + {can && + } diff --git a/src/views/OrganizationPageView/OrganizationPageView.tsx b/src/views/OrganizationPageView/OrganizationPageView.tsx index 744f74e..4dec13f 100644 --- a/src/views/OrganizationPageView/OrganizationPageView.tsx +++ b/src/views/OrganizationPageView/OrganizationPageView.tsx @@ -54,6 +54,7 @@ import useAuth from "../../hooks/useAuth"; import DialogSlave from "./LocalComponents/DialogSlave"; import DemoRole from "../../interfaces/DemoRole"; import ErrorHandler from "../../utils/ErrorHandler"; +import User from "../../entities/User"; /** * OrganizationPageViewPropsStyled - interface for OrganizationPageView function @@ -84,14 +85,14 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, //basic - const {logout} = useAuth(); + const {logout, getUser} = useAuth(); const enqueueErrorSnackbar = useEnqueueErrorSnackbar(); const coreRequest = useCoreRequest(); const {getRouteParams, changeRoute} = useChangeRoute(); const {id} = getRouteParams(); const confirm = useConfirm(); const [loaded, setLoaded] = useState(false); - + const [user, setUser] = useState(); //roles const [isAddRoleButtonActive, setIsAddRoleButtonActive] = useState(false); @@ -145,6 +146,17 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, // setLoaded(true); // } + const handleGetUser = (orgUsers: UserData[]) => { + const u = getUser(); + let uToAdd; + if (u) { + uToAdd = orgUsers.filter(orgUser => orgUser.id === u.id); + if (uToAdd) { + setUser(uToAdd[0]); + } + } + }; + //roles async function handleGetRoles() { try { @@ -153,7 +165,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, } catch (err) { const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(404, "Organization not found") .handle(err); } @@ -167,7 +181,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, } catch (err) { const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(404, "Role not found") .handle(err); } @@ -180,7 +196,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, } catch (err) { const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(404, "Organization not found") .handle(err); } @@ -195,7 +213,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, .catch(err => { const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(404, "Role not found") .handle(err); }); @@ -215,7 +235,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, .catch(err => { const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .handle(err); }); handleGetRoles().then(); @@ -224,7 +246,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler .on(400, "Can not add role to user") - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(403, "User already owns this role") .on(404, "Role or User not found") .handle(err); @@ -248,7 +272,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler .on(400, "Can not delete role from user") - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(403, "User does not own this role") .on(404, "Role not found") .handle(err); @@ -270,7 +296,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler .on(400, "Can not add new role") - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(409, "Role with this name already exist") .handle(err); }); @@ -291,7 +319,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler .on(400, "Can not delete role") - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(404, "Role not found") .handle(err); }); @@ -310,7 +340,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler .on(400, "Can not edit role") - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(404, "Role not found") .on(409, "Role with this name already exist") .handle(err); @@ -350,11 +382,14 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, try { const response = await coreRequest().get(`organizations/${id}/users`); setOrganizationUsers(response.body); + handleGetUser(response.body); return response.body; } catch (err) { const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(404, "Organization not found") .handle(err); } @@ -367,7 +402,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, } catch (err) { const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(404, "Organization not found") .handle(err); } @@ -380,7 +417,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, } catch (err) { const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .handle(err); } } @@ -388,11 +427,13 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, async function handleGetAvailableUsers() { try { const response = await coreRequest().get(`organizations/${id}/availableUsers`); - setAvailableUsers(response.body) + setAvailableUsers(response.body); } catch (err) { const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(404, "Organization not found") .handle(err); } @@ -414,7 +455,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler .on(400, "Can not add user") - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .handle(err); }); } @@ -433,7 +476,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler .on(400, "Can not delete user") - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(404, "User not exist") .on(409, "Some users are not in organization") .handle(err); @@ -476,7 +521,9 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, .catch(err => { const errorHandler = new ErrorHandler(enqueueErrorSnackbar); errorHandler - .on(401, () => {logout()}) + .on(401, () => { + logout(); + }) .on(404, "Plugin not found") .handle(err); }); @@ -485,7 +532,7 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, const handleCloseAddUsersDialog = () => { setIsButtonActive(false); setNewUsers([]); - } + }; const slaves = [ @@ -555,7 +602,7 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, {mainInfo} - + 0 && user.roles[0].canManageRoles))}/> {slaves.map((slave, key) => { @@ -568,7 +615,7 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, primary={slave} secondary="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequuntur, rerum?" /> - + {(user && (user.roles.length > 0 && user.roles[0].canManagePlugins)) && @@ -576,6 +623,7 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, + } ); })} @@ -589,7 +637,12 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, /> - + 0 && user.roles[0].canManageRoles))} + children="Roles" + /> + + {(user && (user.roles.length > 0 && user.roles[0].canManageRoles)) && + } ); })} @@ -650,7 +705,11 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, onModifyRole={handleModifyRole} /> - + 0 && user.roles[0].canManageUsers))} + children="Members" + /> - {organizationUsers.map((user: UserData, key: number) => { + {organizationUsers.map((orgUser: UserData, key: number) => { return ( { - changeRoute({page: "user", id: user.id}); + changeRoute({page: "user", id: orgUser.id}); }}> - + - {user.roles[0] && + {orgUser.roles[0] && } - handleIsUserSettingsButtonActive(user)}> + {(user && (user.roles.length > 0 && user.roles[0].canManageUsers)) && + handleIsUserSettingsButtonActive(orgUser)}> + } @@ -704,6 +765,7 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, 0 && user.roles[0].canManagePlugins))} onClick={() => changeRoute({page: "plugin/create", id: id})} /> {plugins?.map((plugin) => { @@ -714,6 +776,7 @@ const OrganizationPageView = React.forwardRef((props: OrganizationPageViewProps, setDialogPluginButton(!dialogPluginButton); }} setCurrentPlugin={handleSetCurrentPlugin} + can={(user !== undefined && (user.roles.length > 0 && user.roles[0].canManagePlugins))} /> ); })}