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 1 commit
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
10 changes: 5 additions & 5 deletions public/components/custom_panels/custom_panel_table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ export const CustomPanelTable = ({
};

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

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

const onClone = async (newName: string) => {
if (!isNameValid(newName)) {
if (await doesNameExist(newName)) {
setToast(`Observability Dashboard with name "${newName}" already exists`, 'danger');
} else if (!isNameValid(newName)) {
setToast('Invalid Operational Panel name', 'danger');
} else {
let sourcePanel = selectedCustomPanels[0];
Expand Down
3 changes: 1 addition & 2 deletions public/components/custom_panels/custom_panel_view_so.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ export const CustomPanelViewSO = (props: CustomPanelViewProps) => {
};

const onRename = async (newCustomPanelName: string) => {
const nameFlag = await doesNameExist(newCustomPanelName);
if (await nameFlag()) {
if (await doesNameExist(newCustomPanelName)) {
setToast(`Observability Dashboard with name "${newCustomPanelName}" already exists`, 'danger');
} else if (!isNameValid(newCustomPanelName)) {
setToast('Invalid Dashboard name', 'danger');
Expand Down
21 changes: 9 additions & 12 deletions public/components/custom_panels/redux/panel_slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const normalizedPanel = (panel: CustomPanelType): CustomPanelType => ({

export const selectPanelList = (rootState): CustomPanelType[] => rootState.customPanel.panelList;

const {setToast} = useToast();
const { setToast } = useToast();

/*
** ASYNC DISPATCH FUNCTIONS
Expand Down Expand Up @@ -128,7 +128,7 @@ export const fetchPanel = (id) => async (dispatch, getState) => {
dispatch(setPanel(panel));
};

export const fetchVisualization = () => (dispatch, getState) => {};
export const fetchVisualization = () => (dispatch, getState) => { };

const updateLegacyPanel = (panel: CustomPanelType) =>
coreRefs.http!.post(`${CUSTOM_PANELS_API_PREFIX}/panels/update`, {
Expand All @@ -141,12 +141,10 @@ 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() => {
export const doesNameExist = async (newCustomPanelName: string) => {
const panels = await fetchCustomPanels();
if((panels.some((i: { title: string; }) => i.title === newCustomPanelName))){
return true;
}
return false;
console.log(panels);
return (panels.some(({ title }: {title: string}) => title === newCustomPanelName));
}
export const updatePanel = (panel: CustomPanelType, successMsg: string, failureMsg: string) => async (dispatch, getState) => {
try {
Expand All @@ -158,7 +156,7 @@ export const updatePanel = (panel: CustomPanelType, successMsg: string, failureM
dispatch(setPanel(panel));
const panelList = getState().customPanel.panelList.map((p) => (p.id === panel.id ? panel : p));
dispatch(setPanelList(panelList));
} catch (e) {
} catch (e) {
if (failureMsg) {
setToast(failureMsg, 'danger')
}
Expand Down Expand Up @@ -200,7 +198,7 @@ export const replaceVizInPanel = (oldPanel, oldVizId, vizId, newVisualizationTit
const visualizationsWithNewPanel = addVisualizationPanel(vizId, oldVizId, allVisualizations);

const updatedPanel = { ...panel, visualizations: visualizationsWithNewPanel };

dispatch(updatePanel(updatedPanel, `Visualization ${newVisualizationTitle} successfully added!`, `Error in adding ${newVisualizationTitle} visualization to the panel`));
};

Expand All @@ -218,9 +216,8 @@ const deleteLegacyPanels = (customPanelIdList: string[]) => {
};

export const deletePanels = (panelsToDelete: CustomPanelType[]) => async (dispatch, getState) => {
const toastMessage = `Observability Dashboard${
panelsToDelete.length > 1 ? 's' : ' ' + panelsToDelete[0].title
} successfully deleted!`;
const toastMessage = `Observability Dashboard${panelsToDelete.length > 1 ? 's' : ' ' + panelsToDelete[0].title
} successfully deleted!`;
try {
const ids = panelsToDelete.map((p) => p.id);
await Promise.all([deleteLegacyPanels(ids), deletePanelSO(ids)]);
Expand Down