From c144c9d958b9c375e3c089cc33a5210b617c1950 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 14 Jun 2024 22:05:56 +0200 Subject: [PATCH] fix: django cannot run collectstatic with circular imports in modules alert.js was importing request.js which then imports alert.js In the process, we moved the post to send the email out of the alert.js file, and thus tried another pattern to communicate data between an alert and its caller. --- .../static/umap/js/components/alerts/alert.js | 35 +++++++++++-------- umap/static/umap/js/umap.js | 20 +++++++---- umap/static/umap/js/umap.layer.js | 8 ++--- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/umap/static/umap/js/components/alerts/alert.js b/umap/static/umap/js/components/alerts/alert.js index 0d0c422e..fb37ed6d 100644 --- a/umap/static/umap/js/components/alerts/alert.js +++ b/umap/static/umap/js/components/alerts/alert.js @@ -1,5 +1,4 @@ import { translate } from '../../modules/i18n.js' -import { ServerRequest } from '../../modules/request.js' import { uMapElement } from '../base.js' class uMapAlert extends uMapElement { @@ -77,9 +76,14 @@ class uMapAlertCreation extends uMapAlert { // biome-ignore lint/style/useNumberNamespace: Number.Infinity returns undefined by default duration = Infinity, editLink = undefined, - sendLink = undefined + sendCallback = undefined ) { - uMapAlertCreation.emit('alertCreation', { message, duration, editLink, sendLink }) + uMapAlertCreation.emit('alertCreation', { + message, + duration, + editLink, + sendCallback, + }) } constructor() { @@ -94,7 +98,7 @@ class uMapAlertCreation extends uMapAlert { duration = 5000, message = '', editLink = undefined, - sendLink = undefined, + sendCallback = undefined, } = event.detail uMapAlert.prototype.onAlert.call(this, { detail: { level, duration, message } }) this.linkWrapper.querySelector('input[type="url"]').value = editLink @@ -104,15 +108,14 @@ class uMapAlertCreation extends uMapAlert { L.Util.copyToClipboard(editLink) event.target.value = translate('✅ Copied!') }) - if (sendLink) { + if (sendCallback) { this.formWrapper.removeAttribute('hidden') const form = this.formWrapper.querySelector('form') form.addEventListener('submit', async (event) => { event.preventDefault() const formData = new FormData(form) - const server = new ServerRequest() + sendCallback(formData) this.removeAttribute('open') - await server.post(sendLink, {}, formData) }) } } @@ -124,12 +127,8 @@ class uMapAlertCreation extends uMapAlert { } class uMapAlertConflict extends uMapAlert { - static error( - message, - // biome-ignore lint/style/useNumberNamespace: Number.Infinity returns undefined by default - duration = Infinity - ) { - uMapAlertConflict.emit('alertConflict', { level: 'error', message, duration }) + static error(message, forceCallback) { + uMapAlertConflict.emit('alertConflict', { level: 'error', message, forceCallback }) } constructor() { @@ -138,14 +137,20 @@ class uMapAlertConflict extends uMapAlert { } onAlertConflict(event) { - const { level = 'info', duration = 5000, message = '' } = event.detail + // biome-ignore lint/style/useNumberNamespace: Number.Infinity returns undefined by default + const { + level = 'info', + duration = Infinity, + message = '', + forceCallback = undefined, + } = event.detail uMapAlert.prototype.onAlert.call(this, { detail: { level, duration, message } }) const form = this.conflictWrapper.querySelector('form') form.addEventListener('submit', (event) => { event.preventDefault() switch (event.submitter.id) { case 'your-changes': - uMapAlertConflict.emit('alertConflictOverride') + forceCallback() break case 'their-changes': window.location.reload() diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index d37fb559..f91f2461 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -13,7 +13,7 @@ L.Map.mergeOptions({ // we cannot rely on this because of the y is overriden by Leaflet // See https://github.com/Leaflet/Leaflet/pull/9201 // And let's remove this -y when this PR is merged and released. - demoTileInfos: { s: 'a', z: 9, x: 265, y: 181, '-y': 181, r: '' }, + demoTileInfos: { 's': 'a', 'z': 9, 'x': 265, 'y': 181, '-y': 181, 'r': '' }, licences: [], licence: '', enableMarkerDraw: true, @@ -1063,17 +1063,14 @@ U.Map = L.Map.extend({ this.permissions.setOptions(data.permissions) this.permissions.commit() if (data?.permissions?.anonymous_edit_url) { - const send_edit_link_url = - this.options.urls.map_send_edit_link && - this.urls.get('map_send_edit_link', { - map_id: this.options.umap_id, - }) this.once('saved', () => { U.AlertCreation.info( L._('Your map has been created with an anonymous account!'), Number.Infinity, data.permissions.anonymous_edit_url, - send_edit_link_url + this.options.urls.map_send_edit_link + ? this.sendEditLinkEmail.bind(this) + : null ) }) } else { @@ -1887,4 +1884,13 @@ U.Map = L.Map.extend({ }) return bounds }, + + sendEditLinkEmail: async function (formData) { + const sendLink = + this.options.urls.map_send_edit_link && + this.urls.get('map_send_edit_link', { + map_id: this.options.umap_id, + }) + await this.server.post(sendLink, {}, formData) + }, }) diff --git a/umap/static/umap/js/umap.layer.js b/umap/static/umap/js/umap.layer.js index 5090ee1c..024d0843 100644 --- a/umap/static/umap/js/umap.layer.js +++ b/umap/static/umap/js/umap.layer.js @@ -1708,11 +1708,11 @@ U.DataLayer = L.Evented.extend({ L._( 'Whoops! Other contributor(s) changed some of the same map elements as you. ' + 'This situation is tricky, you have to choose carefully which version is pertinent.' - ) + ), + async () => { + await this._trySave(url, {}, formData) + } ) - document.addEventListener('umap:alertConflictOverride', async (event) => { - await this._trySave(url, {}, formData) - }) } } else { // Response contains geojson only if save has conflicted and conflicts have