From 360db5aaad22ebd9c39e96dbac45eca3db8d6a0c Mon Sep 17 00:00:00 2001 From: Karl Determe Date: Tue, 18 Aug 2026 16:05:57 +0200 Subject: [PATCH] feat: implement structures management module with index views, location geojson endpoints, and modal for adding assets --- loko/assets/static/assets/asset_add_modal.js | 39 ++++++++---- .../assets/components/add_asset_modal.html | 6 ++ .../assets/structures_location_detail.html | 2 +- loko/assets/tests.py | 59 +++++++++++++++++++ loko/assets/views/structures.py | 5 ++ 5 files changed, 99 insertions(+), 12 deletions(-) diff --git a/loko/assets/static/assets/asset_add_modal.js b/loko/assets/static/assets/asset_add_modal.js index f201b11..3be3477 100644 --- a/loko/assets/static/assets/asset_add_modal.js +++ b/loko/assets/static/assets/asset_add_modal.js @@ -20,6 +20,18 @@ const categoriesCache = {}; + function populateCategorySelectIfEmpty(select, categories) { + if (!select || select.options.length > 1 || !categories || categories.length === 0) { + return; + } + categories.forEach(cat => { + const option = document.createElement('option'); + option.value = cat.id; + option.textContent = cat.name; + select.appendChild(option); + }); + } + function initializeForm(form, modalId) { const thematicCode = form.dataset.thematic; const categorySelect = form.querySelector('.asset-category-select'); @@ -31,17 +43,22 @@ const submitBtn = form.querySelector(`#${modalId}Submit`); // Fetch and cache categories for this thematic - if (thematicCode && !categoriesCache[thematicCode]) { - fetch(`/assets/${thematicCode}/categories/`) - .then(response => response.json()) - .then(data => { - if (data.categories) { - categoriesCache[thematicCode] = data.categories; - } - }) - .catch(error => { - console.error('Error prefetching categories:', error); - }); + if (thematicCode) { + if (categoriesCache[thematicCode]) { + populateCategorySelectIfEmpty(categorySelect, categoriesCache[thematicCode]); + } else { + fetch(`/assets/${thematicCode}/categories/`) + .then(response => response.json()) + .then(data => { + if (data.categories) { + categoriesCache[thematicCode] = data.categories; + populateCategorySelectIfEmpty(categorySelect, data.categories); + } + }) + .catch(error => { + console.error('Error prefetching categories:', error); + }); + } } async function onCategoryChange() { diff --git a/loko/assets/templates/assets/components/add_asset_modal.html b/loko/assets/templates/assets/components/add_asset_modal.html index 2479f99..b40b4a9 100644 --- a/loko/assets/templates/assets/components/add_asset_modal.html +++ b/loko/assets/templates/assets/components/add_asset_modal.html @@ -23,6 +23,12 @@ {% if intervention_id %} {% endif %} + {% if location %} + + {% endif %} + {% if structure %} + + {% endif %}