HomePageConfigs.jsx 4.49 KB
Newer Older
邓超's avatar
邓超 committed
1 2 3 4 5 6 7 8 9
import React, { useState, useRef, forwardRef, useImperativeHandle, useEffect } from 'react';
import DragTable from '@/components/DragTable/DragTable';
import { Button, Space, Tooltip } from 'antd';
import { DeleteOutlined } from '@ant-design/icons';
import lodash from 'lodash';
import HomeConfigModal from './HomeConfigModal';
import styles from './HomePageConfigs.less';

const HomePageConfigs = (props, ref) => {
10
  const { productList, client, roleHomePages, allProductList, curWeb, userMode } = props;
邓超's avatar
邓超 committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  const [orderTable, setOrderTable] = useState([]);
  const [visible, setVisible] = useState(false);
  const [modalType, setModalType] = useState();
  const [currentPageConfig, setCurrentPageConfig] = useState({});
  const count = useRef(0);
  useImperativeHandle(ref, () => ({
    getHomePageConfig: () => orderTable,
  }));
  useEffect(() => {
    console.log(roleHomePages, 'homePageConfig');
    if (!roleHomePages) {
      return;
    }
    let list = roleHomePages.map(item => {
      let obj = { ...item, keyIndex: count.current };
      count.current += 1;
      return obj;
    });

    setOrderTable(list);
  }, [roleHomePages]);

  // 拖拽回调函数
  const dragCallBack = data => {
    if (data) {
      console.log(data);
      setOrderTable(data);
    }
  };
  const addHomePage = () => {
    let obj = {
      productType: productList[0].PackageName,
      homePage: '',
      roleld: [],
      roleName: [],
      keyIndex: count.current,
    };
    setModalType('add');
    setCurrentPageConfig(obj);
    setVisible(true);
  };
  const delUser = (e, index) => {
    e.stopPropagation();
    let list = lodash.cloneDeep(orderTable);
    list.splice(index, 1);

    setOrderTable(list);
  };
  const onsubmit = val => {
    let list = lodash.cloneDeep(orderTable);
    console.log(list, currentPageConfig);
    if (modalType === 'add') {
      count.current += 1;
      list.push(val);
    } else {
      let index = list.findIndex(item => item.keyIndex === currentPageConfig.keyIndex);
      list[index] = val;
    }

    setOrderTable(list);
  };
  const columns = [
    // {
    //   title: '序号',
    //   dataIndex: 'index',
    //   width: 50,
    //   key: 'index',
    // },
79 80 81 82 83 84
    // {
    //   title: '产品类型',
    //   dataIndex: 'productType',
    //   width: 100,
    //   key: 'productType',
    // },
邓超's avatar
邓超 committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    {
      title: '主页Url',
      dataIndex: 'homePage',
      width: 150,
      key: 'homePage',
      ellipsis: {
        showTitle: false,
      },
      render: (text, record, index) => (
        <Tooltip placement="left" title={text}>
          {text}
        </Tooltip>
      ),
    },
    {
      title: '角色',
      dataIndex: 'roleName',
      width: 150,
      key: 'roleName',
      ellipsis: {
        showTitle: false,
      },
      render: (text, record, index) => <Tooltip title={text}>{text}</Tooltip>,
    },
    {
      title: '操作',
      dataIndex: '',
      width: 80,
      align: 'center',
      key: '',
      render: (text, record, index) => (
        <>
          <Space>
            {/* <Tooltip title="编辑默认承办人">
              <EditTwoTone
                onClick={() => toEdit(record, index)}
                style={{ fontSize: '16px', color: '#1890FF' }}
              />
            </Tooltip> */}

            <DeleteOutlined
              onClick={e => delUser(e, index)}
              style={{ fontSize: '16px', color: '#e86060' }}
            />
          </Space>
        </>
      ),
    },
  ];
  return (
    <>
      <div className={styles.btnBox}>
        <Button onClick={addHomePage}>新增角色主页配置</Button>
      </div>
      <DragTable
        bordered
        style={{ marginBottom: '10px' }}
        rowKey={record => record.keyIndex}
        columns={columns}
        dataSource={orderTable}
        pagination={false}
        size="small"
        dragCallBack={dragCallBack}
        onClick={record => {
          setCurrentPageConfig(record);
          setVisible(true);
          setModalType('edit');
          console.log(record);
        }}
        ItemTypes="homePageOrder"
        scroll={{
          y: 500,
        }}
      />
      <HomeConfigModal
        visible={visible}
        modalType={modalType}
        productList={productList}
163
        allProductList={allProductList}
164 165
        userMode={userMode}
        curWeb={curWeb}
邓超's avatar
邓超 committed
166 167 168 169 170 171 172 173 174 175
        client={client}
        currentPageConfig={currentPageConfig}
        onFinish={val => onsubmit(val)}
        handleCancel={() => setVisible(false)}
      />
    </>
  );
};

export default forwardRef(HomePageConfigs);