Skip to content

Commit

Permalink
[8.x] feat: SavedObjectSaveModal - add postfix if the title is duplic…
Browse files Browse the repository at this point in the history
…ated (#198777) (#199127)

# Backport

This will backport the following commits from `main` to `8.x`:
- [feat: SavedObjectSaveModal - add postfix if the title is duplicated
(#198777)](#198777)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Paulina
Shakirova","email":"paulina.shakirova@elastic.co"},"sourceCommit":{"committedDate":"2024-11-06T12:50:19Z","message":"feat:
SavedObjectSaveModal - add postfix if the title is duplicated
(#198777)\n\n## Summary\r\n\r\nThis PR fixes the issue with the
[[Dashboard][Event annotations][Lens]\r\nappending a postfix when there
is a duplicate title instead of\r\ninterrupting the
save\r\nflow](https://github.com/elastic/kibana/issues/161119).\r\n\r\nCurrently,
if a user wants to save an object as a new group without\r\nmodifying
the title, they will see a warning about it.\r\n\r\nThis PR introduces
an additional step. If the user attempts to save an\r\nobject as a new
group, we automatically append a postfix to the title.\r\nIf the user
manually changes the title back to the original and tries to\r\nsave it,
they will see the currently configured
warning.\r\n\r\n\r\nhttps://github.com/user-attachments/assets/4f764b75-5b83-4277-8e5e-42fe1174fae7","sha":"b8dbf835088295246f457d27daa44d2546e17565","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","usability","v9.0.0","Team:SharedUX","backport:prev-minor","papercut"],"title":"feat:
SavedObjectSaveModal - add postfix if the title is
duplicated","number":198777,"url":"https://github.com/elastic/kibana/pull/198777","mergeCommit":{"message":"feat:
SavedObjectSaveModal - add postfix if the title is duplicated
(#198777)\n\n## Summary\r\n\r\nThis PR fixes the issue with the
[[Dashboard][Event annotations][Lens]\r\nappending a postfix when there
is a duplicate title instead of\r\ninterrupting the
save\r\nflow](https://github.com/elastic/kibana/issues/161119).\r\n\r\nCurrently,
if a user wants to save an object as a new group without\r\nmodifying
the title, they will see a warning about it.\r\n\r\nThis PR introduces
an additional step. If the user attempts to save an\r\nobject as a new
group, we automatically append a postfix to the title.\r\nIf the user
manually changes the title back to the original and tries to\r\nsave it,
they will see the currently configured
warning.\r\n\r\n\r\nhttps://github.com/user-attachments/assets/4f764b75-5b83-4277-8e5e-42fe1174fae7","sha":"b8dbf835088295246f457d27daa44d2546e17565"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/198777","number":198777,"mergeCommit":{"message":"feat:
SavedObjectSaveModal - add postfix if the title is duplicated
(#198777)\n\n## Summary\r\n\r\nThis PR fixes the issue with the
[[Dashboard][Event annotations][Lens]\r\nappending a postfix when there
is a duplicate title instead of\r\ninterrupting the
save\r\nflow](https://github.com/elastic/kibana/issues/161119).\r\n\r\nCurrently,
if a user wants to save an object as a new group without\r\nmodifying
the title, they will see a warning about it.\r\n\r\nThis PR introduces
an additional step. If the user attempts to save an\r\nobject as a new
group, we automatically append a postfix to the title.\r\nIf the user
manually changes the title back to the original and tries to\r\nsave it,
they will see the currently configured
warning.\r\n\r\n\r\nhttps://github.com/user-attachments/assets/4f764b75-5b83-4277-8e5e-42fe1174fae7","sha":"b8dbf835088295246f457d27daa44d2546e17565"}}]}]
BACKPORT-->

Co-authored-by: Paulina Shakirova <paulina.shakirova@elastic.co>
  • Loading branch information
kibanamachine and paulinashakirova authored Nov 6, 2024
1 parent 19e4328 commit 4cf3ee8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,54 @@ describe('SavedObjectSaveModal', () => {
expect(onSave.mock.calls[0][0].newCopyOnSave).toBe(true);
});
});

describe('handle title duplication logic', () => {
it('should append "[1]" to title if no number is present', async () => {
const onSave = jest.fn();

render(
<I18nProvider>
<SavedObjectSaveModal
onSave={onSave}
onClose={() => {}}
title="Saved Object"
objectType="visualization"
showDescription={true}
showCopyOnSave={true}
/>
</I18nProvider>
);

const switchElement = screen.getByTestId('saveAsNewCheckbox');
await userEvent.click(switchElement);

await waitFor(() => {
expect(screen.getByTestId('savedObjectTitle')).toHaveValue('Saved Object [1]');
});
});

it('should increment the number by one when a number is already present', async () => {
const onSave = jest.fn();

render(
<I18nProvider>
<SavedObjectSaveModal
onSave={onSave}
onClose={() => {}}
title="Saved Object [1]"
objectType="visualization"
showDescription={true}
showCopyOnSave={true}
/>
</I18nProvider>
);

const switchElement = screen.getByTestId('saveAsNewCheckbox');
await userEvent.click(switchElement);

await waitFor(() => {
expect(screen.getByTestId('savedObjectTitle')).toHaveValue('Saved Object [2]');
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,28 @@ export class SavedObjectSaveModal extends React.Component<Props, SaveModalState>
});
};

private handleTitleDuplication = () => {
const regex = /\s*\[(\d+)\]$/;
const match = this.state.title.match(regex);

if (match) {
const newNumber = Number(match[1]) + 1;

this.setState({
title: this.state.title.replace(regex, ` [${newNumber}]`),
});
} else {
this.setState({
title: this.state.title + ' [1]',
});
}
};

private onCopyOnSaveChange = (event: EuiSwitchEvent) => {
if (this.props.title === this.state.title && event.target.checked) {
this.handleTitleDuplication();
}

this.setState({
copyOnSave: event.target.checked,
});
Expand Down

0 comments on commit 4cf3ee8

Please sign in to comment.