Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling using async/await #1320

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 92 additions & 125 deletions frontend/src/components/utils/Utils.js
Original file line number Diff line number Diff line change
@@ -1,213 +1,180 @@
import config from "../../config.json";

export const getFromOpenElisServer = (endPoint, callback) => {
fetch(
config.serverBaseUrl + endPoint,

{
//includes the browser sessionId in the Header for Authentication on the backend server
export const getFromOpenElisServer = async (endPoint, callback) => {
try {
const response = await fetch(config.serverBaseUrl + endPoint, {
credentials: "include",
method: "GET",
},
)
.then((response) => {
console.debug("checking response");
// if (response.url.includes("LoginPage")) {
// throw "No Login Session";
// }
const contentType = response.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
return response.json().then((jsonResp) => {
callback(jsonResp);
});
} else {
callback();
}
})
.catch((error) => {
console.error(error);
});

console.debug("checking response");
const contentType = response.headers.get("content-type");

if (contentType && contentType.indexOf("application/json") !== -1) {
const jsonResp = await response.json();
callback(jsonResp);
} else {
callback();
}
} catch (error) {
console.error(error);
}
};

export const postToOpenElisServer = (
export const postToOpenElisServer = async (
endPoint,
payLoad,
callback,
extraParams,
) => {
fetch(
config.serverBaseUrl + endPoint,

{
//includes the browser sessionId in the Header for Authentication on the backend server
try {
const response = await fetch(config.serverBaseUrl + endPoint, {
credentials: "include",
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRF-Token": localStorage.getItem("CSRF"),
},
body: payLoad,
},
)
.then((response) => response.status)
.then((status) => {
callback(status, extraParams);
})
.catch((error) => {
console.error(error);
});

const status = response.status;
callback(status, extraParams);
} catch (error) {
console.error(error);
}
};

export const postToOpenElisServerFullResponse = (
export const postToOpenElisServerFullResponse = async (
endPoint,
payLoad,
callback,
extraParams,
) => {
fetch(
config.serverBaseUrl + endPoint,

{
//includes the browser sessionId in the Header for Authentication on the backend server
try {
const response = await fetch(config.serverBaseUrl + endPoint, {
credentials: "include",
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRF-Token": localStorage.getItem("CSRF"),
},
body: payLoad,
},
)
.then((response) => callback(response, extraParams))
.catch((error) => {
console.error(error);
});

callback(response, extraParams);
} catch (error) {
console.error(error);
}
};

export const postToOpenElisServerFormData = (
export const postToOpenElisServerFormData = async (
endPoint,
formData,
callback,
extraParams,
) => {
fetch(
config.serverBaseUrl + endPoint,

{
try {
const response = await fetch(config.serverBaseUrl + endPoint, {
credentials: "include",
method: "POST",
headers: {
"X-CSRF-Token": localStorage.getItem("CSRF"),
},
body: formData,
},
)
.then((response) => response.status)
.then((status) => {
callback(status, extraParams);
})
.catch((error) => {
console.error(error);
});

const status = response.status;
callback(status, extraParams);
} catch (error) {
console.error(error);
}
};

export const postToOpenElisServerJsonResponse = (
export const postToOpenElisServerJsonResponse = async (
endPoint,
payLoad,
callback,
extraParams,
) => {
fetch(
config.serverBaseUrl + endPoint,

{
//includes the browser sessionId in the Header for Authentication on the backend server
try {
const response = await fetch(config.serverBaseUrl + endPoint, {
credentials: "include",
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRF-Token": localStorage.getItem("CSRF"),
},
body: payLoad,
},
)
.then((response) => response.json())
.then((json) => {
callback(json, extraParams);
})
.catch((error) => {
console.error(error);
});

const json = await response.json();
callback(json, extraParams);
} catch (error) {
console.error(error);
}
};

//provides Synchronous calls to the api
export const getFromOpenElisServerSync = (endPoint, callback) => {
const request = new XMLHttpRequest();
request.open("GET", config.serverBaseUrl + endPoint, false);
request.setRequestHeader("credentials", "include");
request.send();
// if (request.response.url.includes("LoginPage")) {
// throw "No Login Session";
// }
return callback(JSON.parse(request.response));
};

export const postToOpenElisServerForPDF = (endPoint, payLoad, callback) => {
fetch(
config.serverBaseUrl + endPoint,

{
//includes the browser sessionId in the Header for Authentication on the backend server
export const postToOpenElisServerForPDF = async (
endPoint,
payLoad,
callback,
) => {
try {
const response = await fetch(config.serverBaseUrl + endPoint, {
credentials: "include",
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRF-Token": localStorage.getItem("CSRF"),
},
body: payLoad,
},
)
.then((response) => response.blob())
.then((blob) => {
callback(true, blob);
let link = document.createElement("a");
link.href = window.URL.createObjectURL(blob, { type: "application/pdf" });
link.target = "_blank";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
})
.catch((error) => {
callback(false);
console.error(error);
});
};

export const putToOpenElisServer = (endPoint, payLoad, callback) => {
// Build the request options
let options = {
// includes the browser sessionId in the Header for Authentication on the backend server
credentials: "include",
method: "PUT",
headers: {
"Content-Type": "application/json",
"X-CSRF-Token": localStorage.getItem("CSRF"),
},
};

// Include the body only if payLoad is provided
if (payLoad) {
options.body = payLoad;
const blob = await response.blob();
callback(true, blob);

const link = document.createElement("a");
link.href = window.URL.createObjectURL(blob, { type: "application/pdf" });
link.target = "_blank";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} catch (error) {
callback(false);
console.error(error);
}
};

fetch(config.serverBaseUrl + endPoint, options)
.then((response) => response.status)
.then((status) => {
callback(status);
})
.catch((error) => {
console.error(error);
});
export const putToOpenElisServer = async (endPoint, payLoad, callback) => {
try {
const options = {
credentials: "include",
method: "PUT",
headers: {
"Content-Type": "application/json",
"X-CSRF-Token": localStorage.getItem("CSRF"),
},
};

if (payLoad) {
options.body = payLoad;
}

const response = await fetch(config.serverBaseUrl + endPoint, options);
const status = response.status;
callback(status);
} catch (error) {
console.error(error);
}
};

export const hasRole = (userSessionDetails, role) => {
Expand Down
Loading