Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/user data pagination #891

Open
wants to merge 19 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions users/discord/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TabsSection } from './components/TabsSection.js';
import { UsersSection } from './components/UsersSection.js';
import { UserDetailsSection } from './components/UserDetailsSection.js';
import { getUsers } from './utils/util.js';
import { getUsers, mockUsersData } from './utils/util.js';
import { NoUserFound } from './components/NoUserFound.js';

const { createElement, rerender } = react;
Expand All @@ -17,9 +17,48 @@ export const usersData = {
const urlParams = new URLSearchParams(window.location.search);

let activeTab = urlParams.get('tab') ?? 'in_discord';

const INITIAL_USERS = 10;
let isLoading = false;
let currentPage = 1;
let showUser = 0;
usersData[activeTab] = await getUsers(activeTab);

/* this is the original function for fetching user data from the API, will remove it once
the API pagination issue is resolved. Currently testing pagination using mock data.
*/
// usersData[activeTab] = await getUsers(activeTab);
IshanVeer marked this conversation as resolved.
Show resolved Hide resolved

export const fetchUsers = async (tabId, page = 1) => {
if (isLoading) {
return;
}

isLoading = true;

try {
const start = (page - 1) * INITIAL_USERS;
const end = start + INITIAL_USERS;

const newUsers = mockUsersData[tabId].slice(start, end);

if (newUsers.length > 0) {
if (page === 1) {
usersData[tabId] = newUsers; // Initial load
} else {
const existingIds = new Set(usersData[tabId].map((user) => user.id));
const uniqueNewUsers = newUsers.filter(
(user) => !existingIds.has(user.id),
);
usersData[tabId] = [...usersData[tabId], ...uniqueNewUsers];
}
currentPage = page;
}
} catch (error) {
console.error('Error fetching users', error);
} finally {
isLoading = false;
rerender(App(), document.getElementById('root'));
}
};

const handleTabNavigation = async (e) => {
const selectedTabId = e.target.getAttribute('data_key');
Expand Down Expand Up @@ -60,6 +99,10 @@ export const App = () => {
users,
showUser,
handleUserSelected,
fetchUsers,
activeTab,
currentPage,
isLoading,
}),
UserDetailsSection({ user: users[showUser] ?? {} }),
]);
Expand All @@ -69,3 +112,5 @@ export const App = () => {
NoUserFound(),
]);
};

fetchUsers(activeTab, 1);
7 changes: 7 additions & 0 deletions users/discord/components/LoadingSpinner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { createElement } = react;

export const LoadingSpinner = () => {
return createElement('aside', { class: 'users_section' }, [
createElement('div', { class: 'loading' }, ['Loading...']),
]);
};
32 changes: 29 additions & 3 deletions users/discord/components/UsersSection.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
const { createElement } = react;
import { LoadingSpinner } from './LoadingSpinner.js';

export const UsersSection = ({
users,
showUser,
handleUserSelected,
fetchUsers,
activeTab,
currentPage,
isLoading,
}) => {
window.addEventListener(
'scroll',
debounce(() => {
console.log('scroll triggered');
if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
fetchUsers(activeTab, currentPage + 1);
}
}, 200),
);

if (isLoading) {
return LoadingSpinner();
}

export const UsersSection = ({ users, showUser, handleUserSelected }) => {
return createElement(
'aside',
{ class: 'users_section', onclick: handleUserSelected },
{
class: 'users_section',
onclick: handleUserSelected,
},
users?.map((user) => {
return createElement(
'div',
Expand All @@ -18,7 +44,7 @@ export const UsersSection = ({ users, showUser, handleUserSelected }) => {
src: user?.picture?.url ?? dummyPicture,
class: 'user_image',
}),
createElement('span', {}, [user.first_name + ' ' + user.last_name]),
createElement('span', {}, [user.username]),
],
);
}),
Expand Down
3 changes: 2 additions & 1 deletion users/discord/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ main {

.users_section {
grid-area: aside;
overflow: scroll;
overflow: auto;
padding-inline: 1rem;
max-height: 80vh;
}

.tabs_section {
Expand Down
Loading