Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Tabbed navigation #617

Merged
merged 13 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/server/assets/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@
{{if .currentRealm}}
<ul class="nav mr-auto flex-column flex-md-row">
<li class="nav-item">
<a class="nav-link {{if .homeTab}}active{{end}}" href="/home">Issue code</a>
<a class="nav-link {{if eq .path.CurrentFile "home"}}active{{end}}" href="/home">Issue code</a>
</li>
<li class="nav-item">
<a class="nav-link {{if .checkTab}}active{{end}}" href="/code/status">Check code status</a>
<a class="nav-link {{if eq .path.CurrentDir "/code/"}}active{{end}}" href="/code/status">Check code status</a>
</li>
</ul>
{{end}}
Expand Down
4 changes: 4 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ func realMain(ctx context.Context) error {
requireSession := middleware.RequireSession(ctx, sessions, h)
r.Use(requireSession)

// Include the current URI
currentRoute := middleware.CurrentRoute()
r.Use(currentRoute)

// Create common middleware
requireAuth := middleware.RequireAuth(ctx, cacher, auth, db, h, config.SessionIdleTimeout, config.SessionDuration)
requireVerified := middleware.RequireVerified(ctx, auth, db, h, config.SessionDuration)
Expand Down
1 change: 0 additions & 1 deletion pkg/controller/codestatus/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ func (c *Controller) HandleIndex() http.Handler {
func (c *Controller) renderStatus(ctx context.Context, w http.ResponseWriter, code *database.VerificationCode) {
m := controller.TemplateMapFromContext(ctx)
m["code"] = code
m["checkTab"] = true
c.h.RenderHTML(w, "code/status", m)
}
1 change: 0 additions & 1 deletion pkg/controller/codestatus/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,5 @@ type Code struct {
func (c *Controller) renderShow(ctx context.Context, w http.ResponseWriter, code Code) {
m := controller.TemplateMapFromContext(ctx)
m["code"] = code
m["checkTab"] = true
c.h.RenderHTML(w, "code/show", m)
}
1 change: 0 additions & 1 deletion pkg/controller/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func (c *Controller) HandleHome() http.Handler {

// Set test date params
now := time.Now().UTC()
m["homeTab"] = true
m["maxDate"] = now.Format("2006-01-02")
m["minDate"] = now.Add(c.pastDaysDuration).Format("2006-01-02")
m["maxSymptomDays"] = c.displayAllowedDays
Expand Down
58 changes: 58 additions & 0 deletions pkg/controller/middleware/currentpage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2020 Google LLC
whaught marked this conversation as resolved.
Show resolved Hide resolved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package middleware defines shared middleware for handlers.
package middleware

import (
"net/http"
"path"

"github.com/google/exposure-notifications-verification-server/pkg/controller"
"github.com/gorilla/mux"
)

type Path struct {
uri string
}

func CurrentRoute() mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

m := controller.TemplateMapFromContext(ctx)
m["path"] = &Path{uri: r.RequestURI}

ctx = controller.WithTemplateMap(ctx, m)
*r = *r.WithContext(ctx)

next.ServeHTTP(w, r)
})
}
}

func (p *Path) CurrentPath() string {
return p.uri
}

func (p *Path) CurrentDir() string {
dir, _ := path.Split(p.uri)
return dir
}

func (p *Path) CurrentFile() string {
_, file := path.Split(p.uri)
return file
}