From c6a8d7b3c39aa38dc847ac28b332c89c43b727e4 Mon Sep 17 00:00:00 2001 From: Depayan Mondal Date: Sun, 9 Feb 2025 03:30:50 +0530 Subject: [PATCH 1/3] added lazy loading to groups page --- groups/index.html | 3 ++- groups/lazyLoading.js | 63 +++++++++++++++++++++++++++++++++++++++++++ groups/render.js | 22 +++++++++++++++ groups/script.js | 11 +++++--- groups/utils.js | 28 +++++++++++++++++++ 5 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 groups/lazyLoading.js diff --git a/groups/index.html b/groups/index.html index a2e188ab..ccafd495 100644 --- a/groups/index.html +++ b/groups/index.html @@ -12,6 +12,7 @@ +
` : '' - } + }

@@ -169,7 +170,7 @@ const createGroupCreationModal = (onClose = () => {}, onSubmit = () => {}) => {
@@ -257,7 +258,7 @@ const createDeleteConfirmationModal = (

Are you sure you want to delete this group?

- +
diff --git a/groups/lazyLoading.js b/groups/lazyLoading.js index b1582f6d..45db5d52 100644 --- a/groups/lazyLoading.js +++ b/groups/lazyLoading.js @@ -1,7 +1,13 @@ import { getDiscordGroupsPaginated } from './utils.js'; -import { renderLoader, removeLoader, renderMoreGroups } from './render.js'; +import { + renderLoader, + removeLoader, + renderMoreGroups, + renderLoadingCards, + removeLoadingCards, +} from './render.js'; import { getParamValueFromURL } from './utils.js'; -import { groupCardOnAction } from './script.js'; +import { groupCardOnAction, dataStore } from './script.js'; const isDevMode = getParamValueFromURL('dev') === 'true'; @@ -20,10 +26,7 @@ async function loadMoreGroups() { if (isFetching || !hasNextPage) return; isFetching = true; - // Only show loader for subsequent loads (not initial) - if (currentPage !== null) { - renderLoader(); - } + renderLoadingCards(); // ✅ Show shimmer instead of white screen try { const { groups: rawGroups, nextPageUrl } = @@ -31,8 +34,13 @@ async function loadMoreGroups() { ? await getDiscordGroupsPaginated() : await getDiscordGroupsPaginated(currentPage, 10); - // Process titles for paginated groups (same as non-paginated logic) - const groups = rawGroups.map((group) => ({ + if (!rawGroups || rawGroups.length === 0) { + hasNextPage = false; + removeLoader(); + return; + } + + const formattedGroups = rawGroups.map((group) => ({ ...group, title: group.rolename .replace('group-', '') @@ -41,15 +49,25 @@ async function loadMoreGroups() { .join(' '), })); - console.log('Fetched groups:', groups); - - if (!groups || groups.length === 0) { - hasNextPage = false; - removeLoader(); - return; + if (!dataStore.groups) { + dataStore.groups = {}; } - renderMoreGroups({ groups, cardOnClick: groupCardOnAction }); + formattedGroups.forEach((group) => { + dataStore.groups[group.id] = { + ...group, + count: group.memberCount, + isMember: group.isMember, + isUpdating: false, + }; + }); + + dataStore.filteredGroupsIds = Object.keys(dataStore.groups); + + renderMoreGroups({ + groups: formattedGroups, + cardOnClick: groupCardOnAction, + }); currentPage = nextPageUrl ? currentPage === null @@ -60,6 +78,7 @@ async function loadMoreGroups() { } catch (error) { console.error('Error fetching more groups:', error); } finally { + removeLoadingCards(); removeLoader(); isFetching = false; } diff --git a/groups/script.js b/groups/script.js index 0f58bb60..b51e0ced 100644 --- a/groups/script.js +++ b/groups/script.js @@ -178,6 +178,7 @@ const afterAuthentication = async () => { if (isDevMode) { return; } + await Promise.all([getDiscordGroups(), getUserGroupRoles()]).then( ([groups, roleData]) => { const nonDeletedGroups = groups.filter((group) => !group.isDeleted); @@ -258,8 +259,13 @@ function updateGroup(id, group) { } export function groupCardOnAction(id) { + if (!dataStore.groups) return; + const group = dataStore.groups[id]; + if (!group) return; + updateGroup(id, { isUpdating: true }); + if (group.isMember) { removeRoleFromMember(group.roleId, dataStore.discordId) .then(() => updateGroup(id, { isMember: false, count: group.count - 1 })) @@ -329,3 +335,5 @@ function showDeleteModal(groupId) { } onCreate(); + +export { dataStore };