GrantedGroupsInheritanceSelectModal.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { useCallback, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
  4. import {
  5. useGrantedGroupsInheritanceSelectModalActions,
  6. useGrantedGroupsInheritanceSelectModalStatus,
  7. } from '~/states/ui/modal/granted-groups-inheritance-select';
  8. /**
  9. * GrantedGroupsInheritanceSelectModalSubstance - Presentation component (heavy logic, rendered only when isOpen)
  10. */
  11. type GrantedGroupsInheritanceSelectModalSubstanceProps = {
  12. onCreateBtnClick:
  13. | ((onlyInheritUserRelatedGrantedGroups: boolean) => Promise<void>)
  14. | undefined;
  15. closeModal: () => void;
  16. };
  17. const GrantedGroupsInheritanceSelectModalSubstance = (
  18. props: GrantedGroupsInheritanceSelectModalSubstanceProps,
  19. ): React.JSX.Element => {
  20. const { onCreateBtnClick: _onCreateBtnClick, closeModal } = props;
  21. const { t } = useTranslation();
  22. const [
  23. onlyInheritUserRelatedGrantedGroups,
  24. setOnlyInheritUserRelatedGrantedGroups,
  25. ] = useState(false);
  26. const onCreateBtnClick = useCallback(async () => {
  27. await _onCreateBtnClick?.(onlyInheritUserRelatedGrantedGroups);
  28. setOnlyInheritUserRelatedGrantedGroups(false); // reset to false after create request
  29. }, [_onCreateBtnClick, onlyInheritUserRelatedGrantedGroups]);
  30. const setInheritAll = useCallback(
  31. () => setOnlyInheritUserRelatedGrantedGroups(false),
  32. [],
  33. );
  34. const setInheritRelatedOnly = useCallback(
  35. () => setOnlyInheritUserRelatedGrantedGroups(true),
  36. [],
  37. );
  38. return (
  39. <>
  40. <ModalHeader tag="h4" toggle={() => closeModal()}>
  41. {t('modal_granted_groups_inheritance_select.select_granted_groups')}
  42. </ModalHeader>
  43. <ModalBody>
  44. <div className="px-3 pt-3">
  45. <div className="form-check radio-primary mb-3">
  46. <input
  47. type="radio"
  48. id="inheritAllGroupsRadio"
  49. className="form-check-input"
  50. form="formImageType"
  51. checked={!onlyInheritUserRelatedGrantedGroups}
  52. onChange={setInheritAll}
  53. />
  54. <label className="form-check-label" htmlFor="inheritAllGroupsRadio">
  55. {t(
  56. 'modal_granted_groups_inheritance_select.inherit_all_granted_groups_from_parent',
  57. )}
  58. </label>
  59. </div>
  60. <div className="form-check radio-primary">
  61. <input
  62. type="radio"
  63. id="onlyInheritRelatedGroupsRadio"
  64. className="form-check-input"
  65. form="formImageType"
  66. checked={onlyInheritUserRelatedGrantedGroups}
  67. onChange={setInheritRelatedOnly}
  68. />
  69. <label
  70. className="form-check-label"
  71. htmlFor="onlyInheritRelatedGroupsRadio"
  72. >
  73. {t(
  74. 'modal_granted_groups_inheritance_select.only_inherit_related_groups',
  75. )}
  76. </label>
  77. </div>
  78. </div>
  79. </ModalBody>
  80. <ModalFooter className="grw-modal-footer">
  81. <button
  82. type="button"
  83. className="me-2 btn btn-secondary"
  84. onClick={() => closeModal()}
  85. >
  86. {t('Cancel')}
  87. </button>
  88. <button
  89. className="btn btn-primary"
  90. type="button"
  91. onClick={onCreateBtnClick}
  92. >
  93. {t('modal_granted_groups_inheritance_select.create_page')}
  94. </button>
  95. </ModalFooter>
  96. </>
  97. );
  98. };
  99. /**
  100. * GrantedGroupsInheritanceSelectModal - Container component (lightweight, always rendered)
  101. */
  102. export const GrantedGroupsInheritanceSelectModal = (): React.JSX.Element => {
  103. const { isOpened, onCreateBtnClick } =
  104. useGrantedGroupsInheritanceSelectModalStatus();
  105. const { close: closeModal } = useGrantedGroupsInheritanceSelectModalActions();
  106. return (
  107. <Modal isOpen={isOpened} toggle={() => closeModal()}>
  108. {isOpened && (
  109. <GrantedGroupsInheritanceSelectModalSubstance
  110. onCreateBtnClick={onCreateBtnClick}
  111. closeModal={closeModal}
  112. />
  113. )}
  114. </Modal>
  115. );
  116. };