session-api.ts

113 lines | 2.988 kB Blame History Raw Download
//var apiUrl = "https://codeeditor-api.davidssoft.com/";
var apiUrl = "https://codeeditor-api.azurewebsites.net/";

export function hasSession() {
    var localstorageSessionId = localStorage.getItem("sessionId");

    if (!localstorageSessionId) {
        return false;
    }
    else {
        var expiration = localStorage.getItem("sessionIdExpiration");
        if (expiration) {
            return !sessionIsExpired(expiration);
        }
        else {
            return false;
        }
    }
}

function getSessionId() {
    var localstorageSessionId = localStorage.getItem("sessionId");

    if (!localstorageSessionId) {
        return setNewSession();
    }
    else {
        var expiration = localStorage.getItem("sessionIdExpiration");

        if (expiration) {
            if (sessionIsExpired(expiration)) {
                return setNewSession();
            }
            else {
                localStorage.setItem("sessionIdExpiration", (new Date()).toString());
                return localstorageSessionId;
            }

        }
        else {
            return setNewSession();
        }
    }
}

function sessionIsExpired(expiration: string) {
    return dateDiff(new Date(expiration), new Date()) > 120;
}

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;
    }
    else {
        type = 3;
    }

    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();
        }
    });
}