From 533f647c71695d0b5b464d929de5769734d6bef4 Mon Sep 17 00:00:00 2001 From: Bart Louwers Date: Sat, 5 Jul 2025 11:32:32 +0200 Subject: [PATCH] Add duplicate layer id check (#1262) ## Summary Screenshot 2025-07-05 at 00 29 02 - show an error if a layer with an existing id is added - keep Add Layer modal open until the id is unique ## Testing - `npm run lint` - `npm run build` ------ https://chatgpt.com/codex/tasks/task_e_6868498a46188331b16e7b6e120930a7 --- CHANGELOG.md | 1 + cypress/e2e/modals.cy.ts | 16 ++++++++++++++++ src/components/ModalAdd.tsx | 32 ++++++++++++++++++++++++++++---- src/locales/de/translation.json | 1 + src/locales/fr/translation.json | 1 + src/locales/he/translation.json | 1 + src/locales/it/translation.json | 1 + src/locales/ja/translation.json | 1 + src/locales/zh/translation.json | 1 + 9 files changed, 51 insertions(+), 4 deletions(-) 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 }) }} />