import React, { useEffect } from "react"; import { connect } from "react-redux"; import { Icon } from "@blueprintjs/core"; import { TableWrapper } from "components/designSystems/appsmith/TableStyledWrappers"; import { AppState } from "reducers"; import { getAllUsers, getAllRoles, getOrgs, } from "selectors/organizationSelectors"; import PageSectionDivider from "pages/common/PageSectionDivider"; import PageSectionHeader from "pages/common/PageSectionHeader"; import { ReduxActionTypes } from "constants/ReduxActionConstants"; import InviteUsersFormv2 from "pages/organization/InviteUsersFromv2"; import Button from "components/editorComponents/Button"; import { OrgUser, Organization } from "constants/orgConstants"; import { Menu, MenuItem, Popover, Position } from "@blueprintjs/core"; import styled from "styled-components"; import { FormIcons } from "icons/FormIcons"; import "@syncfusion/ej2-react-grids/styles/material.css"; import { RouteComponentProps } from "react-router"; import Spinner from "components/editorComponents/Spinner"; import FormDialogComponent from "components/editorComponents/form/FormDialogComponent"; import { getCurrentUser } from "selectors/usersSelectors"; import { User } from "constants/userConstants"; import { useTable, useFlexLayout } from "react-table"; type OrgProps = { allOrgs: Organization[]; changeOrgName: (value: string) => void; getAllApplication: () => void; fetchUser: (orgId: string) => void; fetchAllRoles: (orgId: string) => void; deleteOrgUser: (orgId: string, username: string) => void; changeOrgUserRole: (orgId: string, role: string, username: string) => void; allUsers: OrgUser[]; allRole: object; currentUser: User | undefined; isFetchAllUsers: boolean; isFetchAllRoles: boolean; }; export type PageProps = OrgProps & RouteComponentProps<{ orgId: string; }>; export type MenuItemProps = { rolename: string; }; type DropdownProps = { activeItem: string; userRoles: object; username: string; }; const StyledDropDown = styled.div` cursor: pointer; `; const StyledTableWrapped = styled(TableWrapper)` width: 100%; height: auto; font-size: 14px; .table { .tbody { overflow: auto; height: auto; max-height: calc( 100vh - (100vh / 3) - ${props => props.theme.headerHeight} ); } } `; const StyledMenu = styled(Menu)` &&&&.bp3-menu { max-width: 250px; cursor: pointer; } `; export const OrgSettings = (props: PageProps) => { const { match: { params: { orgId }, }, deleteOrgUser, changeOrgUserRole, allOrgs, fetchUser, fetchAllRoles, getAllApplication, } = props; const userTableData = props.allUsers.map(user => ({ ...user, roles: props.allRole, isCurrentUser: user.username === props.currentUser?.username, })); const data = React.useMemo(() => userTableData, [ props.allUsers, props.allRole, ]); const columns = React.useMemo(() => { return [ { Header: "Email", accessor: "username", }, { Header: "Name", accessor: "name", }, { Header: "Role", accessor: "roleName", Cell: (cellProps: any) => { const { roleName, roles, username, isCurrentUser, isChangingRole, } = cellProps.row.original; if (isCurrentUser) { return
{roleName}
; } return ( } position={Position.BOTTOM_LEFT} > {roleName} {isChangingRole ? : undefined} ); }, }, { Header: "Delete", accessor: "delete", Cell: (cellProps: any) => { const { username, isCurrentUser, isDeleting, } = cellProps.row.original; return ( !isCurrentUser && (isDeleting ? ( ) : ( deleteOrgUser(orgId, username)} style={{ alignSelf: "center", cursor: "pointer" }} /> )) ); }, }, ]; }, [props.allUsers, props.allRole]); const currentOrg = allOrgs.find(org => org.organization.id === orgId); const currentOrgName = currentOrg?.organization.name ?? ""; const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow, } = useTable( { columns, data, manualPagination: true, }, useFlexLayout, ); useEffect(() => { fetchUser(orgId); fetchAllRoles(orgId); getAllApplication(); }, [orgId, fetchUser, fetchAllRoles, getAllApplication]); const Dropdown = (props: DropdownProps) => { return ( {Object.entries(props.userRoles).map((role, index) => { const MenuContent = (
{role[0]}
{role[1]}
); return ( changeOrgUserRole(orgId, role[0], props.username)} active={props.activeItem === role[0]} text={MenuContent} /> ); })}
); }; return (

{currentOrgName}

Users

} Form={InviteUsersFormv2} orgId={orgId} title={`Invite Users to ${currentOrgName}`} setMaxWidth />
{props.isFetchAllUsers && props.isFetchAllRoles ? ( ) : (
{headerGroups.map((headerGroup: any, index: number) => (
{headerGroup.headers.map( (column: any, columnIndex: number) => (
{column.render("Header")}
), )}
))}
{rows.map((row: any, index: number) => { prepareRow(row); return (
{row.cells.map((cell: any, cellIndex: number) => { return (
{cell.render("Cell")}
); })}
); })}
)}
); }; const mapStateToProps = (state: AppState) => ({ allUsers: getAllUsers(state), allRole: getAllRoles(state), isFetchAllUsers: state.ui.orgs.loadingStates.isFetchAllUsers, isFetchAllRoles: state.ui.orgs.loadingStates.isFetchAllRoles, allOrgs: getOrgs(state), currentUser: getCurrentUser(state), }); const mapDispatchToProps = (dispatch: any) => ({ getAllApplication: () => dispatch({ type: ReduxActionTypes.GET_ALL_APPLICATION_INIT }), changeOrgName: (name: string) => dispatch({ type: ReduxActionTypes.UPDATE_ORG_NAME_INIT, payload: { name, }, }), changeOrgUserRole: (orgId: string, role: string, username: string) => dispatch({ type: ReduxActionTypes.CHANGE_ORG_USER_ROLE_INIT, payload: { orgId, role, username, }, }), deleteOrgUser: (orgId: string, username: string) => dispatch({ type: ReduxActionTypes.DELETE_ORG_USER_INIT, payload: { orgId, username, }, }), fetchUser: (orgId: string) => dispatch({ type: ReduxActionTypes.FETCH_ALL_USERS_INIT, payload: { orgId, }, }), fetchAllRoles: (orgId: string) => dispatch({ type: ReduxActionTypes.FETCH_ALL_ROLES_INIT, payload: { orgId, }, }), }); export default connect(mapStateToProps, mapDispatchToProps)(OrgSettings);