From ab70ac38fdf7974fe316daa76a3fbd077cc5314c Mon Sep 17 00:00:00 2001 From: Benjamin Goering Date: Wed, 1 Mar 2017 16:07:25 +0800 Subject: [PATCH] If fail to addremov best comment tag, show user an error --- client/coral-embed-stream/src/Comment.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/client/coral-embed-stream/src/Comment.js b/client/coral-embed-stream/src/Comment.js index 1e4802470..7f3265f5f 100644 --- a/client/coral-embed-stream/src/Comment.js +++ b/client/coral-embed-stream/src/Comment.js @@ -113,14 +113,26 @@ class Comment extends React.Component { const flag = getActionSummary('FlagActionSummary', comment); const dontagree = getActionSummary('DontAgreeActionSummary', comment); - const addBestTag = () => addCommentTag({ + // call a function, and if it errors, call addNotification('error', ...) (e.g. to show user a snackbar) + const notifyOnError = (fn, errorToMessage) => async () => { + if (typeof errorToMessage !== 'function') {errorToMessage = (error) => error.message;} + try { + return await fn(); + } catch (error) { + addNotification('error', errorToMessage(error)); + throw error; + } + }; + + const addBestTag = notifyOnError(() => addCommentTag({ id: comment.id, tag: BEST_TAG, - }); - const removeBestTag = () => removeCommentTag({ + }), () => 'Failed to tag comment as best'); + + const removeBestTag = notifyOnError(() => removeCommentTag({ id: comment.id, tag: BEST_TAG, - }); + }), () => 'Failed to remove best comment tag'); return (