generated from RealDevSquad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathApp.js
More file actions
123 lines (103 loc) · 3.26 KB
/
App.js
File metadata and controls
123 lines (103 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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 { NoUserFound } from './components/NoUserFound.js';
const { createElement, rerender } = react;
const tabs = [
{ display_name: 'In Discord', id: 'in_discord' },
{ display_name: 'Linked Accounts', id: 'verified' },
];
export const usersData = {
in_discord: null,
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;
let isLoading = false;
let currentPage = 1;
let showUser = 0;
/* 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);
// add feature flag(feature should be only visible when query params dev=true)
export const paginateFetchedUsers = async (tabId, page = 1) => {
if (isLoading) {
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]);
}
} 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');
if (selectedTabId) {
document.location.search = `tab=${selectedTabId}`;
activeTab = selectedTabId;
if (!usersData[activeTab]) {
const data = await getUsers(activeTab);
usersData[activeTab] = data;
}
rerender(App(), window['root']);
}
};
const handleUserSelected = (userId) => {
if (userId) {
const selectedUserIndex = usersData[activeTab]?.findIndex(
(user) => user.id === userId,
);
if (selectedUserIndex !== -1) {
showUser = selectedUserIndex;
rerender(App(), window['root']);
}
}
};
export const App = () => {
const users = usersData[activeTab] ?? [];
if (users.length)
return createElement('main', {}, [
TabsSection({ tabs, activeTab, handleTabNavigation }),
UsersSection({
users,
showUser,
handleUserSelected,
paginateFetchedUsers,
activeTab,
currentPage,
isLoading,
}),
UserDetailsSection({ user: users[showUser] ?? {} }),
]);
return createElement('main', {}, [
TabsSection({ tabs, activeTab, handleTabNavigation }),
NoUserFound(),
]);
};
if (isDev) {
paginateFetchedUsers(activeTab, 1);
}