Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sync): handle sync of datalayer delete #2416

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions umap/static/umap/js/modules/data/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,12 @@ export class DataLayer extends ServerStored {
})
}

_delete() {
this.isDeleted = true
del(sync = true) {
this.erase()
if (sync) {
this.isDeleted = true
this.sync.delete()
}
}

empty() {
Expand Down Expand Up @@ -819,7 +822,7 @@ export class DataLayer extends ServerStored {
<i class="icon icon-24 icon-delete"></i>${translate('Delete')}
</button>`)
deleteButton.addEventListener('click', () => {
this._delete()
this.del()
this._umap.editPanel.close()
})
advancedButtons.appendChild(deleteButton)
Expand Down Expand Up @@ -1147,10 +1150,14 @@ export class DataLayer extends ServerStored {
if (this.createdOnServer) {
await this._umap.server.post(this.getDeleteUrl())
}
delete this._umap.datalayers[stamp(this)]
this.commitDelete()
return true
}

commitDelete() {
delete this._umap.datalayers[stamp(this)]
}

getName() {
return this.options.name || translate('Untitled layer')
}
Expand Down Expand Up @@ -1221,7 +1228,7 @@ export class DataLayer extends ServerStored {
this._umap.dialog
.confirm(translate('Are you sure you want to delete this layer?'))
.then(() => {
this._delete()
this.del()
})
},
this
Expand Down
8 changes: 8 additions & 0 deletions umap/static/umap/js/modules/sync/updaters.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ export class DataLayerUpdater extends BaseUpdater {
}
datalayer.render([key])
}

delete({ metadata }) {
const datalayer = this.getDataLayerFromID(metadata.id)
if (datalayer) {
datalayer.del(false)
datalayer.commitDelete()
}
}
}

export class FeatureUpdater extends BaseUpdater {
Expand Down
5 changes: 4 additions & 1 deletion umap/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ class DataLayerFactory(factory.django.DjangoModelFactory):

@classmethod
def _adjust_kwargs(cls, **kwargs):
print(kwargs, cls.name)
if "data" in kwargs:
data = copy.deepcopy(kwargs.pop("data"))
data.setdefault("_umap_options", {})
if "name" in data["_umap_options"] and kwargs["name"] == cls.name:
kwargs["name"] = data["_umap_options"]["name"]
if "settings" not in kwargs:
kwargs["settings"] = data.get("_umap_options", {})
else:
Expand All @@ -135,7 +139,6 @@ def _adjust_kwargs(cls, **kwargs):
**DataLayerFactory.settings._defaults,
**kwargs["settings"],
}
data.setdefault("_umap_options", {})
kwargs["settings"]["name"] = kwargs["name"]
data["_umap_options"]["name"] = kwargs["name"]
data.setdefault("type", "FeatureCollection")
Expand Down
66 changes: 66 additions & 0 deletions umap/tests/integration/test_websocket_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,72 @@ def test_should_sync_datalayers(new_page, live_server, websocket_server, tilelay
assert DataLayer.objects.count() == 2


@pytest.mark.xdist_group(name="websockets")
def test_should_sync_datalayers_delete(
new_page, live_server, websocket_server, tilelayer
):
map = MapFactory(name="sync", edit_status=Map.ANONYMOUS)
map.settings["properties"]["syncEnabled"] = True
map.save()
data1 = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "Point 1",
},
"geometry": {"type": "Point", "coordinates": [0.065918, 48.385442]},
},
],
"_umap_options": {
"name": "datalayer 1",
},
}
data2 = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "Point 2",
},
"geometry": {"type": "Point", "coordinates": [3.55957, 49.767074]},
},
],
"_umap_options": {
"name": "datalayer 2",
},
}
DataLayerFactory(map=map, data=data1)
DataLayerFactory(map=map, data=data2)

# Create two tabs
peerA = new_page("Page A")
peerA.goto(f"{live_server.url}{map.get_absolute_url()}?edit")
peerB = new_page("Page B")
peerB.goto(f"{live_server.url}{map.get_absolute_url()}?edit")

peerA.get_by_role("button", name="Open browser").click()
expect(peerA.get_by_text("datalayer 1")).to_be_visible()
expect(peerA.get_by_text("datalayer 2")).to_be_visible()
peerB.get_by_role("button", name="Open browser").click()
expect(peerB.get_by_text("datalayer 1")).to_be_visible()
expect(peerB.get_by_text("datalayer 2")).to_be_visible()

# Delete "datalayer 2" in peerA
peerA.locator(".datalayer").get_by_role("button", name="Delete layer").first.click()
peerA.get_by_role("button", name="OK").click()
expect(peerA.get_by_text("datalayer 2")).to_be_hidden()
expect(peerB.get_by_text("datalayer 2")).to_be_hidden()

# Save delete to the server
with peerA.expect_response(re.compile(".*/datalayer/delete/.*")):
peerA.get_by_role("button", name="Save").click()
expect(peerA.get_by_text("datalayer 2")).to_be_hidden()
expect(peerB.get_by_text("datalayer 2")).to_be_hidden()


@pytest.mark.xdist_group(name="websockets")
def test_create_and_sync_map(
new_page, live_server, websocket_server, tilelayer, login, user
Expand Down
Loading