Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Error handling on duplicate dashboard names #441

Merged
merged 6 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions public/components/custom_panels/custom_panel_table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { DeleteModal } from '../common/helpers/delete_modal';
import {
createPanel,
deletePanels,
doesNameExist,
fetchPanels,
isUuid,
newPanelTemplate,
Expand Down Expand Up @@ -120,7 +121,10 @@ export const CustomPanelTable = ({
};

const onCreate = async (newCustomPanelName: string) => {
if (!isNameValid(newCustomPanelName)) {
const nameFlag = await doesNameExist(newCustomPanelName);
if (await nameFlag()) {
setToast(`Observability Dashboard with name "${newCustomPanelName}" already exists`, 'danger');
} else if (!isNameValid(newCustomPanelName)) {
setToast('Invalid Dashboard name', 'danger');
} else {
const newPanel = newPanelTemplate(newCustomPanelName);
Expand All @@ -130,7 +134,10 @@ export const CustomPanelTable = ({
};

const onRename = async (newCustomPanelName: string) => {
if (!isNameValid(newCustomPanelName)) {
const nameFlag = await doesNameExist(newCustomPanelName);
if (await nameFlag()) {
setToast(`Observability Dashboard with name "${newCustomPanelName}" already exists`, 'danger');
} else if (!isNameValid(newCustomPanelName)) {
setToast('Invalid Dashboard name', 'danger');
} else {
dispatch(renameCustomPanel(newCustomPanelName, selectedCustomPanels[0].id));
Expand Down
6 changes: 5 additions & 1 deletion public/components/custom_panels/custom_panel_view_so.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { VisaulizationFlyoutSO } from './panel_modules/visualization_flyout/visu
import {
clonePanel,
deletePanels,
doesNameExist,
fetchPanel,
renameCustomPanel,
selectPanel,
Expand Down Expand Up @@ -208,7 +209,10 @@ export const CustomPanelViewSO = (props: CustomPanelViewProps) => {
};

const onRename = async (newCustomPanelName: string) => {
if (!isNameValid(newCustomPanelName)) {
const nameFlag = await doesNameExist(newCustomPanelName);
if (await nameFlag()) {
setToast(`Observability Dashboard with name "${newCustomPanelName}" already exists`, 'danger');
} else if (!isNameValid(newCustomPanelName)) {
setToast('Invalid Dashboard name', 'danger');
} else {
dispatch(renameCustomPanel(newCustomPanelName, panel.id));
Expand Down
7 changes: 7 additions & 0 deletions public/components/custom_panels/redux/panel_slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ export const uuidRx = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a

export const isUuid = (id) => !!id.match(uuidRx);

export const doesNameExist = (newCustomPanelName: string) => async() => {
const panels = await fetchCustomPanels();
if((panels.some((i: { title: string; }) => i.title === newCustomPanelName))){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. didn't run lint? missing space after if. Also probably reutrn panels.some((i: { title: string; }) => i.title === newCustomPanelName)?

and what happens if fetch call failed?

return true;
}
return false;
}
export const updatePanel = (panel: CustomPanelType, successMsg: string, failureMsg: string) => async (dispatch, getState) => {
try {
if (isUuid(panel.id)) await updateSavedObjectPanel(panel);
Expand Down