DepartmentModal.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React, { useEffect, useState } from 'react';
  2. import { Modal, Button, Input, message, Form } from 'antd';
  3. import { RootState, useAppDispatch, useAppSelector } from '@/states/store';
  4. import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
  5. import { createdDepartment, getDepartment } from '@/API/report/ReportActions';
  6. import useIntl from 'react-intl/src/components/useIntl';
  7. import { setDepartments } from '@/states/patient/DiagnosticReport/departmentSlice';
  8. interface DepartmentModalProps {
  9. visible: boolean;
  10. onClose: () => void;
  11. }
  12. export const DepartmentModal: React.FC<DepartmentModalProps> = ({
  13. visible,
  14. onClose,
  15. }) => {
  16. const intl = useIntl();
  17. const dispatch = useAppDispatch();
  18. const [form] = Form.useForm();
  19. const [submitting, setSubmitting] = useState(false);
  20. const departments = useAppSelector(
  21. (state: RootState) => state.department.departments
  22. );
  23. const getDepartmentData = async () => {
  24. const res = await getDepartment({});
  25. if (res.code === '0x000000') {
  26. dispatch(setDepartments(res?.data?.departments || []));
  27. }
  28. };
  29. const onFinish = async (values: { departments: string[] }) => {
  30. // 防止重复提交
  31. if (submitting) return;
  32. setSubmitting(true);
  33. try {
  34. const res = await createdDepartment(values);
  35. if (res.code === '0x000000') {
  36. await getDepartmentData();
  37. onClose();
  38. message.success(
  39. intl.formatMessage({ id: 'report.department.saveSuccess' })
  40. );
  41. } else {
  42. message.error(intl.formatMessage({ id: 'report.department.saveFail' }));
  43. }
  44. } catch {
  45. message.error(intl.formatMessage({ id: 'report.department.saveFail' }));
  46. } finally {
  47. setSubmitting(false);
  48. }
  49. };
  50. useEffect(() => {
  51. if (visible) {
  52. form.setFieldsValue({
  53. departments: departments.map((k) => k.department),
  54. });
  55. }
  56. }, [visible]);
  57. return (
  58. <Modal
  59. title={intl.formatMessage({
  60. id: 'report.department.title',
  61. })}
  62. centered
  63. closable={false}
  64. open={visible}
  65. maskClosable={false}
  66. destroyOnHidden
  67. onCancel={onClose}
  68. okText={intl.formatMessage({ id: 'report.department.okButtonText' })}
  69. cancelText={intl.formatMessage({
  70. id: 'report.department.cancelButtonText',
  71. })}
  72. styles={{ body: { maxHeight: '50vh', overflow: 'auto' } }}
  73. okButtonProps={{
  74. autoFocus: true,
  75. htmlType: 'submit',
  76. loading: submitting,
  77. style: { marginRight: '50px' },
  78. }}
  79. modalRender={(dom) => (
  80. <Form layout="vertical" form={form} clearOnDestroy onFinish={onFinish}>
  81. {dom}
  82. </Form>
  83. )}
  84. >
  85. <Form.List name="departments">
  86. {(fields, { add, remove }) => (
  87. <>
  88. {fields.map((field) => (
  89. <Form.Item label={''} required={false} key={field.key}>
  90. <Form.Item
  91. {...field}
  92. validateTrigger={['onChange', 'onBlur']}
  93. rules={[
  94. {
  95. required: true,
  96. whitespace: true,
  97. message: intl.formatMessage({
  98. id: 'report.department.inputEmptyCheck',
  99. }),
  100. },
  101. ]}
  102. noStyle
  103. >
  104. <Input
  105. placeholder={intl.formatMessage({
  106. id: 'report.department.placeholder',
  107. })}
  108. style={{ width: '90%' }}
  109. />
  110. </Form.Item>
  111. <MinusCircleOutlined
  112. style={{
  113. position: 'relative',
  114. top: '4px',
  115. margin: '0 8px',
  116. color: '#999',
  117. fontSize: '24px',
  118. cursor: 'pointer',
  119. }}
  120. onClick={() => remove(field.name)}
  121. />
  122. </Form.Item>
  123. ))}
  124. <Form.Item>
  125. <Button
  126. // disabled={fields.length >= 6}
  127. type="dashed"
  128. onClick={() => add()}
  129. style={{ width: '90%' }}
  130. icon={<PlusOutlined />}
  131. >
  132. {intl.formatMessage({ id: 'report.department.addButtonText' })}
  133. </Button>
  134. </Form.Item>
  135. </>
  136. )}
  137. </Form.List>
  138. </Modal>
  139. );
  140. };