diff options
| author | mo khan <mo@mokhan.ca> | 2025-04-25 21:25:40 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-04-28 09:07:31 -0600 |
| commit | 9b01d1616e130a589151bf1273e41181ecc727f4 (patch) | |
| tree | 639ec3b3c3857042a551c8e88b09413f590ebcec /app/controllers/dashboard | |
| parent | 13ab8de7d09b5d4b10132828277d17ba0543b901 (diff) | |
feat: use htmx to render partials
Diffstat (limited to 'app/controllers/dashboard')
| -rw-r--r-- | app/controllers/dashboard/controller.go | 18 | ||||
| -rw-r--r-- | app/controllers/dashboard/controller_test.go | 4 | ||||
| -rw-r--r-- | app/controllers/dashboard/dto.go | 5 |
3 files changed, 26 insertions, 1 deletions
diff --git a/app/controllers/dashboard/controller.go b/app/controllers/dashboard/controller.go index 220871f..0f165ad 100644 --- a/app/controllers/dashboard/controller.go +++ b/app/controllers/dashboard/controller.go @@ -20,6 +20,7 @@ func (c *Controller) MountTo(mux *http.ServeMux) { requireUser := middleware.RequireUser() mux.Handle("GET /dashboard", requireUser(http.HandlerFunc(c.Show))) + mux.Handle("GET /dashboard/nav", http.HandlerFunc(c.Navigation)) } func (c *Controller) Show(w http.ResponseWriter, r *http.Request) { @@ -35,3 +36,20 @@ func (c *Controller) Show(w http.ResponseWriter, r *http.Request) { return } } + +func (c *Controller) Navigation(w http.ResponseWriter, r *http.Request) { + currentUser := cfg.CurrentUser.From(r.Context()) + + w.WriteHeader(http.StatusOK) + w.Header().Add("Content-Type", "text/html") + + dto := &NavigationDTO{ + CurrentUser: currentUser, + IsLoggedIn: currentUser != nil, + } + if err := views.Render(w, "dashboard/nav", dto); err != nil { + log.WithFields(r.Context(), log.Fields{"error": err}) + w.WriteHeader(http.StatusInternalServerError) + return + } +} diff --git a/app/controllers/dashboard/controller_test.go b/app/controllers/dashboard/controller_test.go index 629a03a..f6b2f43 100644 --- a/app/controllers/dashboard/controller_test.go +++ b/app/controllers/dashboard/controller_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test" @@ -22,7 +23,8 @@ func TestController(t *testing.T) { mux.ServeHTTP(w, r) - assert.Equal(t, http.StatusNotFound, w.Code) + require.Equal(t, http.StatusFound, w.Code) + assert.Equal(t, "/", w.Header().Get("Location")) }) }) diff --git a/app/controllers/dashboard/dto.go b/app/controllers/dashboard/dto.go index 6ffe027..0a7f39d 100644 --- a/app/controllers/dashboard/dto.go +++ b/app/controllers/dashboard/dto.go @@ -5,3 +5,8 @@ import "gitlab.com/gitlab-org/software-supply-chain-security/authorization/spark type ViewDashboardDTO struct { CurrentUser *domain.User } + +type NavigationDTO struct { + IsLoggedIn bool + CurrentUser *domain.User +} |
