-
Notifications
You must be signed in to change notification settings - Fork 162
Feature/user data pagination #891
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
base: develop
Are you sure you want to change the base?
Changes from 19 commits
d101d41
efd2e88
4621abc
b8f8a20
4e8b4bf
f7883eb
e4e28f0
1bbfce5
e9ffa53
eb8e8fa
87ec1d9
458cb09
de294ea
9162eaa
31a8a1b
b3c7664
2155129
3c7dde3
db348a9
eb3bd0f
d52882f
b68987a
ecd6511
308f6c9
61e4e4f
82ce565
bad7294
8ebf8e0
2f09079
d9d24dd
d270d0d
31fe563
49a6fa1
f2920d6
9185479
c5435eb
696e05f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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. | ||
*/ | ||
IshanVeer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// usersData[activeTab] = await getUsers(activeTab); | ||
IshanVeer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
export const fetchUsers = async (tabId, page = 1) => { | ||
if (isLoading) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How you are checking this condition? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
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'); | ||
|
@@ -60,6 +99,10 @@ export const App = () => { | |
users, | ||
showUser, | ||
handleUserSelected, | ||
fetchUsers, | ||
activeTab, | ||
currentPage, | ||
isLoading, | ||
}), | ||
UserDetailsSection({ user: users[showUser] ?? {} }), | ||
]); | ||
|
@@ -69,3 +112,5 @@ export const App = () => { | |
NoUserFound(), | ||
]); | ||
}; | ||
|
||
fetchUsers(activeTab, 1); |
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...']), | ||
]); | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.