GrantSelector.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import React, { useCallback, useState } from 'react';
  2. import { isPopulated } from '@growi/core';
  3. import { useTranslation } from 'next-i18next';
  4. import {
  5. UncontrolledDropdown,
  6. DropdownToggle, DropdownMenu, DropdownItem,
  7. Modal, ModalHeader, ModalBody,
  8. } from 'reactstrap';
  9. import { IUserGroupHasId } from '~/interfaces/user';
  10. import { useCurrentUser } from '~/stores/context';
  11. import { useSWRxMyUserGroupRelations } from '~/stores/user-group';
  12. const AVAILABLE_GRANTS = [
  13. {
  14. grant: 1, iconClass: 'icon-people', btnStyleClass: 'outline-info', label: 'Public',
  15. },
  16. {
  17. grant: 2, iconClass: 'icon-link', btnStyleClass: 'outline-teal', label: 'Anyone with the link',
  18. },
  19. // { grant: 3, iconClass: '', label: 'Specified users only' },
  20. {
  21. grant: 4, iconClass: 'icon-lock', btnStyleClass: 'outline-danger', label: 'Only me',
  22. },
  23. {
  24. grant: 5, iconClass: 'icon-options', btnStyleClass: 'outline-purple', label: 'Only inside the group', reselectLabel: 'Reselect the group',
  25. },
  26. ];
  27. type Props = {
  28. disabled?: boolean,
  29. grant: number,
  30. grantGroupId?: string,
  31. grantGroupName?: string,
  32. onUpdateGrant?: (args: { grant: number, grantGroupId?: string | null, grantGroupName?: string | null }) => void,
  33. }
  34. /**
  35. * Page grant select component
  36. */
  37. const GrantSelector = (props: Props): JSX.Element => {
  38. const { t } = useTranslation();
  39. const {
  40. disabled,
  41. grantGroupName,
  42. onUpdateGrant,
  43. grant: currentGrant,
  44. grantGroupId,
  45. } = props;
  46. const [isSelectGroupModalShown, setIsSelectGroupModalShown] = useState(false);
  47. const { data: currentUser } = useCurrentUser();
  48. const shouldFetch = isSelectGroupModalShown;
  49. const { data: myUserGroupRelations, mutate: mutateMyUserGroupRelations } = useSWRxMyUserGroupRelations(shouldFetch);
  50. const showSelectGroupModal = useCallback(() => {
  51. mutateMyUserGroupRelations();
  52. setIsSelectGroupModalShown(true);
  53. }, [mutateMyUserGroupRelations]);
  54. /**
  55. * change event handler for grant selector
  56. */
  57. const changeGrantHandler = useCallback((grant: number) => {
  58. // select group
  59. if (grant === 5) {
  60. showSelectGroupModal();
  61. return;
  62. }
  63. if (onUpdateGrant != null) {
  64. onUpdateGrant({ grant, grantGroupId: null, grantGroupName: null });
  65. }
  66. }, [onUpdateGrant, showSelectGroupModal]);
  67. const groupListItemClickHandler = useCallback((grantGroup: IUserGroupHasId) => {
  68. if (onUpdateGrant != null) {
  69. onUpdateGrant({ grant: 5, grantGroupId: grantGroup._id, grantGroupName: grantGroup.name });
  70. }
  71. // hide modal
  72. setIsSelectGroupModalShown(false);
  73. }, [onUpdateGrant]);
  74. /**
  75. * Render grant selector DOM.
  76. */
  77. const renderGrantSelector = useCallback(() => {
  78. let dropdownToggleBtnColor;
  79. let dropdownToggleLabelElm;
  80. const dropdownMenuElems = AVAILABLE_GRANTS.map((opt) => {
  81. const label = ((opt.grant === 5 && opt.reselectLabel != null) && grantGroupId != null)
  82. ? opt.reselectLabel // when grantGroup is selected
  83. : opt.label;
  84. const labelElm = (
  85. <span>
  86. <i className={`icon icon-fw ${opt.iconClass}`}></i>
  87. <span className="label">{t(label)}</span>
  88. </span>
  89. );
  90. // set dropdownToggleBtnColor, dropdownToggleLabelElm
  91. if (opt.grant === 1 || opt.grant === currentGrant) {
  92. dropdownToggleBtnColor = opt.btnStyleClass;
  93. dropdownToggleLabelElm = labelElm;
  94. }
  95. return <DropdownItem key={opt.grant} onClick={() => changeGrantHandler(opt.grant)}>{labelElm}</DropdownItem>;
  96. });
  97. // add specified group option
  98. if (grantGroupId != null) {
  99. const labelElm = (
  100. <span>
  101. <i className="icon icon-fw icon-organization"></i>
  102. <span className="label">{grantGroupName}</span>
  103. </span>
  104. );
  105. // set dropdownToggleLabelElm
  106. dropdownToggleLabelElm = labelElm;
  107. dropdownMenuElems.push(<DropdownItem key="groupSelected">{labelElm}</DropdownItem>);
  108. }
  109. return (
  110. <div className="form-group grw-grant-selector mb-0">
  111. <UncontrolledDropdown direction="up">
  112. <DropdownToggle color={dropdownToggleBtnColor} caret className="d-flex justify-content-between align-items-center" disabled={disabled}>
  113. {dropdownToggleLabelElm}
  114. </DropdownToggle>
  115. <DropdownMenu>
  116. {dropdownMenuElems}
  117. </DropdownMenu>
  118. </UncontrolledDropdown>
  119. </div>
  120. );
  121. }, [changeGrantHandler, currentGrant, disabled, grantGroupId, grantGroupName, t]);
  122. /**
  123. * Render select grantgroup modal.
  124. */
  125. const renderSelectGroupModalContent = useCallback(() => {
  126. if (!shouldFetch) {
  127. return <></>;
  128. }
  129. // show spinner
  130. if (myUserGroupRelations == null) {
  131. return (
  132. <div className="my-3 text-center">
  133. <i className="fa fa-lg fa-spinner fa-pulse mx-auto text-muted"></i>
  134. </div>
  135. );
  136. }
  137. // extract IUserGroupHasId
  138. const userRelatedGroups: IUserGroupHasId[] = myUserGroupRelations
  139. .map((relation) => {
  140. // relation.relatedGroup should be populated by server
  141. return isPopulated(relation.relatedGroup) ? relation.relatedGroup : undefined;
  142. })
  143. // exclude undefined elements
  144. .filter((elem): elem is IUserGroupHasId => elem != null);
  145. if (userRelatedGroups.length === 0) {
  146. return (
  147. <div>
  148. <h4>{t('user_group.belonging_to_no_group')}</h4>
  149. { currentUser?.admin && (
  150. <p><a href="/admin/user-groups"><i className="icon icon-fw icon-login"></i>{t('user_group.manage_user_groups')}</a></p>
  151. ) }
  152. </div>
  153. );
  154. }
  155. return (
  156. <div className="list-group">
  157. { userRelatedGroups.map((group) => {
  158. return (
  159. <button key={group._id} type="button" className="list-group-item list-group-item-action" onClick={() => groupListItemClickHandler(group)}>
  160. <h5>{group.name}</h5>
  161. {/* TODO: Replace <div className="small">(TBD) List group members</div> */}
  162. </button>
  163. );
  164. }) }
  165. </div>
  166. );
  167. }, [currentUser?.admin, groupListItemClickHandler, myUserGroupRelations, shouldFetch, t]);
  168. return (
  169. <>
  170. { renderGrantSelector() }
  171. {/* render modal */}
  172. { !disabled && currentUser != null && (
  173. <Modal
  174. className="select-grant-group"
  175. isOpen={isSelectGroupModalShown}
  176. toggle={() => setIsSelectGroupModalShown(false)}
  177. >
  178. <ModalHeader tag="h4" toggle={() => setIsSelectGroupModalShown(false)} className="bg-purple text-light">
  179. {t('user_group.select_group')}
  180. </ModalHeader>
  181. <ModalBody>
  182. {renderSelectGroupModalContent()}
  183. </ModalBody>
  184. </Modal>
  185. ) }
  186. </>
  187. );
  188. };
  189. export default GrantSelector;