UserGroupModal.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import React, {
  2. FC, useState, useEffect, useCallback,
  3. } from 'react';
  4. import type { Ref, IUserGroup, IUserGroupHasId } from '@growi/core';
  5. import { useTranslation } from 'next-i18next';
  6. import {
  7. Modal, ModalHeader, ModalBody, ModalFooter,
  8. } from 'reactstrap';
  9. type Props = {
  10. userGroup?: IUserGroupHasId,
  11. buttonLabel?: string,
  12. onClickSubmit?: (userGroupData: Partial<IUserGroupHasId>) => Promise<IUserGroupHasId | void>
  13. isShow?: boolean
  14. onHide?: () => Promise<void> | void
  15. isExternalGroup?: boolean
  16. };
  17. export const UserGroupModal: FC<Props> = (props: Props) => {
  18. const { t } = useTranslation('admin');
  19. const {
  20. userGroup, buttonLabel, onClickSubmit, isShow, onHide, isExternalGroup = false,
  21. } = props;
  22. /*
  23. * State
  24. */
  25. const [currentName, setName] = useState('');
  26. const [currentDescription, setDescription] = useState('');
  27. const [currentParent, setParent] = useState<Ref<IUserGroup> | null>(null);
  28. /*
  29. * Function
  30. */
  31. const onChangeNameHandler = useCallback((e) => {
  32. setName(e.target.value);
  33. }, []);
  34. const onChangeDescriptionHandler = useCallback((e) => {
  35. setDescription(e.target.value);
  36. }, []);
  37. const onSubmitHandler = useCallback(async(e) => {
  38. e.preventDefault(); // no reload
  39. if (onClickSubmit == null) {
  40. return;
  41. }
  42. await onClickSubmit({
  43. _id: userGroup?._id,
  44. name: currentName,
  45. description: currentDescription,
  46. parent: currentParent,
  47. });
  48. }, [userGroup, currentName, currentDescription, currentParent, onClickSubmit]);
  49. // componentDidMount
  50. useEffect(() => {
  51. if (userGroup != null) {
  52. setName(userGroup.name);
  53. setDescription(userGroup.description);
  54. setParent(userGroup.parent);
  55. }
  56. }, [userGroup]);
  57. return (
  58. <Modal className="modal-md" isOpen={isShow} toggle={onHide}>
  59. <form onSubmit={onSubmitHandler}>
  60. <ModalHeader tag="h4" toggle={onHide} className="bg-primary text-light">
  61. {t('user_group_management.basic_info')}
  62. </ModalHeader>
  63. <ModalBody>
  64. <div className="form-group">
  65. <label htmlFor="name">
  66. {t('user_group_management.group_name')}
  67. </label>
  68. <input
  69. className="form-control"
  70. type="text"
  71. name="name"
  72. placeholder={t('user_group_management.group_example')}
  73. value={currentName}
  74. onChange={onChangeNameHandler}
  75. required
  76. disabled={isExternalGroup}
  77. />
  78. </div>
  79. <div className="form-group">
  80. <label htmlFor="description">
  81. {t('Description')}
  82. </label>
  83. <textarea className="form-control" name="description" value={currentDescription} onChange={onChangeDescriptionHandler} />
  84. {isExternalGroup && (
  85. <p className="form-text text-muted">
  86. <small>
  87. {t('external_user_group.description_form_detail')}
  88. </small>
  89. </p>
  90. )}
  91. </div>
  92. {/* TODO 90732: Add a drop-down to show selectable parents */}
  93. {/* TODO 85462: Add a note that "if you change the parent, the offspring will also be moved together */}
  94. </ModalBody>
  95. <ModalFooter>
  96. <div className="form-group">
  97. <button type="submit" className="btn btn-primary">
  98. {buttonLabel}
  99. </button>
  100. </div>
  101. </ModalFooter>
  102. </form>
  103. </Modal>
  104. );
  105. };