From 016f369ba27c6b0a08b68c37f1c9833fdfcb2220 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Tue, 10 Aug 2021 15:50:17 +0200 Subject: [PATCH] Address review comments --- app/javascript/packs/application.ts | 2 -- .../retrospring/features/answerbox/index.ts | 28 +++++++++---------- .../retrospring/features/answerbox/share.ts | 8 +++--- app/javascript/retrospring/utilities/noop.ts | 3 ++ 4 files changed, 20 insertions(+), 21 deletions(-) create mode 100644 app/javascript/retrospring/utilities/noop.ts diff --git a/app/javascript/packs/application.ts b/app/javascript/packs/application.ts index 16e991ae..bcfeddc4 100644 --- a/app/javascript/packs/application.ts +++ b/app/javascript/packs/application.ts @@ -3,5 +3,3 @@ import initAnswerbox from '../retrospring/features/answerbox/index'; start(); document.addEventListener('turbolinks:load', initAnswerbox); -document.addEventListener('turbolinks:load', initAnswerbox); -document.addEventListener('turbolinks:load', initAnswerbox); \ No newline at end of file diff --git a/app/javascript/retrospring/features/answerbox/index.ts b/app/javascript/retrospring/features/answerbox/index.ts index 657dd646..1f1a2ff9 100644 --- a/app/javascript/retrospring/features/answerbox/index.ts +++ b/app/javascript/retrospring/features/answerbox/index.ts @@ -2,19 +2,17 @@ import registerEvents from "retrospring/utilities/registerEvents"; import {createShareEvent} from "./share"; export default (): void => { - if (!('share' in navigator)) { - return; + if ('share' in navigator) { + document.body.classList.add('cap-web-share'); + const entries: NodeList = document.querySelectorAll('.answerbox:not(.js-initialized)'); + + entries.forEach((element: HTMLElement) => { + console.log(element) + registerEvents([ + { type: 'click', target: element.querySelector('[data-action=ab-share]'), handler: createShareEvent(element) } + ]); + + element.classList.add('js-initialized'); + }); } - - document.body.classList.add('cap-web-share'); - const entries: NodeList = document.querySelectorAll('.answerbox:not(.js-initialized)'); - - entries.forEach((element: HTMLElement) => { - console.log(element) - registerEvents([ - { type: 'click', target: element.querySelector('[data-action=ab-share]'), handler: createShareEvent(element) } - ]); - - element.classList.add('js-initialized'); - }); -} \ No newline at end of file +} diff --git a/app/javascript/retrospring/features/answerbox/share.ts b/app/javascript/retrospring/features/answerbox/share.ts index fd6c8d8f..351eea27 100644 --- a/app/javascript/retrospring/features/answerbox/share.ts +++ b/app/javascript/retrospring/features/answerbox/share.ts @@ -1,11 +1,11 @@ +import noop from 'retrospring/utilities/noop'; + export function createShareEvent(answerbox: HTMLElement): () => void { return function (): void { navigator.share({ url: answerbox.querySelector('.answerbox__answer-date > a').href - }).then(() => { - // do nothing, prevents exce ption from being thrown - }).catch(() => { - // do nothing, prevents exception from being thrown }) + .then(noop) + .catch(noop) } } diff --git a/app/javascript/retrospring/utilities/noop.ts b/app/javascript/retrospring/utilities/noop.ts new file mode 100644 index 00000000..260582c8 --- /dev/null +++ b/app/javascript/retrospring/utilities/noop.ts @@ -0,0 +1,3 @@ +export default () => void { + // do nothing +}