WorkDetail.jsx 2.93 KB
Newer Older
熊成伟's avatar
熊成伟 committed
1
import React, {useEffect, useState} from 'react';
熊成伟's avatar
熊成伟 committed
2
import {ActivityIndicator, Toast, WhiteSpace, WingBlank} from 'antd-mobile'
熊成伟's avatar
熊成伟 committed
3 4 5 6
import TodoCard from '../../components/LogCard'
import TopNotice from '../../components/NoticeTab'
import axiosRequest from '../../utils/request';
import './index.less'
熊成伟's avatar
熊成伟 committed
7
import Empty from '../Empty'
熊成伟's avatar
熊成伟 committed
8
import useDebounce from '../../useHooks/useDebounce'
熊成伟's avatar
熊成伟 committed
9 10 11

const WorkDetail = (props) => {
    const [loading, setLoading] = useState(false);
熊成伟's avatar
熊成伟 committed
12
    const [current, setCurrent] = useState(0);
熊成伟's avatar
熊成伟 committed
13
    const [searchWord, setSearchWord] = useState()
熊成伟's avatar
熊成伟 committed
14
    const [data, setData] = useState([]);
熊成伟's avatar
熊成伟 committed
15 16

    const getData = () => {
熊成伟's avatar
熊成伟 committed
17
        const pageMap ={searchWord, nowPage: current + 1, pageSize: 10};
熊成伟's avatar
熊成伟 committed
18 19 20
        setLoading(true)
        axiosRequest({
            method: 'post',
熊成伟's avatar
熊成伟 committed
21 22
            url: '/idtAppServiceV6/oApp/getUnDone',
            body: pageMap,
熊成伟's avatar
熊成伟 committed
23 24
        }).then(res => {
            setLoading(false);
熊成伟's avatar
熊成伟 committed
25
            let result = end && res.rows ? data.concat(res.rows) : data;
熊成伟's avatar
熊成伟 committed
26
            res.rows && res.rows.length >= 10 ? setCurrent(current + 1) : Toast.info('当前是最后一页', 1);
熊成伟's avatar
熊成伟 committed
27
            setEnd(res.rows && res.rows.length >= 10)
熊成伟's avatar
熊成伟 committed
28
            setData(result);
熊成伟's avatar
熊成伟 committed
29 30 31 32
        });
    };

    //下拉加载
熊成伟's avatar
熊成伟 committed
33
    const [end, setEnd] = useState(true)
熊成伟's avatar
熊成伟 committed
34
    const handleScroll = useDebounce(() => {
熊成伟's avatar
熊成伟 committed
35
        const event = document.getElementById("work-content")
熊成伟's avatar
熊成伟 committed
36 37 38 39 40 41
        const top = event.scrollTop;
        const scrollHeight = event.scrollHeight;
        const clientHeight = event.clientHeight;
        if (top + clientHeight === scrollHeight) {
            getData()
        }
熊成伟's avatar
熊成伟 committed
42
    }, 500)
熊成伟's avatar
熊成伟 committed
43 44 45 46 47 48 49 50 51 52 53 54 55
    useEffect(() => {
        window.addEventListener('scroll', handleScroll, true);
        return () =>  window.removeEventListener('scroll', handleScroll, true);
    }, [data]);

    const checkLog = () => {
        props.history.push(`/blank/info/example/1`)
    };

    //回到顶部
    useEffect(() => {
        const element = document.getElementById('work-content')
        element.scrollTo(0, 0)
熊成伟's avatar
熊成伟 committed
56 57 58
    }, []);

    //tab已读未读切换
熊成伟's avatar
熊成伟 committed
59
    const [tab, setTab] = useState('N')
熊成伟's avatar
熊成伟 committed
60
    const tabChange = val => {
熊成伟's avatar
熊成伟 committed
61 62
        setTab(val);
        setData([])
熊成伟's avatar
熊成伟 committed
63 64 65 66 67
    };

    useEffect(() => {
        getData()
    }, [searchWord, tab])
熊成伟's avatar
熊成伟 committed
68 69 70 71 72 73 74 75 76
    return (
        <div id="work-content" className="work-content">
            <WingBlank>
                <ActivityIndicator
                    toast
                    text="加载中..."
                    animating={loading}
                />
                <WhiteSpace/>
熊成伟's avatar
熊成伟 committed
77
                <TopNotice onSearch={setSearchWord} tabChange={tabChange}/>
熊成伟's avatar
熊成伟 committed
78
                {
熊成伟's avatar
熊成伟 committed
79
                    data.length > 0 ? data.map((item, index) => (
熊成伟's avatar
熊成伟 committed
80 81 82 83 84
                        <div key={index}>
                            <WhiteSpace/>
                            <TodoCard data={item} onClick={checkLog}/>
                        </div>

熊成伟's avatar
熊成伟 committed
85
                    )) : <Empty/>
熊成伟's avatar
熊成伟 committed
86 87 88 89 90 91 92 93
                }
            </WingBlank>
        </div>
    );
};

export default WorkDetail;