feat(exam-correct): overwrite request cells from response

This commit is contained in:
Gregor Kleen 2020-02-07 18:40:50 +01:00
parent 6fc0262d2d
commit c8edbb395b

View File

@ -149,7 +149,7 @@ export class ExamCorrect {
}).then(
(response) => response.json()
).then(
(response) => this._processResponse(response, body.user)
(response) => this._processResponse(body, response, body.user)
).catch((error) => {
console.error('Error while validating user input', error);
});
@ -194,7 +194,7 @@ export class ExamCorrect {
if (Object.keys(results).length <= 0) return;
const rowInfo = {
users: [userId || user],
users: [user],
results: results,
status: STATUS.LOADING,
};
@ -216,13 +216,13 @@ export class ExamCorrect {
}).then(
(response) => response.json()
).then(
(response) => this._processResponse(response, body.user, results)
(response) => this._processResponse(body, response, user, results)
).catch((error) => {
console.error('Error while processing response', error);
});
}
_processResponse(response, user, results) {
_processResponse(request, response, user, results, targetRow) {
if (response) {
if (response.status === 'no-op') {
if (response.users) {
@ -286,13 +286,14 @@ export class ExamCorrect {
console.log('response', response);
for (let row of [...this._element.rows]) {
const candidateRows = (targetRow && [targetRow]) || [...this._element.rows];
for (let row of candidateRows) {
let userElem = row.cells.item(this._cIndices.get('user'));
const userIdent = userElem && userElem.getAttribute(EXAM_CORRECT_USER_ATTR); // TODO use other attribute identifier
if (userIdent === user) {
console.log('response-update', row);
let status = STATUS.FAILURE;
switch (response.status) {
// TODO fetch update time from response and replace
case 'success':
status = STATUS.SUCCESS;
if (response.user) {
@ -304,11 +305,9 @@ export class ExamCorrect {
newEntry.users = [response.user];
newEntry.results = response.results;
}
// TODO replace results with results from response
// TODO set edit button visibility
break;
case 'ambiguous':
// TODO show tooltip with error message
// TODO set edit button visibility
status = STATUS.AMBIGUOUS;
if (response.users) {
@ -320,7 +319,7 @@ export class ExamCorrect {
break;
case 'failure':
status = STATUS.FAILURE;
newEntry.users = [user];
newEntry.users = (response.user && [response.user]) || null;
newEntry.results = results;
newEntry.message = response.message || null;
break;
@ -334,10 +333,36 @@ export class ExamCorrect {
});
newEntry.status = status || STATUS.FAILURE;
newEntry.date = response.time || moment().utc().format();
if (newEntry.message) {
const messageElem = document.createTextNode(newEntry.message);
row.querySelector(`.${EXAM_CORRECT_STATUS_CELL_CLASS}`).appendChild(messageElem);
const statusCell = row.querySelector(`.${EXAM_CORRECT_STATUS_CELL_CLASS}`);
const messageElem = statusCell.querySelector('.uw-exam-correct--message');
if (messageElem) {
statusCell.removeChild(messageElem);
}
if (newEntry.message) {
const messageElem = document.createElement('SPAN');
messageElem.classList.add('uw-exam-correct--message');
const messageText = document.createTextNode(newEntry.message);
messageElem.appendChild(messageText);
statusCell.appendChild(messageElem);
}
const userCell = row.querySelector('.uw-exam-correct--user-cell');
if (userCell && newEntry.users && newEntry.users.length === 1) {
const user = newEntry.users[0];
userCell.innerHTML = userToHTML(user);
userCell.setAttribute(EXAM_CORRECT_USER_ATTR, user);
} else if (userCell && newEntry.users) {
row.replaceChild(userCell, this._showUserList(row, newEntry.users, request.results));
}
for (let [k, v] of Object.entries(newEntry.results)) {
const resultCell = row.cells.item(this._cIndices.get(k));
if (v.result)
resultCell.innerHTML = v.result;
}
savedEntries.push(newEntry);
this._storageManager.save('entries', savedEntries);
return;
@ -386,7 +411,7 @@ export class ExamCorrect {
const timeElem = row.cells.item(0);
timeElem.innerHTML = now.format(this._dateFormat);
timeElem.classList.add('exam-correct--local-time');
const userElem = row.cells.item(1);
const userElem = row.cells.item(this._cIndices.get('user'));
const statusElem = row.querySelector('.exam-correct--ambiguous');
setStatus(statusElem, STATUS.LOADING);
@ -402,35 +427,8 @@ export class ExamCorrect {
body: JSON.stringify(body),
}).then(
(response) => response.json()
).then((response) => {
switch (response.status) {
case 'success': {
userElem.innerHTML = userToHTML(response.user);
// TODO replace part results with results from server
timeElem.innerHTML = moment(response.time).format(this._dateFormat);
timeElem.classList.remove('exam-correct--local-time');
setStatus(statusElem, STATUS.SUCCESS);
const savedEntries = this._storageManager.load('entries');
for (let i = 0; i < savedEntries.length; i++) {
for (let user of savedEntries[i].users) {
if (user.id === response.user.id) {
savedEntries[i] = {
users: [response.user],
results: response.results,
status: STATUS.SUCCESS,
date: response.time,
};
break;
}
}
}
this._storageManager.save('entries', savedEntries);
break;
}
default:
// non-success response on request with a uuid => panic and ignore for now
}
}).catch((error) => {
).then((response) => this._processResponse(body, response, userElem.getAttribute(EXAM_CORRECT_USER_ATTR), results, row)
).catch((error) => {
console.error(error);
});
}
@ -452,6 +450,7 @@ export class ExamCorrect {
cells.set(this._cIndices.get('date'), dateCell);
let userCell = document.createElement('TD');
userCell.classList.add('uw-exam-correct--user-cell');
if (!rowInfo.users || rowInfo.users.length === 0) {
console.error('Found rowInfo without users info!');
} else if (rowInfo.users.length === 1) {