Skip to content

Commit

Permalink
Fix tab switching (#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschumpr authored Feb 1, 2024
2 parents 1a3f657 + 1fc4b2f commit 04ef71b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/api/Controllers/CompletionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<IEnumerable<Completion>> GetAsync([FromQuery] int? boreholeId

if (boreholeId != null)
{
completions = completions.Where(c => c.BoreholeId == boreholeId);
completions = completions.Where(c => c.BoreholeId == boreholeId).OrderBy(c => c.Created);
}

return await completions.ToListAsync().ConfigureAwait(false);
Expand Down
92 changes: 86 additions & 6 deletions src/client/cypress/e2e/editor/completion.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ describe("completion crud tests", () => {
expect(location.hash).to.eq("#backfill");
});

// switch tabs while editing: no prompt should be displayed when no changes have been made
// switch tabs
// existing editing to other existing: no prompt should be displayed when no changes have been made
startEditing();
setTab(0);
cy.get('[data-cy="prompt"]').should("not.exist");
Expand All @@ -224,7 +225,7 @@ describe("completion crud tests", () => {
expect(location.hash).to.eq("#casing");
});

// switch tabs while editing: tab switching can be canceled in prompt
// existing editing to other existing: tab switching can be canceled in prompt
startEditing();
setInput("name", "Compl-1 updated");
setTab(1);
Expand All @@ -238,7 +239,7 @@ describe("completion crud tests", () => {
});
evaluateInput("name", "Compl-1 updated");

// switch tabs while editing: changes can be reverted in prompt
// existing editing to other existing: changes can be reverted in prompt
setTab(1);
handlePrompt("Unsaved changes", "reset");
isTabSelected(1);
Expand All @@ -250,7 +251,7 @@ describe("completion crud tests", () => {
expect(location.hash).to.eq("#casing");
});

// switch tabs while editing: changes can be saved in prompt
// existing editing to other existing: changes can be saved in prompt
startEditing();
setInput("name", "Compl-2 updated");
setTab(0);
Expand All @@ -259,7 +260,7 @@ describe("completion crud tests", () => {
isTabSelected(0);
cy.contains("Compl-2 updated");

// switch tabs while adding new completion: no prompt should be displayed when no changes have been made
// new to existing: no prompt should be displayed when no changes have been made
addCompletion();
cy.location().should(location => {
expect(location.pathname).to.eq(`/editor/${boreholeId}/completion/new`);
Expand All @@ -275,7 +276,50 @@ describe("completion crud tests", () => {
expect(location.hash).to.eq("#casing");
});

// add new completion while editing: form should be reset
// new to existing: changes can be reverted in prompt
addCompletion();
cy.location().should(location => {
expect(location.pathname).to.eq(`/editor/${boreholeId}/completion/new`);
expect(location.hash).to.eq("");
});
setInput("name", "new completion");
setSelect("kindId", 1);
setTab(0);
handlePrompt("Unsaved changes", "reset");
cy.wait("@casing_GET");
cy.location().should(location => {
expect(location.pathname).to.eq(
`/editor/${boreholeId}/completion/${completion1Id}`,
);
expect(location.hash).to.eq("#casing");
});
cy.contains("new completion").should("not.exist");

// new to existing: changes can be saved in prompt
addCompletion();
cy.location().should(location => {
expect(location.pathname).to.eq(`/editor/${boreholeId}/completion/new`);
expect(location.hash).to.eq("");
});
setInput("name", "new completion");
setSelect("kindId", 1);
setTab(0);
handlePrompt("Unsaved changes", "save");
cy.wait("@casing_GET");
cy.location().should(location => {
expect(location.pathname).to.eq(
`/editor/${boreholeId}/completion/${completion1Id}`,
);
expect(location.hash).to.eq("#casing");
});
cy.contains("new completion").should("exist");

setTab(2);
deleteCompletion();
handlePrompt("Do you really want to delete this completion?", "delete");

// existing editing to new: no prompt should be displayed when no changes have been made, form should be reset
setTab(0);
startEditing();
cy.location().should(location => {
expect(location.pathname).to.eq(
Expand All @@ -294,6 +338,42 @@ describe("completion crud tests", () => {
expect(location.hash).to.eq("");
});

// existing editing to new: changes can be reverted in prompt
setTab(0);
cy.location().should(location => {
expect(location.pathname).to.eq(
`/editor/${boreholeId}/completion/${completion1Id}`,
);
expect(location.hash).to.eq("#casing");
});
startEditing();
setInput("name", "Reset compl-1");
addCompletion();
handlePrompt("Unsaved changes", "reset");
cy.location().should(location => {
expect(location.pathname).to.eq(`/editor/${boreholeId}/completion/new`);
expect(location.hash).to.eq("");
});
cy.contains("Reset compl-1").should("not.exist");

// existing editing to new: changes can be saved in prompt
setTab(0);
cy.location().should(location => {
expect(location.pathname).to.eq(
`/editor/${boreholeId}/completion/${completion1Id}`,
);
expect(location.hash).to.eq("#casing");
});
startEditing();
setInput("name", "Reset compl-1");
addCompletion();
handlePrompt("Unsaved changes", "save");
cy.location().should(location => {
expect(location.pathname).to.eq(`/editor/${boreholeId}/completion/new`);
expect(location.hash).to.eq("");
});
cy.contains("Reset compl-1").should("exist");

// cancel adding new completion: last tab should be selected
cancelEditing();
isTabSelected(1);
Expand Down
14 changes: 12 additions & 2 deletions src/client/src/commons/form/borehole/completion/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ const Completion = props => {
const saveCompletion = completion => {
if (completion.id === 0) {
addCompletion(completion).then(() => {
setState({ ...state, switchTabTo: state.displayed.length - 1 });
setState({
...state,
switchTabTo:
state.switchTabTo === null
? state.displayed.length - 1
: state.switchTabTo,
});
loadData();
});
} else {
Expand Down Expand Up @@ -214,7 +220,11 @@ const Completion = props => {
switchTabTo: null,
trySwitchTab: false,
});
updateHistory(completions[index].id);
if (index === -1) {
updateHistory("new");
} else {
updateHistory(completions[index].id);
}
} else {
resetState();
history.push(
Expand Down

0 comments on commit 04ef71b

Please sign in to comment.