Search results handling (WIP)

This commit is contained in:
2026-05-23 23:43:48 +02:00
parent 743bbc2f3b
commit f9daaefbdd
3 changed files with 52 additions and 5 deletions

View File

@@ -2,6 +2,8 @@ import { Controller } from "@hotwired/stimulus";
import { GisState } from "../state.js";
import UI from "../ui.js";
const html = String.raw;
export default class extends Controller {
static values = {
'coords': String,
@@ -9,7 +11,7 @@ export default class extends Controller {
'id': String,
};
static targets = ['search'];
static targets = ['search', 'results'];
/**
*
@@ -25,7 +27,53 @@ export default class extends Controller {
}
const response = await fetch("https://caprigis-api.ddev.site:33001/search?" + new URLSearchParams(body));
const results = await response.json();
console.debug(await response.json(), body);
console.debug(results);
this.#injectResults(results);
this.#filterMap(results);
}
#injectResults(results) {
/**
* @type {HTMLOutputElement} output
*/
const output = this.resultsTarget;
output.innerHTML = '';
for (const result of results) {
const item = html`
<p class="p-1">${result.label}</p>
`;
output.innerHTML += item;
}
}
/**
*
* @param {Array<Object>} results
*/
#filterMap(results) {
const map = GisState.map;
const labels = [];
results.forEach(r => labels.push(r.label));
for (const key of Object.keys(GisState.layers)) {
map.removeLayer(GisState.layers[key]);
}
const sites = GisState.markers.sites;
for (let key of Object.keys(sites)) {
// If map has layers from previous search results...
map.removeLayer(sites[key]);
for (const label of labels) {
if (sites[key].options.data.label === label) {
map.addLayer(sites[key]);
}
}
}
}
}

View File

@@ -32,9 +32,7 @@ document.addEventListener('DOMContentLoaded', async () => {
// TEMP!!
const sites = GisState.markers.sites;
for (let key of Object.keys(sites)) {
if (sites[key].options.data.id === 16) {
console.debug('I found it!', key);
}
//console.debug(sites[key].options.data);
}
});