2022-11-10 23:32:26 +01:00
|
|
|
|
2022-11-13 21:13:58 +01:00
|
|
|
function getEmbeddedVariable(variableName) {
|
|
|
|
|
const value = document.body.getAttribute(`data-${variableName}`);
|
|
|
|
|
if (value === null) {
|
|
|
|
|
throw new Error(`No ${variableName} embedded variable detected`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 23:32:26 +01:00
|
|
|
const apiFactory = () => {
|
2022-11-13 21:13:58 +01:00
|
|
|
const apiBasePath = getEmbeddedVariable("apipath");
|
2022-11-10 23:32:26 +01:00
|
|
|
let token = "";
|
|
|
|
|
|
|
|
|
|
const headers = () => ({
|
2022-11-11 12:10:36 +01:00
|
|
|
"Authorization": "Bearer " + token
|
2022-11-10 23:32:26 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const logIn = async (password, remember) => {
|
|
|
|
|
token = password;
|
2022-11-13 21:13:58 +01:00
|
|
|
const response = await fetch(apiBasePath + "list", {
|
2022-11-10 23:32:26 +01:00
|
|
|
headers: headers()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
|
if (remember === true) {
|
2022-11-13 21:13:58 +01:00
|
|
|
localStorage.setItem("token", token);
|
2022-11-10 23:32:26 +01:00
|
|
|
}
|
2022-11-13 21:13:58 +01:00
|
|
|
|
2022-11-10 23:32:26 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token = "";
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-11 12:10:36 +01:00
|
|
|
const checkLogin = async () => {
|
|
|
|
|
const token = localStorage.getItem("token");
|
|
|
|
|
if (token) {
|
2022-11-13 21:13:58 +01:00
|
|
|
return await logIn(token);
|
2022-11-11 12:10:36 +01:00
|
|
|
}
|
2022-11-13 21:13:58 +01:00
|
|
|
|
2022-11-11 12:10:36 +01:00
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-10 23:32:26 +01:00
|
|
|
const logOut = () => {
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const list = async () => {
|
2022-11-13 21:13:58 +01:00
|
|
|
const response = await fetch(apiBasePath + "list", {
|
2022-11-10 23:32:26 +01:00
|
|
|
headers: headers()
|
|
|
|
|
});
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const check = async (containerId) => {
|
|
|
|
|
const requestData = {
|
|
|
|
|
ContainerId: containerId
|
|
|
|
|
};
|
2022-11-13 21:13:58 +01:00
|
|
|
const response = await fetch(apiBasePath + "check", {
|
2022-11-11 12:10:36 +01:00
|
|
|
method: "POST",
|
2022-11-10 23:32:26 +01:00
|
|
|
headers: {
|
|
|
|
|
...headers(),
|
2022-11-11 12:10:36 +01:00
|
|
|
"Content-Type": "application/json",
|
2022-11-10 23:32:26 +01:00
|
|
|
},
|
|
|
|
|
body: JSON.stringify(requestData)
|
|
|
|
|
});
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const update = async (images) => {
|
2022-11-13 21:13:58 +01:00
|
|
|
let updateUrl = new URL(apiBasePath + "/update");
|
2022-11-10 23:32:26 +01:00
|
|
|
|
|
|
|
|
if (images instanceof Array) {
|
|
|
|
|
images.map(image => updateUrl.searchParams.append("image", image));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = await fetch(updateUrl.toString(), {
|
|
|
|
|
headers: headers(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return response.ok;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
checkLogin,
|
|
|
|
|
logIn,
|
|
|
|
|
logOut,
|
|
|
|
|
list,
|
|
|
|
|
check,
|
|
|
|
|
update
|
|
|
|
|
}
|
|
|
|
|
};
|