feat: topic 点击按钮加载更多

pull/56/head
moonrailgun 3 years ago
parent e6dfb81923
commit fd4cdc2865

@ -49,6 +49,14 @@
&.ant-btn-link { &.ant-btn-link {
border-color: transparent; border-color: transparent;
&[disabled] {
&,&:hover, &:focus, &:active {
color: rgba(255, 255, 255, 0.25);
border-color: transparent;
background-color: transparent;
}
}
} }
} }

@ -1,4 +1,4 @@
import React, { useCallback, useEffect } from 'react'; import React, { useCallback, useEffect, useRef, useState } from 'react';
import { TopicCard } from '../components/TopicCard'; import { TopicCard } from '../components/TopicCard';
import { import {
showSuccessToasts, showSuccessToasts,
@ -26,6 +26,8 @@ const Root = styled(LoadingOnFirst)({
flexDirection: 'column', flexDirection: 'column',
width: '100%', width: '100%',
position: 'relative', position: 'relative',
paddingTop: 10,
paddingBottom: 10,
'.ant-empty': { '.ant-empty': {
paddingTop: 80, paddingTop: 80,
@ -42,33 +44,62 @@ const Root = styled(LoadingOnFirst)({
}, },
}); });
const PAGE_SIZE = 20;
const GroupTopicPanelRender: React.FC = React.memo(() => { const GroupTopicPanelRender: React.FC = React.memo(() => {
const panelInfo = useGroupPanelContext(); const panelInfo = useGroupPanelContext();
const { panelId, groupId } = panelInfo; const { panelId, groupId } = panelInfo;
const { topicMap, addTopicPanel, addTopicItem, updateTopicItem } = const {
useTopicStore(); topicMap,
addTopicPanel,
addTopicItem,
updateTopicItem,
resetTopicPanel,
} = useTopicStore();
const topicList = topicMap[panelId]; const topicList = topicMap[panelId];
const currentPageRef = useRef(1);
const [hasMore, setHasMore] = useState(true);
const [{ loading }, fetch] = useAsyncRequest(async () => { const [{ loading }, fetch] = useAsyncRequest(
async (page = 1) => {
if (!groupId || !panelId) { if (!groupId || !panelId) {
return []; return [];
} }
const { data } = await request.get('list', { const { data: list } = await request.post('list', {
params: {
groupId, groupId,
panelId, panelId,
}, page,
size: PAGE_SIZE,
}); });
addTopicPanel(panelId, data); if (Array.isArray(list)) {
}, [groupId, panelId, addTopicPanel]); addTopicPanel(panelId, list);
if (list.length !== PAGE_SIZE) {
// 没有更多了
setHasMore(false);
}
}
},
[groupId, panelId, addTopicPanel]
);
useEffect(() => { useEffect(() => {
/** /**
* *
*/ */
fetch(); fetch();
return () => {
// 因为监听更新只在当前激活的面板中监听,还没添加到全局,因此为了保持面板状态需要清理面板状态
// TODO: 增加群组级别的更新监听新增后可以移除
resetTopicPanel(panelId);
};
}, []);
const handleLoadMore = useCallback(() => {
currentPageRef.current += 1;
fetch(currentPageRef.current);
}, [fetch]); }, [fetch]);
useGlobalSocketEvent( useGlobalSocketEvent(
@ -115,7 +146,21 @@ const GroupTopicPanelRender: React.FC = React.memo(() => {
return ( return (
<Root spinning={loading}> <Root spinning={loading}>
{Array.isArray(topicList) && topicList.length > 0 ? ( {Array.isArray(topicList) && topicList.length > 0 ? (
topicList.map((item, i) => <TopicCard key={i} topic={item} />) <>
{topicList.map((item, i) => (
<TopicCard key={i} topic={item} />
))}
{hasMore ? (
<Button type="link" disabled={loading} onClick={handleLoadMore}>
{loading ? Translate.loading : Translate.loadMore}
</Button>
) : (
<Button type="link" disabled={true} onClick={handleLoadMore}>
{Translate.noMore}
</Button>
)}
</>
) : ( ) : (
<Empty description={Translate.noTopic}> <Empty description={Translate.noTopic}>
<Button type="primary" onClick={handleCreateTopic}> <Button type="primary" onClick={handleCreateTopic}>

@ -12,6 +12,7 @@ interface TopicStoreState {
addTopicPanel: (panelId: string, topicList: GroupTopic[]) => void; addTopicPanel: (panelId: string, topicList: GroupTopic[]) => void;
addTopicItem: (panelId: string, topic: GroupTopic) => void; addTopicItem: (panelId: string, topic: GroupTopic) => void;
updateTopicItem: (panelId: string, topic: GroupTopic) => void; updateTopicItem: (panelId: string, topic: GroupTopic) => void;
resetTopicPanel: (panelId: string) => void;
} }
export const useTopicStore = create< export const useTopicStore = create<
@ -22,7 +23,11 @@ export const useTopicStore = create<
topicMap: {}, topicMap: {},
addTopicPanel: (panelId, topicList) => { addTopicPanel: (panelId, topicList) => {
set((state) => { set((state) => {
if (state.topicMap[panelId]) {
state.topicMap[panelId].push(...topicList);
} else {
state.topicMap[panelId] = topicList; state.topicMap[panelId] = topicList;
}
}); });
}, },
addTopicItem: (panelId, topic) => { addTopicItem: (panelId, topic) => {
@ -44,5 +49,10 @@ export const useTopicStore = create<
} }
}); });
}, },
resetTopicPanel: (panelId) => {
set((state) => {
delete state.topicMap[panelId];
});
},
})) }))
); );

@ -9,4 +9,16 @@ export const Translate = {
'zh-CN': '回复该话题', 'zh-CN': '回复该话题',
'en-US': 'Reply this topic', 'en-US': 'Reply this topic',
}), }),
loadMore: localTrans({
'zh-CN': '加载更多',
'en-US': 'Load More',
}),
noMore: localTrans({
'zh-CN': '没有更多了',
'en-US': 'No More',
}),
loading: localTrans({
'zh-CN': '加载中...',
'en-US': 'Loading...',
}),
}; };

Loading…
Cancel
Save