Use Promise for improveOSM comments

This commit is contained in:
SilentSpike
2020-02-14 16:11:32 +00:00
parent 9d6ade23b5
commit d200d0fce2
2 changed files with 59 additions and 63 deletions

View File

@@ -325,39 +325,33 @@ export default {
});
},
getComments(d, callback) {
getComments(item) {
// If comments already retrieved no need to do so again
if (d.comments) {
if (callback) callback({}, d);
return;
if (item.comments) {
return Promise.resolve(item);
}
const key = d.issueKey;
const key = item.issueKey;
let qParams = {};
if (key === 'ow') {
qParams = d.identifier;
qParams = item.identifier;
} else if (key === 'mr') {
qParams.tileX = d.identifier.x;
qParams.tileY = d.identifier.y;
qParams.tileX = item.identifier.x;
qParams.tileY = item.identifier.y;
} else if (key === 'tr') {
qParams.targetId = d.identifier;
qParams.targetId = item.identifier;
}
const url = `${_impOsmUrls[key]}/retrieveComments?` + utilQsString(qParams);
const after = data => {
// Assign directly for immediate use in the callback
const cacheComments = data => {
// Assign directly for immediate use afterwards
// comments are served newest to oldest
d.comments = data.comments ? data.comments.reverse() : [];
this.replaceItem(d);
if (callback) callback(null, d);
item.comments = data.comments ? data.comments.reverse() : [];
this.replaceItem(item);
};
d3_json(url)
.then(after)
.catch(err => {
if (callback) callback(err.message);
});
return d3_json(url).then(cacheComments).then(() => item);
},
postUpdate(d, callback) {

View File

@@ -19,57 +19,59 @@ export function uiImproveOsmComments() {
.merge(comments);
// must retrieve comments from API before they can be displayed
services.improveOSM.getComments(_qaItem, (err, d) => {
if (!d.comments) return; // nothing to do here
services.improveOSM.getComments(_qaItem)
.then(d => {
if (!d.comments) return; // nothing to do here
const commentEnter = comments.selectAll('.comment')
.data(d.comments)
.enter()
.append('div')
.attr('class', 'comment');
const commentEnter = comments.selectAll('.comment')
.data(d.comments)
.enter()
.append('div')
.attr('class', 'comment');
commentEnter
.append('div')
.attr('class', 'comment-avatar')
.call(svgIcon('#iD-icon-avatar', 'comment-avatar-icon'));
commentEnter
.append('div')
.attr('class', 'comment-avatar')
.call(svgIcon('#iD-icon-avatar', 'comment-avatar-icon'));
const mainEnter = commentEnter
.append('div')
.attr('class', 'comment-main');
const mainEnter = commentEnter
.append('div')
.attr('class', 'comment-main');
const metadataEnter = mainEnter
.append('div')
.attr('class', 'comment-metadata');
const metadataEnter = mainEnter
.append('div')
.attr('class', 'comment-metadata');
metadataEnter
.append('div')
.attr('class', 'comment-author')
.each(function(d) {
const osm = services.osm;
let selection = d3_select(this);
if (osm && d.username) {
selection = selection
.append('a')
.attr('class', 'comment-author-link')
.attr('href', osm.userURL(d.username))
.attr('tabindex', -1)
.attr('target', '_blank');
}
selection
.text(d => d.username);
});
metadataEnter
.append('div')
.attr('class', 'comment-author')
.each(function(d) {
const osm = services.osm;
let selection = d3_select(this);
if (osm && d.username) {
selection = selection
.append('a')
.attr('class', 'comment-author-link')
.attr('href', osm.userURL(d.username))
.attr('tabindex', -1)
.attr('target', '_blank');
}
selection
.text(d => d.username);
});
metadataEnter
.append('div')
.attr('class', 'comment-date')
.text(d => t('note.status.commented', { when: localeDateString(d.timestamp) }));
metadataEnter
.append('div')
.attr('class', 'comment-date')
.text(d => t('note.status.commented', { when: localeDateString(d.timestamp) }));
mainEnter
.append('div')
.attr('class', 'comment-text')
.append('p')
.text(d => d.text);
});
mainEnter
.append('div')
.attr('class', 'comment-text')
.append('p')
.text(d => d.text);
})
.catch(err => {}); // TODO: Handle failed json request gracefully in some way
}
function localeDateString(s) {