Skip to content
This repository has been archived by the owner on Jul 25, 2023. It is now read-only.

Commit

Permalink
feat(conversations): Add Lazy Loading conversations list
Browse files Browse the repository at this point in the history
  • Loading branch information
kenpingshu committed Nov 15, 2019
1 parent 39512c8 commit c10ae9f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
22 changes: 21 additions & 1 deletion src/modules/conversations/components/ConversationsSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ import { Drawer, Layout, List, Menu } from 'antd';
import { useSelector } from 'react-redux';
import { CheckCircle, Heart, RefreshCcw, Star } from 'react-feather';
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';
import { LeadAvatar } from '@onr/conversations';
import { LeadAvatar, ConversationsService } from '@onr/conversations';

const { Sider } = Layout;

interface ConversationsSidebarProps {
conversationsData: any;
onConversationSelected: any;
setConversationsResponse: any;
currentPage: number;
}

export const ConversationsSidebar: React.FC<ConversationsSidebarProps> = ({
conversationsData,
onConversationSelected,
setConversationsResponse,
currentPage,
}: ConversationsSidebarProps) => {
const isMobile = useSelector((store: any) => store.wrapper.mobile);
const [messages, setMessages] = useState(false);
Expand All @@ -23,6 +27,21 @@ export const ConversationsSidebar: React.FC<ConversationsSidebarProps> = ({
setSelectedIndex(index);
onConversationSelected(index);
};
const onListScroll = async (e) => {
const element = e.target
if (element.scrollHeight - element.scrollTop === element.clientHeight) {
const conversationsResponse = await ConversationsService.getConversations({
campaignId: '1',
params: {
page: currentPage + 1,
},
});
// console.log(conversationsData);
conversationsResponse.data = conversationsData.concat(conversationsResponse.data);

setConversationsResponse(conversationsResponse);
}
};
const conversations = (
<div
css={`
Expand Down Expand Up @@ -60,6 +79,7 @@ export const ConversationsSidebar: React.FC<ConversationsSidebarProps> = ({
className="scroll-y flex-1 bg-transparent px-3 py-1"
dataSource={conversationsData}
itemLayout="horizontal"
onScroll={onListScroll}
renderItem={(item, index) => (
<List.Item
onClick={changeIndex.bind(null, index)}
Expand Down
12 changes: 7 additions & 5 deletions src/modules/conversations/pages/ConversationsMainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

const ConversationsComponent: React.FC = () => {
const [selectedIndex, setSelectedIndex] = useState(0);
const [conversationsData, setConversationsData] = useState([]);
const [conversationsResponse, setConversationsResponse] = useState({ data: [], meta: {} });
const fetchConversations = async (campaignId: string) => {
const response = await ConversationsService.getConversations({ campaignId });
return response;
Expand All @@ -22,7 +22,7 @@ const ConversationsComponent: React.FC = () => {
useEffect(() => {
(async function() {
const conversationsResponse = await fetchConversations('1');
setConversationsData(conversationsResponse.data);
setConversationsResponse(conversationsResponse);
})();
}, []);

Expand All @@ -31,11 +31,13 @@ const ConversationsComponent: React.FC = () => {
<Layout className="fill-workspace rounded shadow-sm overflow-hidden">
<FilterSidebar />
<ConversationsSidebar
conversationsData={conversationsData}
conversationsData={conversationsResponse.data}
setConversationsResponse={setConversationsResponse}
onConversationSelected={onConversationSelected}
currentPage={conversationsResponse.meta.current_page}
/>
{conversationsData[selectedIndex] && (
<ChatRoom conversation={conversationsData[selectedIndex]} />
{conversationsResponse.data[selectedIndex] && (
<ChatRoom conversation={conversationsResponse.data[selectedIndex]} />
)}
</Layout>
</>
Expand Down

0 comments on commit c10ae9f

Please sign in to comment.