From bfe1851969ff04a4887d01ae8e3acc3cf8532fbd Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Sun, 19 Nov 2023 16:10:22 +0500 Subject: [PATCH] #1448, add notification toast for copy success & failure --- .../retrospring/controllers/clipboard_controller.ts | 10 +++++++++- config/locales/frontend.en.yml | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/javascript/retrospring/controllers/clipboard_controller.ts b/app/javascript/retrospring/controllers/clipboard_controller.ts index f6227d04..23bfc46c 100644 --- a/app/javascript/retrospring/controllers/clipboard_controller.ts +++ b/app/javascript/retrospring/controllers/clipboard_controller.ts @@ -1,4 +1,6 @@ import { Controller } from "@hotwired/stimulus"; +import I18n from 'retrospring/i18n'; +import { showErrorNotification, showNotification } from "retrospring/utilities/notifications"; export default class extends Controller { @@ -9,6 +11,12 @@ export default class extends Controller { declare readonly copyValue: string; async copy(){ - await navigator.clipboard.writeText(this.copyValue); + try { + await navigator.clipboard.writeText(this.copyValue); + showNotification(I18n.translate("frontend.clipboard_copy.success")); + } catch (error) { + console.log(error); + showErrorNotification(I18n.translate("frontend.clipboard_copy.error")); + } } } diff --git a/config/locales/frontend.en.yml b/config/locales/frontend.en.yml index 327e780b..fe55281a 100644 --- a/config/locales/frontend.en.yml +++ b/config/locales/frontend.en.yml @@ -62,3 +62,6 @@ en: title: "Are you sure you want to report this %{type}?" text: "A moderator will review your report and decide what happens.\nYou can optionally specify a reason." input: "Specify a reason…" + clipboard_copy: + success: "Content copied to clipboard." + error: "Failed to copy content to clipboard."