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

[App Search] Handle curation suggestions with a delete operation #114545

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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);
});
};
Comment on lines +349 to +354
Copy link
Contributor

Choose a reason for hiding this comment

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

At some point we'll want to stop writing new window.confirms and implement EuiConfirmModals but that doesn't need to be now

Copy link
Member Author

Choose a reason for hiding this comment

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

True.