index.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React, { useEffect, useState } from 'react'
  2. import { Modal, Form, Input, Button, Select } from 'antd'
  3. import { I1_APIwarehouseSave } from '@/store/action/Isystem/I1storageSet'
  4. import MyPopconfirm from '@/components/MyPopconfirm'
  5. import { useDispatch, useSelector } from 'react-redux'
  6. import { RootState } from '@/store'
  7. import { getUserListAPI } from '@/store/action/Isystem/I7user'
  8. interface AddWarehouseModalProps {
  9. visible: boolean
  10. onCancel: () => void
  11. onSuccess: () => void
  12. editData?: any
  13. }
  14. const AddWarehouseModal: React.FC<AddWarehouseModalProps> = ({
  15. visible,
  16. onCancel,
  17. onSuccess,
  18. editData
  19. }) => {
  20. const dispatch = useDispatch()
  21. const [loading, setLoading] = useState(false)
  22. const [form] = Form.useForm()
  23. const userInfo = useSelector((state: RootState) => state.I7user.tableInfo)
  24. const handleOk = async () => {
  25. try {
  26. const values = await form.validateFields()
  27. setLoading(true)
  28. const payload = editData ? { ...values, id: editData.id } : values
  29. const res = await I1_APIwarehouseSave(payload)
  30. setLoading(false)
  31. if (res.code === 0) {
  32. onSuccess()
  33. form.resetFields()
  34. }
  35. } catch (err) {
  36. setLoading(false)
  37. }
  38. }
  39. useEffect(() => {
  40. if (!visible) return
  41. dispatch(
  42. getUserListAPI({
  43. pageNum: 1,
  44. pageSize: 9999
  45. })
  46. )
  47. if (editData) {
  48. form.setFieldsValue({
  49. name: editData.name,
  50. managerUserId: editData.managerUserId,
  51. description: editData.description
  52. })
  53. } else {
  54. form.resetFields()
  55. }
  56. }, [dispatch, userInfo.list.length, visible, editData, form])
  57. return (
  58. <Modal
  59. title={editData ? '编辑' : '新增'}
  60. open={visible}
  61. onCancel={() => {
  62. form.resetFields()
  63. onCancel()
  64. }}
  65. footer={null}
  66. destroyOnClose
  67. >
  68. <Form form={form} labelCol={{ span: 5 }}>
  69. <Form.Item
  70. label='库房名称'
  71. name='name'
  72. rules={[{ required: true, message: '请输入库房名称' }]}
  73. >
  74. <Input placeholder='请输入库房名称' />
  75. </Form.Item>
  76. <Form.Item label='库房负责人' name='managerUserId'>
  77. <Select
  78. placeholder='请选择用户'
  79. options={userInfo.list.map((i: any) => ({ label: i.realName, value: i.id }))}
  80. />
  81. </Form.Item>
  82. <Form.Item
  83. label='库房说明'
  84. name='description'
  85. rules={[{ max: 500, message: '库房说明不能超过500字' }]}
  86. >
  87. <Input.TextArea placeholder='请输入库房说明(不超过500字)' maxLength={500} rows={4} />
  88. </Form.Item>
  89. <Form.Item wrapperCol={{ offset: 9, span: 16 }}>
  90. <Button type='primary' loading={loading} onClick={handleOk} style={{ marginRight: 8 }}>
  91. 提交
  92. </Button>
  93. <MyPopconfirm
  94. txtK='取消'
  95. onConfirm={() => {
  96. form.resetFields()
  97. onCancel()
  98. }}
  99. />
  100. </Form.Item>
  101. </Form>
  102. </Modal>
  103. )
  104. }
  105. export default AddWarehouseModal