From d37b9c5aeeb9bc9925a63832a92d638aea5a04e5 Mon Sep 17 00:00:00 2001 From: AmbrishRamachandiran Date: Wed, 26 Jul 2023 10:17:53 +0530 Subject: [PATCH] Limit the use of the same shortcut name when adding a shortcut Signed-off-by: AmbrishRamachandiran --- plugins/shortcuts/src/AddShortcut.tsx | 7 +++++-- plugins/shortcuts/src/EditShortcut.tsx | 23 ++++++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/plugins/shortcuts/src/AddShortcut.tsx b/plugins/shortcuts/src/AddShortcut.tsx index 70988c49eb..32a5aaa947 100644 --- a/plugins/shortcuts/src/AddShortcut.tsx +++ b/plugins/shortcuts/src/AddShortcut.tsx @@ -63,11 +63,14 @@ export const AddShortcut = ({ const analytics = useAnalytics(); const handleSave: SubmitHandler = async ({ url, title }) => { - analytics.captureEvent('click', `Clicked 'Save' in AddShortcut`); + if (!api.get().some(shortcutTitle => shortcutTitle.title === title)) { + analytics.captureEvent('click', `Clicked 'Save' in AddShortcut`); + } const shortcut: Omit = { url, title }; + const shortcutData = api.get(); try { - if (api.get().some(shortcutTitle => shortcutTitle.title === title)) { + if (shortcutData.some(shortcutTitle => shortcutTitle.title === title)) { alertApi.post({ message: `Shortcut title already exist`, severity: 'error', diff --git a/plugins/shortcuts/src/EditShortcut.tsx b/plugins/shortcuts/src/EditShortcut.tsx index 9f2ffe10be..df0476f5db 100644 --- a/plugins/shortcuts/src/EditShortcut.tsx +++ b/plugins/shortcuts/src/EditShortcut.tsx @@ -62,7 +62,9 @@ export const EditShortcut = ({ const analytics = useAnalytics(); const handleSave: SubmitHandler = async ({ url, title }) => { - analytics.captureEvent('click', `Clicked 'Save' in Edit Shortcut`); + if (!api.get().some(shortcutTitle => shortcutTitle.title === title)) { + analytics.captureEvent('click', `Clicked 'Save' in Edit Shortcut`); + } const newShortcut: Shortcut = { ...shortcut, url, @@ -70,12 +72,19 @@ export const EditShortcut = ({ }; try { - await api.update(newShortcut); - alertApi.post({ - message: `Updated shortcut '${title}'`, - severity: 'success', - display: 'transient', - }); + if (api.get().some(shortcutTitle => shortcutTitle.title === title)) { + alertApi.post({ + message: `Shortcut title already exist`, + severity: 'error', + }); + } else { + await api.update(newShortcut); + alertApi.post({ + message: `Updated shortcut '${title}'`, + severity: 'success', + display: 'transient', + }); + } } catch (error) { alertApi.post({ message: `Could not update shortcut: ${error.message}`,