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 37 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d101d41
added function to initially load 20 users and then load more users as…
Sep 25, 2024
efd2e88
fetching mock users and handling fetch more users
Oct 8, 2024
4621abc
merged recent develop branch to feature branch
Oct 9, 2024
b8f8a20
users are getting fetched with scroll but the next set of users are n…
Oct 10, 2024
4e8b4bf
user list is getting updated
Oct 15, 2024
f7883eb
fixed duplicated fetching
Oct 15, 2024
e4e28f0
moved mock data to utils, addded loading state
Oct 15, 2024
1bbfce5
added function to initially load 20 users and then load more users as…
Sep 25, 2024
e9ffa53
fetching mock users and handling fetch more users
Oct 8, 2024
eb8e8fa
users are getting fetched with scroll but the next set of users are n…
Oct 10, 2024
87ec1d9
user list is getting updated
Oct 15, 2024
458cb09
fixed duplicated fetching
Oct 15, 2024
de294ea
moved mock data to utils, addded loading state
Oct 15, 2024
9162eaa
Merge branch 'feature/user-data-pagination' of https://github.com/Ish…
Oct 15, 2024
31a8a1b
fixed loading psinner issue
Oct 15, 2024
b3c7664
removed empty else block
Oct 16, 2024
2155129
removed debounce function as it is already defined globally
Oct 17, 2024
3c7dde3
Added comments for a few fucntions
Oct 17, 2024
db348a9
created a separate component for loading spinner
Oct 22, 2024
eb3bd0f
updated pagination function to get data from api and then paginate th…
Oct 30, 2024
d52882f
added feature flag to pagination function
Oct 30, 2024
b68987a
Added tests and feature flag
Nov 2, 2024
ecd6511
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-d…
Nov 6, 2024
308f6c9
reverted jest.config
Nov 6, 2024
61e4e4f
reverted babel config file
Nov 6, 2024
82ce565
reverted test file
Nov 6, 2024
bad7294
updated tests
Nov 7, 2024
8ebf8e0
pushed test for 2 mock users
Nov 7, 2024
2f09079
fixed dev flag
Nov 7, 2024
d9d24dd
removed unnecessary dependencies
Nov 7, 2024
d270d0d
added tests for discord and verified tabs
Nov 10, 2024
31fe563
updated verified users test in feature flag
Nov 10, 2024
49a6fa1
updated tests
Nov 10, 2024
f2920d6
removed console logs and updated test
Nov 10, 2024
9185479
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-d…
Nov 11, 2024
c5435eb
added test for scroll function
Nov 11, 2024
696e05f
removed comments
Nov 12, 2024
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
40 changes: 40 additions & 0 deletions __tests__/users/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,46 @@ describe('App Component', () => {
afterAll(async () => {
await browser.close();
});
it('should fetch and append new users on subsequent pages for discord users tab when feature flag is on', async () => {
await page.goto(`${BASE_URL}/users/discord/?tab=in_discord&dev=true`);
await page.waitForNetworkIdle();

const userCardTestIds = await page.$$eval(
'[data-testid^="user-card-"]',
(cards) => cards.map((card) => card.getAttribute('data-testid')),
);
expect(userCardTestIds.length).toBe(2);
});
it('should fetch and append new users on subsequent pages for discord users tab', async () => {
await page.goto(`${BASE_URL}/users/discord/?tab=in_discord`);
await page.waitForNetworkIdle();

const userCardTestIds = await page.$$eval(
'[data-testid^="user-card-"]',
(cards) => cards.map((card) => card.getAttribute('data-testid')),
);
expect(userCardTestIds.length).toBe(10);
});
IshanVeer marked this conversation as resolved.
Show resolved Hide resolved
it('should fetch and append new users on subsequent pages for verified users tab when feature flag is on', async () => {
await page.goto(`${BASE_URL}/users/discord/?tab=verified&dev=true`);
await page.waitForNetworkIdle();

const userCardTestIds = await page.$$eval(
'[data-testid^="user-card-"]',
(cards) => cards.map((card) => card.getAttribute('data-testid')),
);
expect(userCardTestIds.length).toBe(2);
});
it('should fetch and append new users on subsequent pages for verified users tab', async () => {
await page.goto(`${BASE_URL}/users/discord/?tab=verified`);
await page.waitForNetworkIdle();

const userCardTestIds = await page.$$eval(
'[data-testid^="user-card-"]',
(cards) => cards.map((card) => card.getAttribute('data-testid')),
);
expect(userCardTestIds.length).toBe(10);
});

it('should render all sections', async () => {
await page.waitForSelector('.tabs_section');
Expand Down
58 changes: 56 additions & 2 deletions users/discord/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,58 @@ export const usersData = {
verified: null,
};
const urlParams = new URLSearchParams(window.location.search);
const isDev = urlParams.get('dev') === 'true';

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

const INITIAL_USERS = 10;
IshanVeer marked this conversation as resolved.
Show resolved Hide resolved
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.
*/
IshanVeer marked this conversation as resolved.
Show resolved Hide resolved

if (!isDev) {
usersData[activeTab] = await getUsers(activeTab);
}

// add feature flag(feature should be only visible when query params dev=true)
IshanVeer marked this conversation as resolved.
Show resolved Hide resolved

export const paginateFetchedUsers = async (tabId, page = 1) => {
if (isLoading) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How you are checking this condition?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checked it with mock data, so when data is not being loaded the loading spinner is rendered.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did you determine isLoading and where are you getting this from ?

return;
}
usersData[activeTab] = await getUsers(activeTab);

isLoading = true;

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

const newUsers = usersData[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;
console.log(usersData[tabId]);
IshanVeer marked this conversation as resolved.
Show resolved Hide resolved
}
} 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 +107,10 @@ export const App = () => {
users,
showUser,
handleUserSelected,
paginateFetchedUsers,
activeTab,
currentPage,
isLoading,
}),
UserDetailsSection({ user: users[showUser] ?? {} }),
]);
Expand All @@ -69,3 +120,6 @@ export const App = () => {
NoUserFound(),
]);
};
if (isDev) {
paginateFetchedUsers(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...']),
]);
};
26 changes: 24 additions & 2 deletions users/discord/components/UsersSection.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
const { createElement } = react;
import { LoadingSpinner } from './LoadingSpinner.js';

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

if (isLoading) {
return LoadingSpinner();
}

export const UsersSection = ({ users, showUser, handleUserSelected }) => {
return createElement(
'aside',
{
class: 'users_section',

'data-testid': 'users-section',
onclick: handleUserSelected,
IshanVeer marked this conversation as resolved.
Show resolved Hide resolved
},
users?.map((user) => {
return createElement(
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