diff --git a/assets/controllers/map_controller.js b/assets/controllers/map_controller.js index 90a9c3f..294caf1 100644 --- a/assets/controllers/map_controller.js +++ b/assets/controllers/map_controller.js @@ -17,9 +17,22 @@ export default class extends Controller { maxZoom: 19, }).addTo(this.map); + this.pendingLatlng = null; + + this.dialog = document.getElementById('add-subject-dialog'); + this.nameInput = document.getElementById('subject-name'); + this.submitBtn = document.getElementById('subject-submit'); + this.cancelBtn = document.getElementById('subject-cancel'); + this.loadSubjects(); - this.map.on('click', (e) => this.createSubject(e.latlng)); + this.map.on('click', (e) => this.openModal(e.latlng)); + + this.submitBtn.addEventListener('click', () => this.handleSubmit()); + this.cancelBtn.addEventListener('click', () => this.closeModal()); + + // Esc key / backdrop close — clear pending state + this.dialog.addEventListener('close', () => this.resetModal()); } loadSubjects() { @@ -34,9 +47,27 @@ export default class extends Controller { }); } - createSubject(latlng) { - const name = prompt('Name of this place?'); - if (!name) return; + openModal(latlng) { + this.pendingLatlng = latlng; + this.nameInput.value = ''; + this.dialog.showModal(); + this.nameInput.focus(); + } + + closeModal() { + this.dialog.close(); + } + + resetModal() { + this.pendingLatlng = null; + this.nameInput.value = ''; + } + + handleSubmit() { + const name = this.nameInput.value.trim(); + if (!name || !this.pendingLatlng) return; + + const latlng = this.pendingLatlng; fetch('/api/subjects', { method: 'POST', @@ -48,7 +79,10 @@ export default class extends Controller { }), }) .then(r => r.json()) - .then(() => this.addMarker(latlng.lat, latlng.lng, name)); + .then(() => { + this.addMarker(latlng.lat, latlng.lng, name); + this.closeModal(); + }); } addMarker(lat, lng, name) { diff --git a/templates/index/page.html.twig b/templates/index/page.html.twig index 63f526c..91e5b4d 100644 --- a/templates/index/page.html.twig +++ b/templates/index/page.html.twig @@ -1,5 +1,29 @@ {% extends 'base.html.twig' %} {% block body %} -
+
+
+ + + + {% endblock %}