Skip to content

Commit

Permalink
feat: SavedObjectSaveModal - add postfix if the title is duplicated (e…
Browse files Browse the repository at this point in the history
…lastic#198777)

## Summary

This PR fixes the issue with the [[Dashboard][Event annotations][Lens]
appending a postfix when there is a duplicate title instead of
interrupting the save
flow](elastic#161119).

Currently, if a user wants to save an object as a new group without
modifying the title, they will see a warning about it.

This PR introduces an additional step. If the user attempts to save an
object as a new group, we automatically append a postfix to the title.
If the user manually changes the title back to the original and tries to
save it, they will see the currently configured warning.


https://github.com/user-attachments/assets/4f764b75-5b83-4277-8e5e-42fe1174fae7
  • Loading branch information
paulinashakirova authored and mgadewoll committed Nov 7, 2024
1 parent 5c2c5fe commit 925f9d0
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 925f9d0

Please sign in to comment.