Skip to content

Commit

Permalink
Handle deletes (elastic#114545)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonStoltz authored and kibanamachine committed Oct 12, 2021
1 parent 0f15cce commit 2775668
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,42 @@ describe('CurationSuggestionLogic', () => {
);
});

describe('when a suggestion is a "delete" suggestion', () => {
const deleteSuggestion = {
...suggestion,
operation: 'delete',
promoted: [],
curation_id: 'cur-6155e69c7a2f2e4f756303fd',
};

it('will show a confirm message before applying, and redirect a user back to the curations page, rather than the curation details page', async () => {
jest.spyOn(global, 'confirm').mockReturnValueOnce(true);
http.put.mockReturnValueOnce(
Promise.resolve({
results: [{ ...suggestion, status: 'accepted', curation_id: undefined }],
})
);
mountLogic({
suggestion: deleteSuggestion,
});
CurationSuggestionLogic.actions.acceptSuggestion();
await nextTick();

expect(navigateToUrl).toHaveBeenCalledWith('/engines/some-engine/curations');
});

it('will do nothing if the user does not confirm', async () => {
jest.spyOn(global, 'confirm').mockReturnValueOnce(false);
mountLogic({
suggestion: deleteSuggestion,
});
CurationSuggestionLogic.actions.acceptSuggestion();
await nextTick();
expect(http.put).not.toHaveBeenCalled();
expect(navigateToUrl).not.toHaveBeenCalled();
});
});

itHandlesErrors(http.put, () => {
CurationSuggestionLogic.actions.acceptSuggestion();
});
Expand Down Expand Up @@ -404,6 +440,42 @@ describe('CurationSuggestionLogic', () => {
);
});

describe('when a suggestion is a "delete" suggestion', () => {
const deleteSuggestion = {
...suggestion,
operation: 'delete',
promoted: [],
curation_id: 'cur-6155e69c7a2f2e4f756303fd',
};

it('will show a confirm message before applying, and redirect a user back to the curations page, rather than the curation details page', async () => {
jest.spyOn(global, 'confirm').mockReturnValueOnce(true);
http.put.mockReturnValueOnce(
Promise.resolve({
results: [{ ...suggestion, status: 'accepted', curation_id: undefined }],
})
);
mountLogic({
suggestion: deleteSuggestion,
});
CurationSuggestionLogic.actions.acceptAndAutomateSuggestion();
await nextTick();

expect(navigateToUrl).toHaveBeenCalledWith('/engines/some-engine/curations');
});

it('will do nothing if the user does not confirm', async () => {
jest.spyOn(global, 'confirm').mockReturnValueOnce(false);
mountLogic({
suggestion: deleteSuggestion,
});
CurationSuggestionLogic.actions.acceptAndAutomateSuggestion();
await nextTick();
expect(http.put).not.toHaveBeenCalled();
expect(navigateToUrl).not.toHaveBeenCalled();
});
});

itHandlesErrors(http.put, () => {
CurationSuggestionLogic.actions.acceptAndAutomateSuggestion();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ export const CurationSuggestionLogic = kea<
const { engineName } = EngineLogic.values;
const { suggestion } = values;

if (suggestion!.operation === 'delete') {
const confirmed = await confirmDialog('Are you sure you want to delete this curation?');
if (!confirmed) return;
}

try {
const updatedSuggestion = await updateSuggestion(
http,
Expand All @@ -155,11 +160,16 @@ export const CurationSuggestionLogic = kea<
{ defaultMessage: 'Suggestion was succefully applied.' }
)
);
KibanaLogic.values.navigateToUrl(
generateEnginePath(ENGINE_CURATION_PATH, {
curationId: updatedSuggestion.curation_id,
})
);
if (suggestion!.operation === 'delete') {
// Because if a curation is deleted, there will be no curation detail page to navigate to afterwards.
KibanaLogic.values.navigateToUrl(generateEnginePath(ENGINE_CURATIONS_PATH));
} else {
KibanaLogic.values.navigateToUrl(
generateEnginePath(ENGINE_CURATION_PATH, {
curationId: updatedSuggestion.curation_id,
})
);
}
} catch (e) {
flashAPIErrors(e);
}
Expand All @@ -169,6 +179,11 @@ export const CurationSuggestionLogic = kea<
const { engineName } = EngineLogic.values;
const { suggestion } = values;

if (suggestion!.operation === 'delete') {
const confirmed = await confirmDialog('Are you sure you want to delete this curation?');
if (!confirmed) return;
}

try {
const updatedSuggestion = await updateSuggestion(
http,
Expand All @@ -187,11 +202,16 @@ export const CurationSuggestionLogic = kea<
}
)
);
KibanaLogic.values.navigateToUrl(
generateEnginePath(ENGINE_CURATION_PATH, {
curationId: updatedSuggestion.curation_id,
})
);
if (suggestion!.operation === 'delete') {
// Because if a curation is deleted, there will be no curation detail page to navigate to afterwards.
KibanaLogic.values.navigateToUrl(generateEnginePath(ENGINE_CURATIONS_PATH));
} else {
KibanaLogic.values.navigateToUrl(
generateEnginePath(ENGINE_CURATION_PATH, {
curationId: updatedSuggestion.curation_id,
})
);
}
} catch (e) {
flashAPIErrors(e);
}
Expand Down Expand Up @@ -325,3 +345,10 @@ const getCuration = async (http: HttpSetup, engineName: string, curationId: stri
query: { skip_record_analytics: 'true' },
});
};

const confirmDialog = (msg: string) => {
return new Promise(function (resolve) {
const confirmed = window.confirm(msg);
return resolve(confirmed);
});
};

0 comments on commit 2775668

Please sign in to comment.