From d200d0fce23cf88129ed26937dbec5df0251471a Mon Sep 17 00:00:00 2001 From: SilentSpike Date: Fri, 14 Feb 2020 16:11:32 +0000 Subject: [PATCH] Use Promise for improveOSM comments --- modules/services/improveOSM.js | 32 +++++------ modules/ui/improveOSM_comments.js | 90 ++++++++++++++++--------------- 2 files changed, 59 insertions(+), 63 deletions(-) diff --git a/modules/services/improveOSM.js b/modules/services/improveOSM.js index 6f171598a..dfa8711a4 100644 --- a/modules/services/improveOSM.js +++ b/modules/services/improveOSM.js @@ -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) { diff --git a/modules/ui/improveOSM_comments.js b/modules/ui/improveOSM_comments.js index a80d52ddd..e50088e4d 100644 --- a/modules/ui/improveOSM_comments.js +++ b/modules/ui/improveOSM_comments.js @@ -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) {