|
@@ -1,44 +1,80 @@
|
|
|
import { GameListRep, searchGameListApi } from "@/api/home";
|
|
|
import Card from "@/components/Card/Card";
|
|
|
+import Empty from "@/components/Empty";
|
|
|
+import { debounce } from "@/utils/methods";
|
|
|
+import { InfiniteScroll } from "antd-mobile";
|
|
|
import React from "react";
|
|
|
import styles from "./page.module.scss";
|
|
|
+
|
|
|
interface Props {
|
|
|
providerId: number;
|
|
|
}
|
|
|
|
|
|
const Left: React.FC<Props> = ({ providerId }) => {
|
|
|
- console.log(searchGameListApi);
|
|
|
const [data, setData] = React.useState<GameListRep[]>([]);
|
|
|
+ const [loading, setLoading] = React.useState<boolean>(false);
|
|
|
+ const [hasMore, setHasMore] = React.useState(true);
|
|
|
+ const [isInit, setIsInit] = React.useState(true);
|
|
|
+ const PageRef = React.useRef({
|
|
|
+ current_page: 0,
|
|
|
+ page_size: 15,
|
|
|
+ });
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
- getData();
|
|
|
+ setData([]);
|
|
|
+ setHasMore(true);
|
|
|
+ setLoading(false);
|
|
|
+ setIsInit(true);
|
|
|
+ PageRef.current.current_page = 0;
|
|
|
}, [providerId]);
|
|
|
+
|
|
|
const getData = async () => {
|
|
|
- const res = await searchGameListApi({
|
|
|
- provider_id: providerId,
|
|
|
- current_page: 1,
|
|
|
- page_size: 15,
|
|
|
- use_page: true,
|
|
|
- });
|
|
|
- if (res.data) {
|
|
|
- setData(res.data);
|
|
|
+ try {
|
|
|
+ setLoading(true);
|
|
|
+ setIsInit(false);
|
|
|
+ const res = await searchGameListApi({
|
|
|
+ provider_id: providerId,
|
|
|
+ ...PageRef.current,
|
|
|
+ use_page: true,
|
|
|
+ });
|
|
|
+ if (res.data) {
|
|
|
+ const toData = [...data, ...res.data];
|
|
|
+ setData(toData);
|
|
|
+ if (toData.length >= res.page.total_count) {
|
|
|
+ setHasMore(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ setLoading(false);
|
|
|
}
|
|
|
- console.log(1223, res);
|
|
|
};
|
|
|
+ const loadMore = debounce(async () => {
|
|
|
+ if (!hasMore || loading || !providerId) return;
|
|
|
+ PageRef.current.current_page += 1;
|
|
|
+ getData();
|
|
|
+ }, 500) as () => Promise<void>;
|
|
|
|
|
|
return (
|
|
|
<div className={styles.right}>
|
|
|
- {data.map((item) => {
|
|
|
- return (
|
|
|
- <Card
|
|
|
- item={item}
|
|
|
- className={styles.gameCard}
|
|
|
- isShowFavorite={true}
|
|
|
- isShowOnline={true}
|
|
|
- ></Card>
|
|
|
- );
|
|
|
- })}
|
|
|
+ <div className={styles.rightBox}>
|
|
|
+ {data.map((item) => {
|
|
|
+ return (
|
|
|
+ <Card
|
|
|
+ key={item.id}
|
|
|
+ item={item}
|
|
|
+ className={styles.gameCard}
|
|
|
+ isShowFavorite={true}
|
|
|
+ isShowOnline={true}
|
|
|
+ ></Card>
|
|
|
+ );
|
|
|
+ })}
|
|
|
+ </div>
|
|
|
+ {(isInit || data.length > 0) && (
|
|
|
+ <InfiniteScroll loadMore={loadMore} hasMore={hasMore}></InfiniteScroll>
|
|
|
+ )}
|
|
|
+
|
|
|
+ {!isInit && !loading && data.length <= 0 && <Empty></Empty>}
|
|
|
</div>
|
|
|
);
|
|
|
};
|
|
|
-export default Left;
|
|
|
+export default React.memo(Left);
|