diff --git a/CHANGELOG.md b/CHANGELOG.md index 567bc7de..3edcd96a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ ### 🐞 Bug fixes - Fix incorrect handing of network error response (#944) +- Show an error when adding a layer with a duplicate ID - _...Add new stuff here..._ ## 2.1.1 diff --git a/cypress/e2e/modals.cy.ts b/cypress/e2e/modals.cy.ts index 71420b78..c87ea1ea 100644 --- a/cypress/e2e/modals.cy.ts +++ b/cypress/e2e/modals.cy.ts @@ -272,6 +272,22 @@ describe("modals", () => { }); + describe("add layer", () => { + beforeEach(() => { + when.setStyle("layer"); + when.modal.open(); + }); + + it("shows duplicate id error", () => { + when.setValue("add-layer.layer-id.input", "background"); + when.click("add-layer"); + then(get.elementByTestId("modal:add-layer")).shouldExist(); + then(get.element(".maputnik-modal-error")).shouldContainText( + "Layer ID already exists" + ); + }); + }); + describe("sources", () => { it("toggle"); }); diff --git a/src/components/ModalAdd.tsx b/src/components/ModalAdd.tsx index e503c02e..edbdfe04 100644 --- a/src/components/ModalAdd.tsx +++ b/src/components/ModalAdd.tsx @@ -23,10 +23,16 @@ type ModalAddState = { id: string source?: string 'source-layer'?: string + error?: string | null }; class ModalAddInternal extends React.Component { addLayer = () => { + if (this.props.layers.some(l => l.id === this.state.id)) { + this.setState({ error: this.props.t('Layer ID already exists') }) + return + } + const changedLayers = this.props.layers.slice(0) const layer: ModalAddState = { id: this.state.id, @@ -41,9 +47,10 @@ class ModalAddInternal extends React.Component { + this.props.onLayersChange(changedLayers) + this.props.onOpenToggle(false) + }) } constructor(props: ModalAddInternalProps) { @@ -51,6 +58,7 @@ class ModalAddInternal extends React.Component 0) { @@ -129,6 +137,21 @@ class ModalAddInternal extends React.Component + {this.state.error} + this.setState({ error: null })} + className="maputnik-modal-error-close" + > + × + + + ); + } return + {errorElement}
{ - this.setState({ id: v }) + this.setState({ id: v, error: null }) }} />