2022-10-09 16:02:07 +01:00
|
|
|
let dateOptions = {month: 'short', year: 'numeric'};
|
|
|
|
document.addEventListener('DOMContentLoaded', () =>
|
2022-07-29 20:00:36 +01:00
|
|
|
{
|
2022-09-16 16:49:58 +01:00
|
|
|
// check if the user is logged in, if not redirect to log in
|
2022-10-09 16:02:07 +01:00
|
|
|
/* fetch('/api/user/isLoggedIn').then(res =>
|
|
|
|
{
|
|
|
|
if (!res.ok)
|
|
|
|
{
|
|
|
|
window.location.href = './';
|
|
|
|
}
|
|
|
|
});*/
|
|
|
|
document.querySelector("#dateFrom").max = new Date().toISOString().split("T")[0];
|
|
|
|
|
|
|
|
|
|
|
|
fetch("/api/timelineData/edu").then(res =>
|
2022-07-29 20:00:36 +01:00
|
|
|
{
|
2022-10-09 16:02:07 +01:00
|
|
|
res.json().then(json =>
|
2022-07-29 20:00:36 +01:00
|
|
|
{
|
2022-10-09 16:02:07 +01:00
|
|
|
if (res.ok)
|
|
|
|
{
|
|
|
|
for (let i = 0; i < json.length; i++)
|
|
|
|
{
|
2022-10-09 23:32:50 +01:00
|
|
|
let id = json[i].ID;
|
2022-10-09 16:02:07 +01:00
|
|
|
let timelineItem = document.createElement("form")
|
2022-10-09 23:32:50 +01:00
|
|
|
timelineItem.id = "timelineItem" + id;
|
2022-10-09 16:02:07 +01:00
|
|
|
timelineItem.classList.add("timelineItem");
|
2022-10-09 23:32:50 +01:00
|
|
|
timelineItem.onsubmit = e => updateEduItem(id, e);
|
2022-10-09 16:02:07 +01:00
|
|
|
timelineItem.innerHTML = `
|
|
|
|
<div class="modifyBtnContainer">
|
2022-10-09 23:32:50 +01:00
|
|
|
<button class="edit" type="button" id="edit${id}" onclick="edit(${id})"><i class="fa-solid fa-pen-to-square"></i></button>
|
|
|
|
<button class="delete" type="button" id="delete${id}" onclick="deleteEduItem(${id})"><i class="fa- fa-trash"></i></button>
|
2022-10-09 16:02:07 +01:00
|
|
|
</div>
|
|
|
|
<div class="dateContainer formControl">
|
2022-10-09 23:32:50 +01:00
|
|
|
<input type="date" name="dateFrom${id}" id="dateFrom${id}" onload="this.max = new Date().toISOString().split('T')[0]" value="${json[i]['startPeriod']}">
|
2022-10-09 16:02:07 +01:00
|
|
|
-
|
2022-10-09 23:32:50 +01:00
|
|
|
<input type="date" name="dateTo${id}" id="dateTo${id}" value="${json[i]['endPeriod']}">
|
2022-10-09 16:02:07 +01:00
|
|
|
</div>
|
2022-10-09 23:32:50 +01:00
|
|
|
<h3 class="timelineHeader" id="timelineHeader${id}">${new Date(json[i]["startPeriod"]).toLocaleString('en-gb', dateOptions)} - ${new Date(json[i]["endPeriod"]).toLocaleString('en-gb', dateOptions)}</h3>
|
2022-10-09 16:02:07 +01:00
|
|
|
<div class="gradeContainer formControl">
|
2022-10-09 23:32:50 +01:00
|
|
|
<label for="grade${id}">Grade:</label>
|
|
|
|
<input type="text" name="grade${id}" id="grade${id}" value="${json[i]['grade']}" disabled>
|
2022-10-09 16:02:07 +01:00
|
|
|
</div>
|
|
|
|
<div class="formControl">
|
2022-10-09 23:32:50 +01:00
|
|
|
<textarea class="courseText" name="course${id}" id="course${id}" cols="10" rows="3" disabled>${json[i]['course']}</textarea>
|
2022-10-09 16:02:07 +01:00
|
|
|
</div>
|
|
|
|
|
2022-10-09 23:32:50 +01:00
|
|
|
<div class="error hidden" id="eduError${id}">
|
|
|
|
<button class="close" type="button" onclick="this.parentElement.classList.toggle('hidden');">×</button>
|
2022-10-09 16:02:07 +01:00
|
|
|
<div></div>
|
|
|
|
</div>
|
|
|
|
<input type="submit" value="Change">
|
|
|
|
`;
|
|
|
|
document.getElementById("edu").appendChild(timelineItem);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
document.querySelector("#edu").innerHTML = "No education data found";
|
|
|
|
})
|
|
|
|
});
|
2022-09-16 16:49:58 +01:00
|
|
|
})
|
|
|
|
|
2022-10-09 16:02:07 +01:00
|
|
|
document.querySelector("#navOpen").addEventListener("click", e =>
|
2022-09-16 16:49:58 +01:00
|
|
|
{
|
2022-10-09 16:02:07 +01:00
|
|
|
document.querySelector("nav.sideNav").style.removeProperty("width");
|
|
|
|
document.querySelector("main.editor").style.removeProperty("margin-left");
|
|
|
|
e.target.style.removeProperty("visibility");
|
2022-09-16 16:49:58 +01:00
|
|
|
});
|
|
|
|
|
2022-10-09 16:02:07 +01:00
|
|
|
document.querySelector("#navClose").addEventListener("click", () =>
|
2022-09-16 16:49:58 +01:00
|
|
|
{
|
|
|
|
document.querySelector("nav.sideNav").style.width = "0";
|
|
|
|
document.querySelector("main.editor").style.marginLeft = "0";
|
|
|
|
document.querySelector("#navOpen").style.visibility = "visible";
|
2022-10-09 16:02:07 +01:00
|
|
|
});
|
|
|
|
|
2022-10-09 23:32:50 +01:00
|
|
|
/**
|
|
|
|
* Switches the timeline item between edit and view mode
|
|
|
|
* @param id the id of the timeline item
|
|
|
|
*/
|
|
|
|
function edit(id)
|
2022-10-09 16:02:07 +01:00
|
|
|
{
|
2022-10-09 23:32:50 +01:00
|
|
|
document.querySelector("#timelineItem" + id).classList.toggle("editing");
|
|
|
|
document.querySelector("#grade" + id).toggleAttribute("disabled");
|
|
|
|
document.querySelector("#course" + id).toggleAttribute("disabled");
|
2022-10-09 16:02:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-09 23:32:50 +01:00
|
|
|
function updateEduItem(id, e)
|
2022-10-09 16:02:07 +01:00
|
|
|
{
|
|
|
|
e.preventDefault();
|
|
|
|
|
2022-10-09 23:32:50 +01:00
|
|
|
let data = {}
|
|
|
|
data["dateFrom"] = document.querySelector("#dateFrom" + id).value;
|
|
|
|
data["dateTo"] = document.querySelector("#dateTo" + id).value;
|
|
|
|
data["grade"] = document.querySelector("#grade" + id).value;
|
|
|
|
data["course"] = document.querySelector("#course" + id).value;
|
|
|
|
|
|
|
|
fetch("/api/timelineData/edu/" + id, {
|
|
|
|
method: "PATCH",
|
|
|
|
body: JSON.stringify(data),
|
2022-10-09 16:02:07 +01:00
|
|
|
headers: {
|
2022-10-09 23:32:50 +01:00
|
|
|
"Content-Type": "application/json",
|
2022-10-09 16:02:07 +01:00
|
|
|
"Authorization": "Bearer " + localStorage.getItem("token")
|
|
|
|
}
|
|
|
|
}).then(res =>
|
|
|
|
{
|
|
|
|
if (res.ok)
|
|
|
|
{
|
2022-10-09 23:32:50 +01:00
|
|
|
document.querySelector("#timelineHeader" + id).innerHTML = new Date(document.querySelector("#dateFrom" + id).value).toLocaleString('en-gb', dateOptions) + " - " + new Date(document.querySelector("#dateTo" + id).value).toLocaleString('en-gb', dateOptions);
|
|
|
|
document.querySelector("#timelineItem" + id).classList.toggle("editing");
|
|
|
|
document.querySelector("#grade" + id).setAttribute("disabled", "");
|
|
|
|
document.querySelector("#course" + id).setAttribute("disabled", "");
|
2022-10-09 16:02:07 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-10-09 23:32:50 +01:00
|
|
|
|
|
|
|
if (res.status === 401)
|
|
|
|
{
|
|
|
|
window.location.href = "./";
|
|
|
|
return;
|
|
|
|
}
|
2022-10-09 16:02:07 +01:00
|
|
|
|
|
|
|
res.json().then(json =>
|
|
|
|
{
|
2022-10-09 23:32:50 +01:00
|
|
|
document.querySelector("#eduError" + id).classList.remove("hidden");
|
|
|
|
document.querySelector(`#eduError${id} div`).innerHTML = json.error;
|
|
|
|
});
|
2022-10-09 16:02:07 +01:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|