GrantedGroupsInheritanceSelectModal.tsx 3.8 KB

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