passbolt-styleguide
Version:
Passbolt styleguide contains common styling assets used by the different sites, plugin, etc.
151 lines (137 loc) • 4.73 kB
JavaScript
/**
* Passbolt ~ Open source password manager for teams
* Copyright (c) Passbolt SA (https://www.passbolt.com)
*
* Licensed under GNU Affero General Public License version 3 of the or any later version.
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
* @link https://www.passbolt.com Passbolt(tm)
* @since 2.13.0
*/
import React from "react";
import PropTypes from "prop-types";
import CaretDownSVG from "../../../../img/svg/caret_down.svg";
import CaretRightSVG from "../../../../img/svg/caret_right.svg";
import { withUserWorkspace } from "../../../contexts/UserWorkspaceContext";
import { withAppContext } from "../../../../shared/context/AppContext/AppContext";
import GroupAvatar from "../../Common/Avatar/GroupAvatar";
import { Trans, withTranslation } from "react-i18next";
/**
* This component displays the user details about information
*/
class DisplayUserDetailsGroups extends React.Component {
/**
* Default constructor
* @param props
*/
constructor(props) {
super(props);
this.state = this.defaultState;
this.bindHandlers();
}
/**
* Returns the component default state
*/
get defaultState() {
return {
open: false, // Flag for the expand / collapse mode
};
}
/**
* Bind the component handlers
*/
bindHandlers() {
this.handleTitleClicked = this.handleTitleClicked.bind(this);
}
/**
* The groups the current selected user belongs to
*/
get groups() {
const { groups } = this.props.context;
// If the group haven't yet been loaded
if (!groups) {
return [];
}
const selectedUser = this.props.userWorkspaceContext.details.user;
if (selectedUser) {
const belongsToGroup = (group) => group.groups_users.some((group_user) => group_user.user_id === selectedUser.id);
const groupUser = (group) => group.groups_users.find((group_user) => group_user.user_id === selectedUser.id);
const userRole = (groupUser) => (groupUser.is_admin ? this.translate("Group manager") : this.translate("Member"));
const roleMapper = (group) => Object.assign({}, group, { role: userRole(groupUser(group)) });
return groups.filter(belongsToGroup).map(roleMapper);
} else {
return [];
}
}
/**
* Returns the base url
*/
get baseUrl() {
return this.props.context.userSettings.getTrustedDomain();
}
/**
* Handle the click on the title
*/
handleTitleClicked() {
this.setState({ open: !this.state.open });
}
/**
* Get the translate function
* @returns {function(...[*]=)}
*/
get translate() {
return this.props.t;
}
/**
* Render the component
*/
render() {
const groups = this.groups;
const hasGroups = groups.length > 0;
return (
<div className={`detailed-user-groups accordion sidebar-section ${this.state.open ? "" : "closed"}`}>
<div className="accordion-header">
<h4>
<button type="button" className="link no-border section-opener" onClick={this.handleTitleClicked}>
<span className="accordion-title">Groups</span>
{this.state.open && <CaretDownSVG />}
{!this.state.open && <CaretRightSVG />}
</button>
</h4>
</div>
{this.state.open && (
<div className="accordion-content">
<ul>
{hasGroups &&
groups.map((group) => (
<div key={group.id} className="permission usercard-col-2">
<GroupAvatar group={group} />
<div className="content-wrapper">
<div className="content">
<div className="name">{group.name}</div>
<div className="subinfo">{group.role}</div>
</div>
</div>
</div>
))}
</ul>
{!hasGroups && (
<em className="empty-feedback empty-group-feedback">
<Trans>The user is not a member of any group yet</Trans>
</em>
)}
</div>
)}
</div>
);
}
}
DisplayUserDetailsGroups.propTypes = {
context: PropTypes.any, // The application context
userWorkspaceContext: PropTypes.object, // The user workspace context
t: PropTypes.func, // The translation function
};
export default withAppContext(withUserWorkspace(withTranslation("common")(DisplayUserDetailsGroups)));