generated from RealDevSquad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathutils.js
More file actions
232 lines (212 loc) · 5.45 KB
/
utils.js
File metadata and controls
232 lines (212 loc) · 5.45 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
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
const BASE_URL = window.API_BASE_URL; // REPLACE WITH YOUR LOCALHOST URL FOR TESTING LOCAL BACKEND
async function getMembers() {
try {
const res = await fetch(`${BASE_URL}/users/`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-type': 'application/json',
},
});
const { users } = await res.json();
return users;
} catch (err) {
return err;
}
}
async function getUserSelf() {
try {
const res = await fetch(`${BASE_URL}/users?profile=true`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-type': 'application/json',
},
});
const user_self = await res.json();
return user_self;
} catch (err) {
return err;
}
}
async function getUserGroupRoles() {
const res = await fetch(`${BASE_URL}/discord-actions/roles`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-type': 'application/json',
},
});
return await res.json();
}
async function getDiscordGroups() {
try {
const res = await fetch(`${BASE_URL}/discord-actions/groups`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-type': 'application/json',
},
});
const { groups } = await res.json();
return groups;
} catch (err) {
return err;
}
}
async function createDiscordGroupRole(groupRoleBody) {
try {
const res = await fetch(`${BASE_URL}/discord-actions/groups`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify(groupRoleBody),
});
const self_user = await res.json();
return self_user;
} catch (err) {
return err;
}
}
async function addGroupRoleToMember(memberRoleBody) {
try {
const res = await fetch(`${BASE_URL}/discord-actions/roles`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify(memberRoleBody),
});
if (!res.ok) {
const error = await res.json();
throw error;
}
const data = await res.json();
return data;
} catch (err) {
throw err;
}
}
async function removeRoleFromMember(roleId, discordId) {
try {
const res = await fetch(`${BASE_URL}/discord-actions/roles`, {
method: 'DELETE',
credentials: 'include',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify({ roleid: roleId, userid: discordId }),
});
return await res.json();
} catch (err) {
throw err;
}
}
async function deleteDiscordGroupRole(groupId) {
try {
const res = await fetch(`${BASE_URL}/discord-actions/groups/${groupId}`, {
method: 'DELETE',
credentials: 'include',
headers: {
'Content-type': 'application/json',
},
});
if (!res.ok) {
const errorResponse = await res.json();
throw new Error(
`Failed to delete group role: ${JSON.stringify(errorResponse.error)}`,
);
}
return await res.json();
} catch (err) {
throw err;
}
}
function removeGroupKeywordFromDiscordRoleName(groupName) {
if (/^group.*/.test(groupName)) {
const splitNames = groupName.split('-');
splitNames.shift();
return splitNames.join('-');
}
return groupName;
}
function getDiscordGroupIdsFromSearch(groups, multipleGroupSearch) {
if (!multipleGroupSearch) return groups.map((group) => group.id);
const GROUP_SEARCH_SEPARATOR = ',';
const searchGroups = multipleGroupSearch
.split(GROUP_SEARCH_SEPARATOR)
.map((group) => group.trim().toLowerCase());
const matchGroups = groups.filter((group) =>
searchGroups.some((searchGroup) =>
group.title.toLowerCase().startsWith(searchGroup),
),
);
return matchGroups.map((group) => group.id);
}
function getParamValueFromURL(paramKey) {
const params = new URLSearchParams(window.location.search);
return params.get(paramKey);
}
function setParamValueInURL(paramKey, paramValue) {
const params = new URLSearchParams(window.location.search);
if (paramValue === '') params.delete(paramKey);
else params.set(paramKey, paramValue);
window.history.replaceState(
{},
'',
window.location.pathname + (params.toString() && `?${params}`),
);
}
const fullDateString = (timestamp) => {
if (typeof timestamp !== 'number' || !Number.isFinite(timestamp)) {
return 'N/A';
}
const dateObj = new Date(timestamp);
if (isNaN(dateObj.getTime())) {
return 'N/A';
}
const options = {
weekday: 'long',
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
timeZoneName: 'short',
hour12: true,
};
return new Intl.DateTimeFormat('en-US', options).format(dateObj);
};
const shortDateString = (timestamp) => {
if (typeof timestamp !== 'number' || !Number.isFinite(timestamp)) {
return 'N/A';
}
const date = new Date(timestamp);
if (isNaN(date.getTime())) {
return 'N/A';
}
const year = date.getFullYear();
const month = new Intl.DateTimeFormat('en-US', { month: 'short' }).format(
date,
);
return `${date.getDate()} ${month} ${year}`;
};
export {
getUserGroupRoles,
getMembers,
getUserSelf,
getDiscordGroups,
createDiscordGroupRole,
addGroupRoleToMember,
removeRoleFromMember,
deleteDiscordGroupRole,
removeGroupKeywordFromDiscordRoleName,
getDiscordGroupIdsFromSearch,
getParamValueFromURL,
setParamValueInURL,
fullDateString,
shortDateString,
};