Assignment | Score | Ticket | Comments |
---|
<script>
const urlParams = new URLSearchParams(window.location.search);
const teamId = urlParams.get('id');
var a = document.getElementById('addAssignmentLink');
a.href = `https://rebecca-123.github.io/mrr_frontend/addAssignment?id=${teamId}`
// prepare fetch urls
const team_url = `https://mrr.rebeccaaa.tk/database/reviews/${teamId}`;
const get_url = team_url + "/";
const teamContainer = document.getElementById("users");
// prepare fetch GET options
const options = {
method: 'GET', // *GET, POST, PUT, DELETE, etc.
// mode: 'cors', // no-cors, *cors, same-origin
cache: 'default', // *default, no-cache, reload, force-cache, only-if-cached
// credentials: 'same-origin', // include, same-origin, omit
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
};
// fetch the API
fetch(get_url, options)
// response is a RESTful "promise" on any successful fetch
.then(response => {
// check for response errors
if (response.status !== 200) {
error('GET API response failure: ' + response.status);
return;
}
// valid response will have JSON data
response.json().then(data => {
const assignments = data;
for (const assign of assignments) {
console.log(assign);
// columns
const tr = document.createElement("tr");
const assignment = document.createElement("td");
const score = document.createElement("td");
const ticket = document.createElement("td");
const comments = document.createElement("td");
// url containers
// accessing JSON values
assignment.innerHTML = assign.assignment;
score.innerHTML = assign.score;
ticket.innerHTML = assign.ticket;
comments.innerHTML = assign.comments;
tr.appendChild(assignment);
tr.appendChild(score);
tr.appendChild(ticket);
tr.appendChild(comments);
// add row to table
teamContainer.appendChild(tr);
}
})
})
// catch fetch errors (ie Nginx ACCESS to server blocked)
.catch(err => {
error(err + " " + get_url);
});
// Something went wrong with actions or responses
function error(err) {
// log as Error in console
console.error(err);
// append error to resultContainer
const tr = document.createElement("tr");
const td = document.createElement("td");
td.innerHTML = err;
tr.appendChild(td);
teamContainer.appendChild(tr);
}
function addAssignment() {
const urlParams = new URLSearchParams(window.location.search);
const teamId = urlParams.get('id');
const assignment = document.getElementById('assignment').value;
const score = document.getElementById('score').value;
const ticket = document.getElementById('ticket').value;
const comments = document.getElementById('comments').value;
const user = {
assignment: assignment,
score: score,
ticket: ticket,
comments: comments,
};
const options = {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'include',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(user), // convert to JSON
};
const endpoint = `https://mrr.rebeccaaa.tk/database/addreview/${teamId}`;
fetch(endpoint, options)
.then(response => response.json())
.then(data => {
console.log('User creation response:', data);
// Handle the response from the server
// You can perform further actions based on the response
})
}
</script>