forked from codelitdev/courselit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
151 lines (129 loc) · 3.92 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import fetch from "isomorphic-unfetch";
import {
URL_EXTENTION_POSTS,
URL_EXTENTION_COURSES,
permissions,
} from "../config/constants.js";
import { RichText as TextEditor } from "@courselit/components-library";
export const queryGraphQL = async (url, query, token) => {
const options = {
method: "POST",
headers: token
? {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
}
: { "Content-Type": "application/json" },
body: JSON.stringify({ query }),
};
let response = await fetch(url, options);
response = await response.json();
if (response.errors && response.errors.length > 0) {
throw new Error(response.errors[0].message);
}
return response.data;
};
export const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
export const queryGraphQLWithUIEffects = (
backend,
dispatch,
networkAction,
token
) => async (query) => {
try {
dispatch(networkAction(false));
const response = await queryGraphQL(`${backend}/graph`, query, token);
return response;
} finally {
dispatch(networkAction(false));
}
};
export const formattedLocaleDate = (epochString) =>
new Date(Number(epochString)).toLocaleString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
// Regex copied from: https://stackoverflow.com/a/48675160/942589
export const makeGraphQLQueryStringFromJSObject = (obj) =>
JSON.stringify(obj).replace(/"([^(")"]+)":/g, "$1:");
export const formulateCourseUrl = (course, backend = "") =>
`${backend}/${course.isBlog ? URL_EXTENTION_POSTS : URL_EXTENTION_COURSES}/${
course.courseId
}/${course.slug}`;
export const getPostDescriptionSnippet = (rawDraftJSContentState) => {
const firstSentence = TextEditor.hydrate({ data: rawDraftJSContentState })
.getCurrentContent()
.getPlainText()
.split(".")[0];
return firstSentence ? firstSentence + "." : firstSentence;
};
export const getGraphQLQueryFields = (
jsObj,
fieldsNotPutBetweenQuotes = []
) => {
let queryString = "{";
for (const i of Object.keys(jsObj)) {
if (jsObj[i] !== undefined) {
queryString += fieldsNotPutBetweenQuotes.includes(i)
? `${i}: ${jsObj[i]},`
: `${i}: "${jsObj[i]}",`;
}
}
queryString += "}";
return queryString;
};
export const getObjectContainingOnlyChangedFields = (baseline, obj) => {
const result = {};
for (const i of Object.keys(baseline)) {
if (baseline[i] !== obj[i]) {
result[i] = obj[i];
}
}
return result;
};
export const areObjectsDifferent = (baseline, obj) => {
const onlyChangedFields = getObjectContainingOnlyChangedFields(baseline, obj);
return !!Object.keys(onlyChangedFields).length;
};
export const getAddress = (host) => {
return {
domain: extractDomainFromURL(host),
backend: getBackendAddress(host),
frontend: `http://${host}`,
};
};
export const getBackendAddress = (host) => {
const domain = extractDomainFromURL(host);
if (process.env.NODE_ENV === "production") {
return `${
process.env.INSECURE === "true" ? "http" : "https"
}://${domain}/api`;
} else {
return `http://${domain}:8000`;
}
};
export const checkPermission = (actualPermissions, desiredPermissions) =>
actualPermissions.some((permission) =>
desiredPermissions.includes(permission)
);
const extractDomainFromURL = (host) => {
return host.split(":")[0];
};
export const canAccessDashboard = (profile) => {
return checkPermission(profile.permissions, [
permissions.manageCourse,
permissions.manageAnyCourse,
permissions.manageMedia,
permissions.manageAnyMedia,
permissions.manageLayout,
permissions.manageThemes,
permissions.manageMenus,
permissions.manageWidgets,
permissions.manageSettings,
permissions.manageUsers,
permissions.viewAnyMedia,
]);
};
export const constructThumbnailUrlFromFileUrl = (url) =>
url ? url.replace(url.split("/").pop(), "thumb.webp") : null;