4 - Replace prompt() with DaisyUI modal for adding subjects
This commit is contained in:
parent
c154404c6f
commit
02fae2737d
2 changed files with 64 additions and 6 deletions
|
|
@ -17,9 +17,22 @@ export default class extends Controller {
|
||||||
maxZoom: 19,
|
maxZoom: 19,
|
||||||
}).addTo(this.map);
|
}).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.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() {
|
loadSubjects() {
|
||||||
|
|
@ -34,9 +47,27 @@ export default class extends Controller {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
createSubject(latlng) {
|
openModal(latlng) {
|
||||||
const name = prompt('Name of this place?');
|
this.pendingLatlng = latlng;
|
||||||
if (!name) return;
|
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', {
|
fetch('/api/subjects', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|
@ -48,7 +79,10 @@ export default class extends Controller {
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
.then(r => r.json())
|
.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) {
|
addMarker(lat, lng, name) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,29 @@
|
||||||
{% extends 'base.html.twig' %}
|
{% extends 'base.html.twig' %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<div {{ stimulus_controller('map') }} class="map-container"></div>
|
<div {{ stimulus_controller('map') }} class="map-container"
|
||||||
|
data-map-dialog-outlet="#add-subject-dialog">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<dialog id="add-subject-dialog" class="modal">
|
||||||
|
<div class="modal-box">
|
||||||
|
<h3 class="text-lg font-bold mb-4">Add a place</h3>
|
||||||
|
|
||||||
|
<fieldset class="fieldset">
|
||||||
|
<legend class="fieldset-legend">Name</legend>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="subject-name"
|
||||||
|
class="input w-full"
|
||||||
|
placeholder="e.g. Frituur De Leeuw"
|
||||||
|
autocomplete="off"
|
||||||
|
>
|
||||||
|
</fieldset>
|
||||||
|
|
||||||
|
<div class="modal-action">
|
||||||
|
<button id="subject-cancel" class="btn">Cancel</button>
|
||||||
|
<button id="subject-submit" class="btn btn-primary">Add</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</dialog>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue