index.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import Tables from "@/components/Tables"
  2. import { useAjax } from "@/Hook/useAjax"
  3. import { getDataSourceApi, sysDataSourceApi } from "@/services/launchAdq/createAd"
  4. import { CheckOutlined, SyncOutlined } from "@ant-design/icons"
  5. import { Button, Modal, Space } from "antd"
  6. import React, { useEffect, useState } from "react"
  7. import style from '../goodsModal/index.less'
  8. import columns from './tableConfig'
  9. /**
  10. * 获取数据源
  11. * @returns
  12. */
  13. interface Props {
  14. visible?: boolean,
  15. onClose?: () => void,
  16. onChange?: (data: any) => void,
  17. data: any
  18. }
  19. const DataSourceModal: React.FC<Props> = (props) => {
  20. /************************/
  21. const { visible, onClose, data: data1, onChange } = props
  22. const [tableData, setTableData] = useState<any[]>([])//table数据
  23. const [selectAdz, setSelectAdz] = useState<number>(1) // 选择广告主
  24. const [data, setData] = useState<any>(data1)
  25. const getDataSource = useAjax((params) => getDataSourceApi(params))
  26. const sysDataSource = useAjax((params) => sysDataSourceApi(params))
  27. /************************/
  28. useEffect(() => {
  29. if (data?.length > 0) {
  30. getList([data[selectAdz - 1].adAccountId])
  31. } else {
  32. setTableData([])
  33. }
  34. }, [selectAdz])
  35. // 获取数据源
  36. const getList = (params: number[]) => {
  37. getDataSource.run(params).then(res => {
  38. console.log('res===>', res)
  39. if (res && Object.keys(res)?.indexOf(params[0].toString()) !== -1) {
  40. setTableData(res[params[0]]?.map((item: { userActionSetId: string }) => ({ ...item, id: item?.userActionSetId })))
  41. } else {
  42. setTableData([])
  43. }
  44. })
  45. }
  46. const handleOk = () => {
  47. onChange && onChange(data)
  48. }
  49. // 同步数据源
  50. const synDataSourceList = () => {
  51. sysDataSource.run(data?.map((item: { id: number }) => item?.id)).then(res => {
  52. getList([data[selectAdz - 1].adAccountId])
  53. })
  54. }
  55. /** 设置选中广告主 */
  56. const handleSelectAdz = (value: number, item: any) => {
  57. if (value === selectAdz) {
  58. return
  59. }
  60. setSelectAdz(value)
  61. }
  62. /** 表格选折 */
  63. const onChangeTable = (selectedRowKeys: React.Key[], selectedRows: any) => {
  64. let newData = JSON.parse(JSON.stringify(data))
  65. newData[selectAdz - 1]['userActionSetsList'] = selectedRows
  66. setData([...newData])
  67. }
  68. return <Modal
  69. title={<Space>
  70. <span>数据源</span>
  71. <Button size="small" onClick={() => { synDataSourceList() }} loading={sysDataSource?.loading}>同步数据源</Button>
  72. </Space>}
  73. visible={visible}
  74. onCancel={() => { onClose && onClose() }}
  75. onOk={handleOk}
  76. width={1100}
  77. className={style.SelectPackage}
  78. bodyStyle={{ padding: '0 10px 0 10px' }}
  79. >
  80. <div className={style.content}>
  81. <div className={style.left}>
  82. <h4 className={style.title}>广告主账户</h4>
  83. {data?.map((item: { adAccountId: number, id: number }, index: number) => (
  84. <div key={index} onClick={() => { handleSelectAdz(index + 1, item) }} className={`${style.accItem} ${selectAdz === index + 1 && style.select} `}>
  85. {item?.adAccountId}
  86. {data[index].userActionSetsList?.length > 0 && <CheckOutlined style={{ color: '#1890ff' }} />}
  87. </div>))}
  88. </div>
  89. <div className={style.right}>
  90. <Space style={{ marginBottom: 10 }}>
  91. <Button icon={<SyncOutlined />} type='link' loading={getDataSource?.loading} onClick={() => { getList([data[selectAdz - 1].adAccountId]) }}></Button>
  92. </Space>
  93. <Tables
  94. columns={columns()}
  95. dataSource={tableData}
  96. size="small"
  97. loading={getDataSource?.loading}
  98. scroll={{ y: 300 }}
  99. bordered
  100. rowSelection={{
  101. type: 'checkbox',
  102. selectedRowKeys: data[selectAdz - 1]?.userActionSetsList?.map((item: any) => item?.id?.toString()),
  103. onChange: onChangeTable
  104. }}
  105. />
  106. </div>
  107. </div>
  108. </Modal>
  109. }
  110. export default React.memo(DataSourceModal)