added sessions support
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
export function isSessionPage() {
|
||||
return document.URL.indexOf("/sessions") != -1;
|
||||
}
|
||||
|
||||
export function isRootPage() {
|
||||
return document.URL.indexOf("/sessions") == -1;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
var apiUrl = "https://codeeditor-api.davidssoft.com/";
|
||||
|
||||
function getSessionId() {
|
||||
var localstorageSessionId = localStorage.getItem("sessionId");
|
||||
|
||||
if (!localstorageSessionId) {
|
||||
return setNewSession();
|
||||
}
|
||||
else {
|
||||
var expiration = localStorage.getItem("sessionIdExpiration");
|
||||
|
||||
if (expiration) {
|
||||
var expirationDate = new Date(expiration);
|
||||
|
||||
if (dateDiff(expirationDate, new Date()) > 120) {
|
||||
return setNewSession();
|
||||
}
|
||||
else {
|
||||
localStorage.setItem("sessionIdExpiration", (new Date()).toString());
|
||||
return localstorageSessionId;
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
return setNewSession();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setNewSession() {
|
||||
var newSessionId = uuidv4();
|
||||
localStorage.setItem("sessionIdExpiration", (new Date()).toString());
|
||||
localStorage.setItem("sessionId", newSessionId);
|
||||
|
||||
return newSessionId;
|
||||
}
|
||||
|
||||
function dateDiff(startTime: Date, endTime: Date) {
|
||||
var difference = endTime.getTime() - startTime.getTime(); // This will give difference in milliseconds
|
||||
var resultInMinutes = Math.round(difference / 60000);
|
||||
return resultInMinutes;
|
||||
}
|
||||
|
||||
function uuidv4() {
|
||||
//@ts-ignore
|
||||
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
|
||||
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
||||
);
|
||||
}
|
||||
|
||||
export function getSessions(onGet: (sessions: any[]) => void) {
|
||||
fetch(apiUrl + "session/all").then((res) => {
|
||||
res.json().then((jsonRes) => {
|
||||
onGet(jsonRes as any);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function getSessionData(sessionId: string, onGet: (sessions: any[]) => void) {
|
||||
fetch(apiUrl + "session/data/" + sessionId, { method: "GET" }).then((res) => {
|
||||
res.json().then((jsonRes) => {
|
||||
onGet(jsonRes as any);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function setSessionData(language: string, input: any, onSet?: () => void) {
|
||||
var type = 0;
|
||||
|
||||
if (language.toLowerCase() == "javascript") {
|
||||
type = 0;
|
||||
}
|
||||
else if (language.toLowerCase() == "html") {
|
||||
type = 1;
|
||||
}
|
||||
else if (language.toLowerCase() == "css") {
|
||||
type = 2;
|
||||
}
|
||||
|
||||
fetch(apiUrl + "session/set", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ sessionId: getSessionId(), timestamp: new Date(), type: type, data: input }) }).then((res) => {
|
||||
if (onSet) {
|
||||
onSet();
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user