group-users.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import classNames from 'classnames'
  2. import moment from 'moment'
  3. import { useMemo, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { User } from '@ol-types/group-management/user'
  6. import MaterialIcon from '@/shared/components/material-icon'
  7. import OLButton from '@/shared/components/ol/ol-button'
  8. import OLCard from '@/shared/components/ol/ol-card'
  9. import OLCol from '@/shared/components/ol/ol-col'
  10. import OLFormCheckbox from '@/shared/components/ol/ol-form-checkbox'
  11. import OLFormSelect from '@/shared/components/ol/ol-form-select'
  12. import OLNotification from '@/shared/components/ol/ol-notification'
  13. import OLRow from '@/shared/components/ol/ol-row'
  14. import OLTable from '@/shared/components/ol/ol-table'
  15. import OLTag from '@/shared/components/ol/ol-tag'
  16. import getMeta from '@/utils/meta'
  17. import BackButton from './back-button'
  18. import { useGroupMembersContext } from '../context/group-members-context'
  19. const getUserRole = (user: User) =>
  20. user.isEntityAdmin ? 'admin' : user.isEntityManager ? 'manager' : 'member'
  21. export default function GroupUsers() {
  22. const { t } = useTranslation()
  23. const groupName = getMeta('ol-groupName')
  24. const groupId = getMeta('ol-groupId')
  25. const groupSize = getMeta('ol-groupSize')
  26. const canUseAddSeatsFeature = getMeta('ol-canUseAddSeatsFeature')
  27. const groupSSOActive = getMeta('ol-groupSSOActive')
  28. const managedUsersActive = getMeta('ol-managedUsersActive')
  29. const {
  30. users,
  31. // selectedUsers,
  32. // addMembers,
  33. // removeMembers,
  34. // removeMemberLoading,
  35. // removeMemberError,
  36. // inviteMemberLoading,
  37. // inviteError,
  38. // memberAdded,
  39. // paths,
  40. } = useGroupMembersContext()
  41. const [page, setPage] = useState(1)
  42. const numPages = Math.ceil(users.length / 10)
  43. const paginatedUsers = useMemo(() => {
  44. const firstUser = (page - 1) * 10
  45. const lastUser = Math.min(page * 10, users.length)
  46. return users.slice(firstUser, lastUser)
  47. }, [users, page])
  48. const addedUsersSize = users.filter(user => user.isEntityMember).length
  49. return (
  50. <div className="container group-users-container">
  51. <OLRow>
  52. <OLCol>
  53. <div className="group-heading">
  54. <BackButton
  55. href="/user/subscription"
  56. accessibilityLabel={t('back_to_subscription')}
  57. />
  58. <h1 className="heading">{groupName || t('group_subscription')}</h1>
  59. </div>
  60. </OLCol>
  61. </OLRow>
  62. <OLCard>
  63. <OLRow className="justify-content-between">
  64. <OLCol xs="auto">
  65. <h2 className="page-title">{t('user_management')}</h2>
  66. <a href="/" className="learn-more">
  67. {t('learn_more_about_roles_permissions')}
  68. </a>
  69. </OLCol>
  70. <OLCol xs="auto" className="align-content-center">
  71. <OLButton>{t('invite_users')}</OLButton>
  72. </OLCol>
  73. </OLRow>
  74. <OLRow className="license-info">
  75. <OLNotification
  76. type="info"
  77. content={
  78. users.length === 1
  79. ? t('you_have_1_license_and_your_plan_supports_up_to_y', {
  80. groupSize,
  81. })
  82. : t('you_have_x_licenses_and_your_plan_supports_up_to_y', {
  83. addedUsersSize,
  84. groupSize,
  85. })
  86. }
  87. action={
  88. canUseAddSeatsFeature ? (
  89. <a
  90. href="/user/subscription/group/add-users"
  91. className={classNames({
  92. 'btn btn-premium': addedUsersSize === groupSize,
  93. })}
  94. >
  95. {t('buy_more_licenses')}
  96. </a>
  97. ) : undefined
  98. }
  99. />
  100. </OLRow>
  101. <OLRow>
  102. <OLCol>
  103. <OLTable hover responsive bordered>
  104. <thead>
  105. <tr>
  106. <th>
  107. <OLFormCheckbox />
  108. </th>
  109. <th>{t('email')}</th>
  110. <th>{t('name')}</th>
  111. <th>{t('last_active')}</th>
  112. <th>{t('role')}</th>
  113. <th className="text-center">{t('license')}</th>
  114. {groupSSOActive && (
  115. <th className="text-center">{t('sso')}</th>
  116. )}
  117. {managedUsersActive && (
  118. <th className="text-center">{t('managed')}</th>
  119. )}
  120. <th />
  121. </tr>
  122. </thead>
  123. <tbody>
  124. {paginatedUsers.map(user => (
  125. <tr key={user.email} className="align-middle">
  126. <td>
  127. <OLFormCheckbox />
  128. </td>
  129. <td>{user.email}</td>
  130. <td className="text-nowrap">
  131. {user.first_name} {user.last_name}
  132. </td>
  133. <td className="text-nowrap">
  134. {user.invite ? (
  135. <OLTag>{t('pending_invite')}</OLTag>
  136. ) : (
  137. moment(user.last_active_at).format('Do MMM YYYY')
  138. )}
  139. </td>
  140. <td>
  141. <OLFormSelect
  142. aria-label={t('select_user_role')}
  143. name="user-role"
  144. defaultValue={getUserRole(user)}
  145. className="user-role-select"
  146. disabled
  147. >
  148. <option value="admin">{t('admin_titlecase')}</option>
  149. <option value="manager">{t('manager')}</option>
  150. <option value="member">{t('member')}</option>
  151. </OLFormSelect>
  152. </td>
  153. <td className="text-center">
  154. {user.isEntityMember ? (
  155. <MaterialIcon
  156. type="check"
  157. className="text-success"
  158. accessibilityLabel={t('license_allocated')}
  159. />
  160. ) : (
  161. <MaterialIcon
  162. type="close"
  163. className="text-danger"
  164. accessibilityLabel={t('license_not_allocated')}
  165. />
  166. )}
  167. </td>
  168. {groupSSOActive && (
  169. <td className="text-center">
  170. {user.enrollment?.sso?.some(
  171. sso => sso.groupId === groupId
  172. ) ? (
  173. <MaterialIcon
  174. type="check"
  175. className="text-success"
  176. accessibilityLabel={t('sso_active')}
  177. />
  178. ) : (
  179. <MaterialIcon
  180. type="close"
  181. className="text-danger"
  182. accessibilityLabel={t('sso_not_active')}
  183. />
  184. )}
  185. </td>
  186. )}
  187. {managedUsersActive && (
  188. <td className="text-center">
  189. {user.enrollment?.managedBy === groupId ? (
  190. <MaterialIcon
  191. type="check"
  192. className="text-success"
  193. accessibilityLabel={t('managed')}
  194. />
  195. ) : (
  196. <MaterialIcon
  197. type="close"
  198. className="text-danger"
  199. accessibilityLabel={t('not_managed')}
  200. />
  201. )}
  202. </td>
  203. )}
  204. <td>...</td>
  205. </tr>
  206. ))}
  207. </tbody>
  208. </OLTable>
  209. </OLCol>
  210. </OLRow>
  211. <OLRow className="justify-content-between align-items-center">
  212. <OLCol xs="auto" className="ms-3">
  213. <p className="mb-0">
  214. {t('showing_x_out_of_n_users', {
  215. x: paginatedUsers.length,
  216. n: users.length,
  217. })}
  218. </p>
  219. </OLCol>
  220. <OLCol xs="auto" className="pagination-container">
  221. <OLButton
  222. variant="ghost"
  223. disabled={page === 1}
  224. onClick={() => setPage(1)}
  225. >
  226. <MaterialIcon
  227. type="first_page"
  228. accessibilityLabel={t('go_to_first_page')}
  229. />
  230. </OLButton>
  231. <OLButton
  232. variant="ghost"
  233. disabled={page === 1}
  234. onClick={() => setPage(page => page - 1)}
  235. >
  236. <MaterialIcon
  237. type="chevron_left"
  238. accessibilityLabel={t('go_to_previous_page')}
  239. />
  240. </OLButton>
  241. <p className="d-inline-flex mb-0">
  242. {t('page_x_of_n', { x: page, n: numPages })}
  243. </p>
  244. <OLButton
  245. variant="ghost"
  246. disabled={page === numPages}
  247. onClick={() => setPage(page => page + 1)}
  248. >
  249. <MaterialIcon
  250. type="chevron_right"
  251. accessibilityLabel={t('go_to_next_page')}
  252. />
  253. </OLButton>
  254. <OLButton
  255. variant="ghost"
  256. disabled={page === numPages}
  257. onClick={() => setPage(numPages)}
  258. >
  259. <MaterialIcon
  260. type="last_page"
  261. accessibilityLabel={t('go_to_last_page')}
  262. />
  263. </OLButton>
  264. </OLCol>
  265. </OLRow>
  266. </OLCard>
  267. </div>
  268. )
  269. }