diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index 4d2b3707..4a0d797b 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -401,7 +401,7 @@ U.Map = L.Map.extend({ this._controls.search = new U.SearchControl() this._controls.embed = new L.Control.Embed(this, this.options.embedOptions) this._controls.tilelayersChooser = new U.TileLayerChooser(this) - this._controls.star = new U.StarControl(this) + if (this.options.user) this._controls.star = new U.StarControl(this) this._controls.editinosm = new L.Control.EditInOSM({ position: 'topleft', widgetOptions: { @@ -468,6 +468,7 @@ U.Map = L.Map.extend({ status = this.options[`${name}Control`] if (status === false) continue control = this._controls[name] + if (!control) continue control.addTo(this) if (status === undefined || status === null) L.DomUtil.addClass(control._container, 'display-on-more') @@ -1142,24 +1143,22 @@ U.Map = L.Map.extend({ }) }, - star: function () { + star: async function () { if (!this.options.umap_id) return this.ui.alert({ content: L._('Please save the map first'), level: 'error', }) const url = this.urls.get('map_star', { map_id: this.options.umap_id }) - this.post(url, { - context: this, - callback: function (data) { - this.options.starred = data.starred - let msg = data.starred - ? L._('Map has been starred') - : L._('Map has been unstarred') - this.ui.alert({ content: msg, level: 'info' }) - this.renderControls() - }, - }) + const [data, response, error] = await this.server.post(url) + if (!error) { + this.options.starred = data.starred + let msg = data.starred + ? L._('Map has been starred') + : L._('Map has been unstarred') + this.ui.alert({ content: msg, level: 'info' }) + this.renderControls() + } }, geometry: function () { diff --git a/umap/tests/integration/test_star.py b/umap/tests/integration/test_star.py new file mode 100644 index 00000000..3984fb91 --- /dev/null +++ b/umap/tests/integration/test_star.py @@ -0,0 +1,27 @@ +import re + +import pytest +from playwright.sync_api import expect + +from umap.models import Star + +pytestmark = pytest.mark.django_db + + +def test_star_control_is_visible_if_logged_in(map, live_server, page, login, user): + login(user) + assert not Star.objects.count() + page.goto(f"{live_server.url}{map.get_absolute_url()}") + page.get_by_title("More controls").click() + control = page.locator(".leaflet-control-star") + expect(control).to_be_visible() + with page.expect_response(re.compile(".*/star/")): + control.click() + assert Star.objects.count() == 1 + + +def test_no_star_control_if_not_logged_in(map, live_server, page): + page.goto(f"{live_server.url}{map.get_absolute_url()}") + page.get_by_title("More controls").click() + control = page.locator(".leaflet-control-star") + expect(control).to_be_hidden()