MessageListDrawer.tsx 3.11 KB
import { RESPONSE_CODE } from '@/constants/enum';
import { postOrderErpMessageQueryMyMessage } from '@/services';
import { formatDateTime, getUserInfo } from '@/utils';
import { UserOutlined } from '@ant-design/icons';
import { Avatar, Badge, Drawer, Flex, List } from 'antd';
import VirtualList from 'rc-virtual-list';
import { useEffect, useState } from 'react';

export default ({ setVisible }) => {
  const userInfo = getUserInfo();
  const current = useState<number>(1); //当前页码
  // const [total, setTotal] = useState(0);
  const [messageListData, setMessageListData] = useState<any[]>([]); //列表数据
  const ContainerHeight = 400;
  /**
   * 获取消息列表
   */
  async function getMessageListData() {
    let res = await postOrderErpMessageQueryMyMessage({
      data: { username: userInfo.username, current: current[0] },
    });
    if (res && res.result === RESPONSE_CODE.SUCCESS) {
      setMessageListData([...messageListData, ...res?.data?.data]);
      // setTotal(res?.data?.total);
    }
  }

  /**
   * 跳转到订单列表
   */
  function toOrderList(mainOrderId: any) {
    window.open('/order?id=' + mainOrderId, '_blank');
  }

  const onScroll = (e: React.UIEvent<HTMLElement, UIEvent>) => {
    // Refer to: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#problems_and_solutions
    if (
      Math.abs(
        e.currentTarget.scrollHeight -
          e.currentTarget.scrollTop -
          ContainerHeight,
      ) <= 1
    ) {
      getMessageListData();
    }
  };

  useEffect(() => {
    getMessageListData();
  }, []);

  return (
    <>
      <Drawer
        title="消息列表"
        onClose={() => {
          setVisible(false);
        }}
        open={true}
        width={450}
        styles={{ body: { paddingTop: 0 } }}
      >
        <List>
          <VirtualList
            data={messageListData}
            height={400}
            itemHeight={47}
            itemKey="email"
            onScroll={onScroll}
          >
            {(item: any) => (
              <List.Item key={item.id}>
                <List.Item.Meta
                  avatar={
                    <Badge dot={item.unreadNum > 0}>
                      <Avatar shape="square" icon={<UserOutlined />} />
                    </Badge>
                  }
                />
                <Flex
                  vertical
                  className="hover:cursor-pointer"
                  onClick={() => {
                    toOrderList(item.mainOrderId);
                  }}
                >
                  <Flex>
                    <div>
                      {item.content}
                      <span className="text-[#8C8C8C]">
                        (点击跳转到对应主订单)
                      </span>
                    </div>
                  </Flex>
                  <Flex>
                    <span className="text-xs text-[#8C8C8C] pt-1">
                      {formatDateTime(item.createTime)}
                    </span>
                  </Flex>
                </Flex>
              </List.Item>
            )}
          </VirtualList>
        </List>
      </Drawer>
    </>
  );
};