UserGroupModal.tsx 3.4 KB

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