import "./WriteOffModal.scss"; import Modal from "react-modal"; import { useState, useRef, useEffect, useCallback } from "react"; import { post } from "../../../js/helpers/data-helper"; import ScanModal from "../ScanModal/ScanModal"; import InfiniteScroll from "react-infinite-scroller"; import ListEnd from "../ListEnd/ListEnd"; const WriteOffModal = ({ onBack, memberID }) => { const [orderNo, setOrderNo] = useState(""); const [showScanModal, setShowScanModal] = useState(false); const [list, setList] = useState([]); const [nextPageIndex, setNextPageIndex] = useState(1); const [loading, setLoading] = useState(false); const hasMore = nextPageIndex !== null; const [allCount, setAllCount] = useState(0); const [month, setMonth] = useState(new Date().toJSON().slice(0, 7)); const showListEnd = list !== null && list.length > 0; const listRef = useRef(); const loadMore = useCallback(async () => { if (loading || nextPageIndex === null) return; setLoading(true); try { const { code, data, msg } = await post("/api/ar/v1/applet/WriteOffHis", { paging: 1, pageIndex: nextPageIndex, pageSize: 10, memberID, month, }); if (code === 200) { setList([...list, ...data.list]); setNextPageIndex( nextPageIndex + 1 > data.allPage ? null : nextPageIndex + 1 ); setAllCount(data.allCount); } else { setNextPageIndex(null); window.weui.toast(msg, { className: "toast", }); } } catch (error) { setNextPageIndex(null); } finally { setLoading(false); } }, [list, loading, nextPageIndex]); useEffect(() => { if (listRef.current) listRef.current.scrollTop = 0; setNextPageIndex(1); setAllCount(0); setList([]); }, [month]); const toast = (text) => window.weui.toast(text, { className: "toast", }); const writeOff = async (orderNo) => { try { const { code, msg } = await post("/api/ar/v1/applet/writeoffcoupon", { orderNo, memberID, }); if (code === 200) { toast("核销成功"); return true; } else { toast(msg); return false; } } catch (error) { return false; } }; const submitFromBtn = async (orderNo) => { const result = await writeOff(orderNo); if (result) { setOrderNo(""); } }; const handleCode = async (orderNo) => { await writeOff(orderNo); setShowScanModal(false); }; return (
onBack && onBack()}>
核销
setOrderNo(e.target.value)} >
setShowScanModal(true)}>
{!!orderNo && (
submitFromBtn(orderNo)}> 确认核销
)}
已核销({allCount}张)
setMonth(e.target.value)} >
listRef && listRef.current} > {list !== null && list.map((coupon) => (
礼券标题
{coupon.title}
核销时间
{coupon.writeOffTime}
))} {showListEnd && }
{showScanModal && ( setShowScanModal(false)} onCode={handleCode} > )}
); }; export default WriteOffModal;