Skip to content

Commit a691d94

Browse files
committed
Establishes active scope persistence
Refactors the global controller advice to separately manage all available scopes and the active scope ID. The active scope is now determined from a `cosmos-scope-id` cookie. Introduces a new endpoint to handle Htmx requests from the scope selection dropdown, storing the chosen scope ID in the `cosmos-scope-id` cookie. Updates the navigation fragment to use the new endpoint for setting the active scope and pre-selects the dropdown based on the value retrieved from the cookie. This allows the user's active scope selection to persist across requests.
1 parent fa7fc29 commit a691d94

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package dev.ikm.server.cosmos.global;
22

3+
import dev.ikm.server.cosmos.scope.ScopeDTO;
34
import dev.ikm.server.cosmos.scope.ScopeService;
45
import org.springframework.ui.Model;
56
import org.springframework.web.bind.annotation.ControllerAdvice;
7+
import org.springframework.web.bind.annotation.CookieValue;
68
import org.springframework.web.bind.annotation.ModelAttribute;
79

10+
import java.util.List;
11+
import java.util.UUID;
12+
813
@ControllerAdvice
914
public class GlobalControllerAdvice {
1015

@@ -14,12 +19,14 @@ public GlobalControllerAdvice(ScopeService scopeService) {
1419
this.scopeService = scopeService;
1520
}
1621

17-
@ModelAttribute
18-
public void addGlobalAttributes(Model model) {
19-
// This ensures 'scopes' is available on every page for the navigation dropdown
20-
model.addAttribute("scopes", scopeService.retrieveAllScopes());
22+
@ModelAttribute("scopes")
23+
public List<ScopeDTO> addScopesToModel() {
24+
return scopeService.retrieveAllScopes();
25+
}
2126

22-
// You can also handle 'activeScope' here if it's stored in the session
23-
// or determine it based on the request
27+
@ModelAttribute("activeScopeId")
28+
public UUID addScopeSelectionToModel(
29+
@CookieValue(name = "cosmos-scope-id", defaultValue = "none") String scopeSelectionId) {
30+
return UUID.fromString(scopeSelectionId);
2431
}
2532
}

src/main/java/dev/ikm/server/cosmos/scope/ScopeController.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
import dev.ikm.server.cosmos.api.coordinate.CoordinateService;
44
import io.github.wimdeblauwe.htmx.spring.boot.mvc.HxRequest;
5+
import jakarta.servlet.http.Cookie;
6+
import jakarta.servlet.http.HttpServletResponse;
57
import org.slf4j.Logger;
68
import org.slf4j.LoggerFactory;
79
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.http.ResponseEntity;
811
import org.springframework.stereotype.Controller;
912
import org.springframework.ui.Model;
1013
import org.springframework.web.bind.annotation.DeleteMapping;
1114
import org.springframework.web.bind.annotation.GetMapping;
1215
import org.springframework.web.bind.annotation.ModelAttribute;
1316
import org.springframework.web.bind.annotation.PathVariable;
1417
import org.springframework.web.bind.annotation.PostMapping;
18+
import org.springframework.web.bind.annotation.RequestParam;
1519
import org.springframework.web.bind.annotation.ResponseBody;
1620
import org.springframework.web.servlet.view.FragmentsRendering;
1721

@@ -93,6 +97,15 @@ public FragmentsRendering postScope(@ModelAttribute("scopeForm") ScopeFormDTO sc
9397
.build();
9498
}
9599

100+
@HxRequest
101+
@PostMapping("/scope/selected")
102+
public ResponseEntity<Void> postSelectedScope(@RequestParam("scopeId") UUID id, HttpServletResponse response, Model model) {
103+
Cookie cookie = new Cookie("cosmos-scope-id", id.toString());
104+
cookie.setPath("/");
105+
response.addCookie(cookie);
106+
return ResponseEntity.noContent().build();
107+
}
108+
96109
@HxRequest
97110
@DeleteMapping("/scope/{id}")
98111
@ResponseBody

src/main/resources/templates/fragments/layout/navigation.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@
9090
<ul class="navbar-nav ms-auto">
9191
<li class="nav-item">
9292
<div data-bs-theme="dark">
93-
<select class="form-select" id="scopeSelect" name="scopeId" aria-label="Select Scope">
94-
<option value="" th:selected="${activeScope == null}">Select Scope</option>
93+
<select class="form-select" id="scopeSelect" name="scopeId" aria-label="Select Scope"
94+
hx-post="/scope/selected"
95+
hx-trigger="change">
96+
<option value="" th:selected="${activeScopeId == null}">Select Scope</option>
9597
<option th:each="scope : ${scopes}"
9698
th:value="${scope.id()}"
9799
th:text="${scope.name()}"
98-
th:selected="${activeScope != null and activeScope.id == scope.id}">Scope Name</option>
100+
th:selected="${activeScopeId != null and activeScopeId == scope.id()}">Scope Name</option>
99101
</select>
100102
</div>
101103
</li>

0 commit comments

Comments
 (0)