import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import dateFnsFormat from 'date-fns/format'; import UserGroupUserModal from './UserGroupUserModal'; import UserPicture from '../../User/UserPicture'; import { createSubscribedElement } from '../../UnstatedUtils'; import AppContainer from '../../../services/AppContainer'; import { toastSuccess, toastError } from '../../../util/apiNotification'; class UserGroupUserTable extends React.Component { constructor(props) { super(props); this.state = { userGroupRelations: props.userGroupRelations, notRelatedUsers: props.notRelatedUsers, isUserGroupUserModalOpen: false, }; this.xss = window.xss; this.removeUser = this.removeUser.bind(this); this.openUserGroupUserModal = this.openUserGroupUserModal.bind(this); this.closeUserGroupUserModal = this.closeUserGroupUserModal.bind(this); this.addUser = this.addUser.bind(this); } async removeUser(username) { try { const res = await this.props.appContainer.apiv3.delete(`/user-groups/${this.props.userGroup._id}/users/${username}`); this.setState((prevState) => { return { userGroupRelations: prevState.userGroupRelations.filter((u) => { return u._id !== res.data.userGroupRelation._id }), notRelatedUsers: [...prevState.notRelatedUsers, res.data.user], }; }); toastSuccess(`Removed "${username}" from "${this.xss.process(this.props.userGroup.name)}"`); } catch (err) { toastError(new Error(`Unable to remove "${this.xss.process(username)}" from "${this.xss.process(this.props.userGroup.name)}"`)); } } openUserGroupUserModal() { this.setState({ isUserGroupUserModalOpen: true }); } closeUserGroupUserModal() { this.setState({ isUserGroupUserModalOpen: false }); } addUser(user, userGroup, userGroupRelation) { this.setState((prevState) => { return { userGroupRelations: [...prevState.userGroupRelations, userGroupRelation], notRelatedUsers: prevState.notRelatedUsers.filter((u) => { return u._id !== user._id }), }; }); } render() { const { t } = this.props; return ( { t('User List') } {this.state.userGroupRelations.map((sRelation) => { const { relatedUser } = sRelation; return ( ); })}
# { t('User') } { t('Name') } { t('Created') } { t('Last Login')}
{relatedUser.username} {relatedUser.name} {relatedUser.createdAt ? dateFnsFormat(new Date(relatedUser.createdAt), 'yyyy-MM-dd') : ''} {relatedUser.lastLoginAt ? dateFnsFormat(new Date(relatedUser.lastLoginAt), 'yyyy-MM-dd HH:mm:ss') : ''}
); } } UserGroupUserTable.propTypes = { t: PropTypes.func.isRequired, // i18next appContainer: PropTypes.instanceOf(AppContainer).isRequired, userGroupRelations: PropTypes.arrayOf(PropTypes.object).isRequired, notRelatedUsers: PropTypes.arrayOf(PropTypes.object).isRequired, userGroup: PropTypes.object.isRequired, }; /** * Wrapper component for using unstated */ const UserGroupUserTableWrapper = (props) => { return createSubscribedElement(UserGroupUserTable, props, [AppContainer]); }; export default withTranslation()(UserGroupUserTableWrapper);