UserGroupModal.tsx 3.4 KB

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