forked from Real-Dev-Squad/website-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
354 lines (321 loc) · 10.6 KB
/
script.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const userManagementLink = document.getElementById(USER_MANAGEMENT_LINK);
const discordUserLink = document.getElementById('discord-user-link');
const extensionRequestsLink = document.getElementById(EXTENSION_REQUESTS_LINK);
const syncUsersStatusButton = document.getElementById(SYNC_USERS_STATUS);
const UpdatedstatusMessage = 'All repos uptodate';
const syncExternalAccountsButton = document.getElementById(
SYNC_EXTERNAL_ACCOUNTS,
);
const syncUnverifiedUsersButton = document.getElementById(
SYNC_UNVERIFIED_USERS,
);
const syncNicknamesButton = document.getElementById(SYNC_NICKNAMES);
const syncUsersStatusUpdate = document.getElementById(SYNC_USERS_STATUS_UPDATE);
const syncIdleUsersButton = document.getElementById(SYNC_IDLE_USERS);
const syncIdleUsersUpdate = document.getElementById(SYNC_IDLE_USERS_UPDATE);
const syncIdle7dUsersButton = document.getElementById(SYNC_IDLE_7D_Plus_USERS);
const syncIdle7dUsersUpdate = document.getElementById(
SYNC_IDLE_7D_Plus_USERS_UPDATE,
);
const repoSyncStatusUpdate = document.getElementById(SYNC_REPO_STATUS_UPDATE);
const params = new URLSearchParams(window.location.search);
const taskRequestsLink = document.getElementById(TASK_REQUESTS_LINK);
const syncNicknamesStatusUpdate = document.getElementById(
SYNC_NICKNAMES_STATUS_UPDATE,
);
const syncExternalAccountsUpdate = document.getElementById(
SYNC_EXTERNAL_ACCOUNTS_UPDATE,
);
const syncUnverifiedUsersUpdate = document.getElementById(
SYNC_UNVERIFIED_USERS_UPDATE,
);
const buttonSection = document.getElementById('sync-buttons');
const syncOnboarding31dPlusUsersButton = document.getElementById(
SYNC_ONBOARDING_31D_PLUS_USERS,
);
const syncOnboarding31dPlusUsersUpdate = document.getElementById(
SYNC_ONBOARDING_31D_PLUS_USERS_UPDATE,
);
function getCurrentTimestamp() {
return new Date().toLocaleString();
}
export async function showSuperUserOptions(...privateBtns) {
try {
const isSuperUser = await checkUserIsSuperUser();
if (isSuperUser) {
privateBtns.forEach((btn) => {
if (btn) btn.classList.remove('element-display-remove');
});
buttonSection.classList.remove('element-display-remove');
syncUsersStatusUpdate.textContent = `Last Sync: ${
localStorage.getItem('lastSyncUsersStatus') ||
'Synced Data Not Available'
}`;
syncExternalAccountsUpdate.textContent = `Last Sync: ${
localStorage.getItem('lastSyncExternalAccounts') ||
'Synced Data Not Available'
}`;
syncUnverifiedUsersUpdate.textContent = `Last Sync: ${
localStorage.getItem('lastSyncUnverifiedUsers') ||
'Synced Data Not Available'
}`;
syncIdleUsersUpdate.textContent = `Last Sync: ${
localStorage.getItem('lastSyncIdleUsers') || 'Synced Data Not Available'
}`;
syncIdle7dUsersUpdate.textContent = `Last Sync: ${
localStorage.getItem('lastSyncIdle7dUsers') ||
'Synced Data Not Available'
}`;
syncNicknamesStatusUpdate.textContent = `Last Sync: ${
localStorage.getItem('lastSyncNicknames') || 'Synced Data Not Available'
}`;
syncOnboarding31dPlusUsersUpdate.textContent = `Last Sync: ${
localStorage.getItem('lastSyncOnboarding31dPlusUsers') ||
'Synced Data Not Available'
}`;
}
} catch (err) {
console.log(err);
}
}
/*
* To show the super user options only to the super user, give all those
* buttons or node the class "element-display-remove" so by default they are hidden.
* Then get the node from the DOM into a variable and pass that variable in the
* function below.
*/
showSuperUserOptions(
userManagementLink,
extensionRequestsLink,
discordUserLink,
taskRequestsLink,
);
const createGoalButton = document.getElementById('create-goal');
const repoSyncDiv = document.getElementById('sync-repo-div');
const repoSyncButton = document.getElementById('repo-sync-button');
const createActivityFeedButton = document.getElementById(
'create-activity-feed',
);
const profileUpdateRequestsButton = document.getElementById(
'profile-update-requests',
);
const requestPageButton = document.getElementById('requests-link');
const toast = document.getElementById('toast');
if (params.get('dev') === 'true') {
createGoalButton.classList.remove('element-display-remove');
repoSyncDiv.classList.remove('element-display-remove');
createActivityFeedButton.classList.remove('element-display-remove');
requestPageButton.classList.remove('element-display-remove');
profileUpdateRequestsButton.classList.remove('element-display-remove');
}
function addClickEventListener(
button,
endpoint,
localStorageKey,
lastSyncElement,
method,
) {
button.addEventListener('click', async (event) => {
await handleSync(endpoint, localStorageKey, lastSyncElement, method, event);
});
}
async function handleSync(
endpoint,
localStorageKey,
lastSyncElement,
method,
event,
) {
const button = event.target;
const wrapper = button.parentElement;
const spinner = wrapper.querySelector('.spinner');
const status = wrapper.querySelector('.status');
button.disabled = true;
button.classList.add(DISABLED);
spinner.style.display = 'inline-block';
status.textContent = SYNC_IN_PROGRESS;
if (button.id === 'sync-users-status') {
try {
const userStatus = fetch(`${API_BASE_URL}${endpoint.userStatusUpdate}`, {
method: method.userStatusMethod,
credentials: 'include',
});
const idleUsers = fetch(`${API_BASE_URL}${endpoint.idle}`, {
method: method.idleMethod,
credentials: 'include',
})
.then((res) => res.json())
.then((data) => data.data.users);
const [userStatusResponse, idleUsersData] = await Promise.all([
userStatus,
idleUsers,
]);
const batchResponse = await fetch(
`${API_BASE_URL}${endpoint.batchIdle}`,
{
headers: {
'Content-Type': 'application/json',
},
method: method.batchIdleMethod,
body: JSON.stringify({ users: idleUsersData }),
credentials: 'include',
},
);
if (userStatusResponse.ok && batchResponse.ok) {
status.textContent = SYNC_SUCCESSFUL;
const lastSyncTimestamp = getCurrentTimestamp();
localStorage.setItem(localStorageKey, lastSyncTimestamp);
if (lastSyncElement) {
lastSyncElement.textContent = `Last Sync: ${lastSyncTimestamp}`;
}
} else {
status.textContent = SYNC_FAILED;
}
} catch (err) {
console.error(err);
status.textContent = SYNC_FAILED;
} finally {
spinner.style.display = 'none';
button.classList.remove(DISABLED);
button.disabled = false;
}
} else {
try {
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
method: method,
credentials: 'include',
});
if (response.ok) {
status.textContent = SYNC_SUCCESSFUL;
const lastSyncTimestamp = getCurrentTimestamp();
localStorage.setItem(localStorageKey, lastSyncTimestamp);
if (lastSyncElement) {
lastSyncElement.textContent = `Last Sync: ${lastSyncTimestamp}`;
}
} else {
status.textContent = SYNC_FAILED;
}
} catch (err) {
console.error(err);
status.textContent = SYNC_FAILED;
} finally {
spinner.style.display = 'none';
button.classList.remove(DISABLED);
button.disabled = false;
}
}
}
function showToast(message, type) {
if (typeof message === 'string') {
toast.innerHTML = `<div class="message">${message}</div>`;
}
toast.classList.remove('hidden');
if (type === 'success') {
for (let i = 0; i < message.merge_status.length; i++) {
if (message.merge_status[i].status.updated) {
let repo = message.merge_status[i].repository;
let text = repo.substring(repo.lastIndexOf('/') + 1) + ' synced';
toast.innerHTML = `<div class="message"> ✓ ${text}</div>`;
} else {
toast.innerHTML = `<div class="message">✓ ${UpdatedstatusMessage} </div>`;
}
}
toast.classList.add('success');
toast.classList.remove('failure');
} else if (type === 'failure') {
toast.classList.add('failure');
toast.classList.remove('success');
}
const progressBar = document.createElement('div');
progressBar.classList.add('progress-bar');
progressBar.classList.add('fill');
toast.appendChild(progressBar);
setTimeout(() => {
toast.classList.add('hidden');
toast.innerHTML = '';
}, 5000);
}
const repoSyncHandler = async (event) => {
const button = event.target;
const wrapper = button.parentElement;
const spinner = wrapper.querySelector('.spinner');
const status = wrapper.querySelector('.status');
button.disabled = true;
button.classList.add(DISABLED);
spinner.style.display = 'inline-block';
try {
const apiResponse = await fetch(REPO_SYNC_API_URL);
const response = await apiResponse.json();
if (apiResponse.ok) {
showToast(response, 'success');
} else {
showToast('✗ API response not as expected', 'failure');
}
} catch (err) {
console.error('Error while fetching repo sync data');
showToast('✗ Something unexpected happened!', 'failure');
} finally {
spinner.style.display = 'none';
button.classList.remove(DISABLED);
button.disabled = false;
}
};
repoSyncButton.addEventListener('click', repoSyncHandler);
// Attach (button,API,cookie name,div element of status,HTTP method of API
addClickEventListener(
syncUsersStatusButton,
{
idle: '/users/status?aggregate=true',
batchIdle: '/users/status/batch',
userStatusUpdate: '/users/status/update',
},
'lastSyncUsersStatus',
syncUsersStatusUpdate,
{
idleMethod: 'GET',
batchIdleMethod: 'PATCH',
userStatusMethod: 'PATCH',
},
);
addClickEventListener(
syncExternalAccountsButton,
'/external-accounts/users?action=discord-users-sync',
'lastSyncExternalAccounts',
syncExternalAccountsUpdate,
'POST',
);
addClickEventListener(
syncUnverifiedUsersButton,
'/users/',
'lastSyncUnverifiedUsers',
syncUnverifiedUsersUpdate,
'POST',
);
addClickEventListener(
syncIdleUsersButton,
'/discord-actions/group-idle',
'lastSyncIdleUsers',
syncIdleUsersUpdate,
'PUT',
);
addClickEventListener(
syncNicknamesButton,
'/discord-actions/nicknames/sync?dev=true',
'lastSyncNicknames',
syncNicknamesStatusUpdate,
'POST',
);
addClickEventListener(
syncIdle7dUsersButton,
'/discord-actions/group-idle-7d',
'lastSyncIdle7dUsers',
syncIdle7dUsersUpdate,
'PUT',
);
addClickEventListener(
syncOnboarding31dPlusUsersButton,
'/discord-actions/group-onboarding-31d-plus',
'lastSyncOnboarding31dPlusUsers',
syncOnboarding31dPlusUsersUpdate,
'PUT',
);