AddMessage.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React, { useRef } from 'react'
  2. import { Button, Form, message } from 'antd'
  3. import { ModalForm, ProFormDependency, ProFormRadio, ProFormSelect, ProFormText } from '@ant-design/pro-form'
  4. import RighEditor from './RighEditor'
  5. import { Plus } from '@icon-park/react'
  6. export const msgTypeEnum = {
  7. NotificationOne: 'notification',
  8. Message: 'message'
  9. }
  10. export const mediaType = {
  11. VersionUpdate: 1,
  12. HolidayNotice: 2
  13. }
  14. const AddMessage = props => {
  15. const { onConfirm } = props
  16. const formRef = useRef(null)
  17. const handleFormValues = values => {
  18. const newValues = { ...values }
  19. switch (values.msgType) {
  20. case msgTypeEnum.NotificationOne:
  21. switch (values.media) {
  22. case mediaType.VersionUpdate:
  23. newValues.avatar =
  24. (isDevMode() ? 'http://cld2qa.com' : 'http://127.0.0.1') + '/resource/notice_email.png'
  25. break
  26. case mediaType.HolidayNotice:
  27. newValues.avatar =
  28. (isDevMode() ? 'http://cld2qa.com' : 'http://127.0.0.1') + '/resource/notice_airplane.png'
  29. break
  30. default:
  31. break
  32. }
  33. break
  34. default:
  35. break
  36. }
  37. return newValues
  38. }
  39. return (
  40. <ModalForm
  41. isKeyPressSubmit={false}
  42. formRef={formRef}
  43. trigger={
  44. <Button type="primary">
  45. <Plus className="mr-1" />
  46. 新建通知
  47. </Button>
  48. }
  49. initialValues={{ msgType: 'notification', refresh: 0 }}
  50. onFinish={async values => {
  51. try {
  52. const nValues = handleFormValues(values)
  53. await onConfirm(nValues)
  54. formRef.current?.resetFields()
  55. message.success('添加成功')
  56. return true
  57. } catch (error) {
  58. return false
  59. }
  60. }}>
  61. <ProFormText
  62. name="title"
  63. label="标题"
  64. placeholder="请输入标题"
  65. rules={[{ required: true, message: '请输入标题' }]}
  66. />
  67. <Form.Item label="内容" name="content" rules={[{ required: true, message: '请输入通知内容' }]}>
  68. <RighEditor />
  69. </Form.Item>
  70. <ProFormRadio.Group
  71. label="类型"
  72. name="msgType"
  73. required
  74. options={[
  75. { label: '通知', value: msgTypeEnum.NotificationOne }
  76. // { label: '消息', value: msgTypeEnum.Message }
  77. ]}
  78. />
  79. <ProFormDependency name={['msgType']}>
  80. {({ msgType }) =>
  81. msgType === msgTypeEnum.NotificationOne ? (
  82. <ProFormSelect
  83. name="media"
  84. label="通知类型"
  85. rules={[{ required: true, message: '请选择类型' }]}
  86. options={[
  87. { label: '版本更新', value: mediaType.VersionUpdate },
  88. { label: '节假通知', value: mediaType.HolidayNotice }
  89. ]}
  90. />
  91. ) : null
  92. }
  93. </ProFormDependency>
  94. <ProFormDependency name={['msgType', 'media']}>
  95. {({ msgType, media }) =>
  96. msgType === msgTypeEnum.NotificationOne && media === mediaType.VersionUpdate ? (
  97. <ProFormRadio.Group
  98. name="refresh"
  99. rules={[{ required: true, message: '请选择刷新机制' }]}
  100. options={[
  101. { label: '手动刷新', value: 0 },
  102. { label: '强制刷新', value: 1 }
  103. ]}
  104. />
  105. ) : null
  106. }
  107. </ProFormDependency>
  108. </ModalForm>
  109. )
  110. }
  111. export default AddMessage