saveMaterial.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { Button, Card, Form, message, Modal, Space, TreeSelect } from "antd"
  2. import React, { useEffect, useState } from "react"
  3. import '../../tencentAdPutIn/index.less'
  4. import { addRemoteMaterialApi, getFolderListApi } from "@/services/adqV3/cloudNew"
  5. import { useAjax } from "@/Hook/useAjax"
  6. import { DataNode } from "antd/lib/tree"
  7. import { updateTreeData } from "../cloudNew/const"
  8. interface Props {
  9. data: any[]
  10. visible?: boolean
  11. onChange?: () => void
  12. onClose?: () => void
  13. }
  14. /**
  15. * 移动至素材库
  16. * @returns
  17. */
  18. const SaveMaterial: React.FC<Props> = ({ data, visible, onChange, onClose }) => {
  19. /*************************************/
  20. const [treeData, setTreeData] = useState<DataNode[]>([]);
  21. const [form] = Form.useForm();
  22. const getFolderList = useAjax((params) => getFolderListApi(params))
  23. const addRemoteMaterial = useAjax((params) => addRemoteMaterialApi(params))
  24. /*************************************/
  25. const handleOk = (values: any) => {
  26. console.log(values)
  27. addRemoteMaterial.run({
  28. ...values,
  29. data: data.map(item => ({
  30. aspectRatio: item.aspect_ratio,
  31. description: item.description,
  32. designerId: localStorage.getItem('userId'),
  33. fileSize: item.file_size,
  34. height: item.height,
  35. width: item.width,
  36. materialName: item.description,
  37. materialType: item.source,
  38. md5: item.signature,
  39. url: item.preview_url,
  40. }))
  41. }).then(res => {
  42. if (res) {
  43. message.success('任务创建成功,结果要几分钟后返回')
  44. onChange?.()
  45. }
  46. })
  47. }
  48. useEffect(() => {
  49. getFolder()
  50. }, [])
  51. const getFolder = () => {
  52. getFolderList.run({}).then(res => {
  53. setTreeData(() => res?.map((item: { folderName: any; id: any; createBy: number }) => ({
  54. title: item.folderName,
  55. value: item.id,
  56. key: item.id,
  57. disabled: localStorage.getItem('userId') !== item.createBy.toString()
  58. })) || [])
  59. })
  60. }
  61. // 下级目录
  62. const handleUpdateFolder = (parentId: number) => {
  63. return getFolderListApi({ parentId }).then(res => {
  64. setTreeData(origin =>
  65. updateTreeData(origin, parentId, res?.data?.map((item: { folderName: any; id: any; createBy: number }) => ({
  66. title: item.folderName,
  67. value: item.id,
  68. key: item.id,
  69. disabled: localStorage.getItem('userId') !== item.createBy.toString()
  70. })) || []),
  71. );
  72. })
  73. }
  74. return <Modal
  75. title={<strong>{'保存至素材库'}</strong>}
  76. open={visible}
  77. onCancel={onClose}
  78. footer={null}
  79. className="modalResetCss"
  80. bodyStyle={{ padding: '0 0 40px', position: 'relative', borderRadius: '0 0 8px 8px' }}
  81. maskClosable={false}
  82. width={600}
  83. >
  84. <Form
  85. form={form}
  86. name="newFolder"
  87. labelAlign='left'
  88. labelCol={{ span: 4 }}
  89. layout="horizontal"
  90. colon={false}
  91. style={{ backgroundColor: '#f1f4fc', maxHeight: 650, overflow: 'hidden', overflowY: 'auto', padding: '10px', borderRadius: '0 0 8px 8px' }}
  92. scrollToFirstError={{
  93. behavior: 'smooth',
  94. block: 'center'
  95. }}
  96. onFinishFailed={({ errorFields }) => {
  97. message.error(errorFields?.[0]?.errors?.[0])
  98. }}
  99. onFinish={handleOk}
  100. >
  101. <Card className="cardResetCss">
  102. <Form.Item label={<strong>文件夹</strong>} name={'folderId'} rules={[{ required: true, message: '请选择文件夹' }]}>
  103. <TreeSelect
  104. loading={getFolderList.loading}
  105. allowClear
  106. style={{ width: '100%' }}
  107. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  108. placeholder="请选择文件夹"
  109. loadData={({ value }) => {
  110. return new Promise<void>(async (resolve) => {
  111. await handleUpdateFolder(Number(value))
  112. resolve()
  113. })
  114. }}
  115. treeData={treeData}
  116. />
  117. </Form.Item>
  118. </Card>
  119. <Form.Item className="submit_pull">
  120. <Space>
  121. <Button onClick={onClose}>取消</Button>
  122. <Button type="primary" htmlType="submit" loading={addRemoteMaterial.loading} className="modalResetCss">
  123. 确定
  124. </Button>
  125. </Space>
  126. </Form.Item>
  127. </Form>
  128. </Modal>
  129. }
  130. export default React.memo(SaveMaterial)