From ceee1c3ca25377bfac35d137795dc85455bf9fa1 Mon Sep 17 00:00:00 2001 From: AmbrishRamachandiran Date: Wed, 26 Jul 2023 11:28:41 +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 | 8 +++----- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/plugins/shortcuts/src/AddShortcut.tsx b/plugins/shortcuts/src/AddShortcut.tsx index 32a5aaa947..e518a89934 100644 --- a/plugins/shortcuts/src/AddShortcut.tsx +++ b/plugins/shortcuts/src/AddShortcut.tsx @@ -61,20 +61,17 @@ export const AddShortcut = ({ const [formValues, setFormValues] = useState(); const open = Boolean(anchorEl); const analytics = useAnalytics(); + const shortcutData = api.get(); const handleSave: SubmitHandler = async ({ url, title }) => { 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 (shortcutData.some(shortcutTitle => shortcutTitle.title === title)) { - alertApi.post({ - message: `Shortcut title already exist`, - severity: 'error', - }); + throw new Error(`Shortcut Title '${title}' already Exist`); } else { await api.add(shortcut); alertApi.post({ diff --git a/plugins/shortcuts/src/EditShortcut.tsx b/plugins/shortcuts/src/EditShortcut.tsx index df0476f5db..5f1125e37a 100644 --- a/plugins/shortcuts/src/EditShortcut.tsx +++ b/plugins/shortcuts/src/EditShortcut.tsx @@ -60,6 +60,7 @@ export const EditShortcut = ({ const alertApi = useApi(alertApiRef); const open = Boolean(anchorEl); const analytics = useAnalytics(); + const shortcutData = api.get(); const handleSave: SubmitHandler = async ({ url, title }) => { if (!api.get().some(shortcutTitle => shortcutTitle.title === title)) { @@ -72,11 +73,8 @@ export const EditShortcut = ({ }; try { - if (api.get().some(shortcutTitle => shortcutTitle.title === title)) { - alertApi.post({ - message: `Shortcut title already exist`, - severity: 'error', - }); + if (shortcutData.some(shortcutTitle => shortcutTitle.title === title)) { + throw new Error(`Shortcut Title '${title}' already Exist`); } else { await api.update(newShortcut); alertApi.post({