Move pesticide settings outside the mocks

And build a system for pulling common code out of the handlers
This commit is contained in:
Eli Ribble 2026-02-16 20:16:57 +00:00
parent f6879ac094
commit 38f64783ac
No known key found for this signature in database
3 changed files with 56 additions and 5 deletions

View file

@ -1,6 +1,6 @@
{{ template "sync/layout/base.html" . }}
{{ template "sync/layout/authenticated.html" . }}
{{ define "title" }}Dash{{ end }}
{{ define "title" }}Settings - Pesticide{{ end }}
{{ define "extraheader" }}
<style>
.target-icon {
@ -39,7 +39,7 @@
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="mb-0">Pesticide Products Configuration</h1>
<a
href="{{ .URLs.SettingPesticideAdd }}"
href="{{ .URL.SettingPesticideAdd }}"
class="btn btn-primary"
id="addProductBtn"
>

View file

@ -1,8 +1,12 @@
package sync
import (
"context"
"net/http"
"github.com/Gleipnir-Technology/nidus-sync/api"
"github.com/Gleipnir-Technology/nidus-sync/auth"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/html"
"github.com/go-chi/chi/v5"
)
@ -20,7 +24,6 @@ func Router() chi.Router {
// Mock endpoints
r.Get("/mock", renderMockList)
addMock(r, "/mock/admin", "sync/mock/admin.html")
addMock(r, "/mock/admin", "sync/mock/admin.html")
addMock(r, "/mock/dispatch", "sync/mock/dispatch.html")
addMock(r, "/mock/dispatch-results", "sync/mock/dispatch-results.html")
addMock(r, "/mock/report", "sync/mock/report.html")
@ -32,7 +35,6 @@ func Router() chi.Router {
addMock(r, "/mock/report/{code}/update", "sync/mock/report-update.html")
addMock(r, "/mock/service-request/{code}", "sync/mock/service-request-detail.html")
addMock(r, "/mock/setting", "sync/mock/setting.html")
addMock(r, "/mock/setting/pesticide", "sync/mock/setting-pesticide.html")
addMock(r, "/mock/setting/pesticide/add", "sync/mock/setting-pesticide-add.html")
addMock(r, "/mock/setting/user", "sync/mock/setting-user.html")
addMock(r, "/mock/setting/user/add", "sync/mock/setting-user-add.html")
@ -60,6 +62,7 @@ func Router() chi.Router {
r.Method("GET", "/setting", auth.NewEnsureAuth(getSetting))
r.Method("GET", "/setting/district", auth.NewEnsureAuth(getSettingDistrict))
r.Method("GET", "/setting/integration", auth.NewEnsureAuth(getSettingIntegration))
r.Method("GET", "/setting/pesticide", authenticatedHandler(getSettingPesticide))
r.Method("GET", "/signout", auth.NewEnsureAuth(getSignout))
r.Method("GET", "/source/{globalid}", auth.NewEnsureAuth(getSource))
r.Method("GET", "/stadia", auth.NewEnsureAuth(getStadia))
@ -69,3 +72,43 @@ func Router() chi.Router {
html.AddStaticRoute(r, "/static")
return r
}
type errorWithStatus struct {
Message string
Status int
}
func (e *errorWithStatus) Error() string {
return e.Message
}
type handlerFunction func(context.Context, *models.User) (string, interface{}, *errorWithStatus)
type wrappedHandler func(http.ResponseWriter, *http.Request)
type contentAuthenticated struct {
C interface{}
URL ContentURL
User User
}
// w http.ResponseWriter, r *http.Request, u *models.User) {
func authenticatedHandler(f handlerFunction) http.Handler {
return auth.NewEnsureAuth(func(w http.ResponseWriter, r *http.Request, u *models.User) {
ctx := r.Context()
userContent, err := contentForUser(ctx, u)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
template, content, e := f(ctx, u)
if err != nil {
//log.Warn().Int("status", s).Err(e).Str("user message", m).Msg("Responding with an error from sync pages")
http.Error(w, err.Error(), e.Status)
return
}
html.RenderOrError(w, template, contentAuthenticated{
C: content,
URL: newContentURL(),
User: userContent,
})
})
}

View file

@ -1,6 +1,7 @@
package sync
import (
"context"
"net/http"
"github.com/Gleipnir-Technology/bob"
@ -126,3 +127,10 @@ func getSettingIntegration(w http.ResponseWriter, r *http.Request, u *models.Use
}
html.RenderOrError(w, "sync/setting-integration.html", data)
}
type contentSettingPesticide struct{}
func getSettingPesticide(ctx context.Context, user *models.User) (string, interface{}, *errorWithStatus) {
content := contentSettingPesticide{}
return "sync/setting-pesticide.html", content, nil
}