forked from Real-Dev-Squad/website-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
132 lines (116 loc) · 3.42 KB
/
utils.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
function goToAuthPage() {
const authUrl = `https://github.com/login/oauth/authorize?client_id=23c78f66ab7964e5ef97&}&state=${window.location.href}`;
window.open(authUrl, '_self');
}
async function getSelfUser(endpoint = '/users/self') {
try {
const res = await fetch(`${API_BASE_URL}${endpoint}`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-type': 'application/json',
},
});
if (endpoint === '/users/self') {
const self_user = await res.json();
if (res.status === 200) {
return self_user?.user || self_user;
}
} else {
location.reload();
}
} catch (err) {}
}
async function checkUserIsSuperUser() {
const self_user = await getSelfUser();
return self_user?.roles['super_user'];
}
function createElement({ type, attributes = {}, innerText }) {
const element = document.createElement(type);
Object.keys(attributes).forEach((item) => {
element.setAttribute(item, attributes[item]);
});
element.textContent = innerText;
return element;
}
function addLoader(container) {
removeLoader('loader');
if (!container) return;
const loader = createElement({
type: 'div',
attributes: { class: 'loader' },
});
const loadertext = createElement({
type: 'p',
attributes: { class: 'loader-text' },
innerText: 'Loading...',
});
loader.appendChild(loadertext);
container.appendChild(loader);
}
function removeLoader(classname = 'loader') {
const loader = document.querySelector(`.${classname}`);
if (loader) loader.remove();
}
function debounce(func, delay) {
let timerId;
return (...args) => {
if (timerId) {
clearTimeout(timerId);
}
timerId = setTimeout(() => {
func(...args);
}, delay);
};
}
async function addDelay(milliSeconds) {
await new Promise((resolve) => setTimeout(resolve, milliSeconds));
}
function dateDiff(date1, date2, formatter) {
if (date2 > date1) {
return dateDiff(date2, date1, formatter);
}
const timeDifference = new Date(date1).getTime() - new Date(date2).getTime();
const seconds = Math.floor(timeDifference / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
const months = Math.floor(days / 30);
const years = Math.floor(days / 365);
let res;
if (seconds < 60) {
res = `${seconds} ${seconds === 1 ? 'second' : 'seconds'}`;
} else if (minutes < 60) {
res = `${minutes} ${minutes === 1 ? 'minute' : 'minutes'}`;
} else if (hours < 24) {
res = `${hours} ${hours === 1 ? 'hour' : 'hours'}`;
} else if (days < 30) {
res = `${days} ${days === 1 ? 'day' : 'days'}`;
} else if (months < 12) {
res = `${months} ${months === 1 ? 'month' : 'months'}`;
} else {
res = `${years} ${years === 1 ? 'year' : 'years'}`;
}
return formatter ? formatter(res) : res;
}
const generateRqlQuery = (filterQueries, sortQueries) => {
let queryString = '';
if (filterQueries) {
Object.entries(filterQueries).forEach(([key, value]) => {
if (Array.isArray(value)) {
queryString += value
.map((valueElement) => key + ':' + valueElement)
.join(' ');
} else {
queryString += key + ':' + value;
}
queryString += ' ';
});
}
if (sortQueries) {
Object.entries(sortQueries).forEach(([key, value]) => {
queryString += 'sort:' + key + '-' + value;
});
}
return queryString.trim();
};