Cars: arrange the garage, and choose what a car's page shows
Three things you can now set up rather than live with.
The garage takes a drag: cards reorder as you drag across them and the
arrangement saves on drop — or on dragend, since a card released in the
gap between cards never produces a drop and would otherwise revert on
the next load. It is a per-user list of car ids on the profile, so it
covers cars shared with you and never reorders anybody else's garage;
the API returns /api/cars in that order, so a client only sends the new
one back. Pointer-only: touch browsers don't fire the native drag
events, and this is not worth a dependency.
A car's page is now configurable from the gear in its header: which tabs
it shows, and which of the 14 Information rows. Both belong to the car,
so everyone it is shared with sees the same page — Fuel off on an EV
stays off for all of them — and setting them needs write access. Stored
as the hidden sets, so anything added in a later release is on by
default. PUT /api/cars/{id}/view is its own endpoint precisely so an
ordinary save of the car form, which sends every other field, can never
reveal something that was deliberately switched off. Information itself
can't be hidden: a page with no tabs left would be a dead end.
The connected-service cards fold away, remembered per device, so a
provider that reports eight sections can be trimmed to the two worth
watching. A failed section keeps a short badge in its collapsed header
and puts the provider's own message — a few hundred characters of JSON,
which used to stretch the page sideways — inside the body with
everything else.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e373497958
commit
049da69c83
18 files changed
+840
-46
No files matched your search
@@ -6,6 +6,8 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"drivervault/apiserver/internal/models"
|
||||
)
|
||||
@@ -136,9 +138,41 @@ func (s *Server) listCars(w http.ResponseWriter, r *http.Request) {
|
||||
out = append(out, m)
|
||||
}
|
||||
|
||||
// Hand the garage back in the order the user arranged it. Best effort: if the
|
||||
// profile can't be read, the default order (owned by name, then shared) still
|
||||
// renders a usable garage.
|
||||
if rec, err := s.fetchUser(r, me); err == nil {
|
||||
applyCarOrder(out, rec.carOrder())
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// applyCarOrder sorts cars into the user's arranged order, in place. Cars the
|
||||
// arrangement doesn't mention — a car added or shared since the last drag — keep
|
||||
// their relative order and follow the arranged ones, so a new car shows up at
|
||||
// the end rather than jumping into the middle.
|
||||
func applyCarOrder(cars []models.Car, order []string) {
|
||||
if len(order) == 0 || len(cars) < 2 {
|
||||
return
|
||||
}
|
||||
rank := make(map[string]int, len(order))
|
||||
for i, id := range order {
|
||||
rank[id] = i
|
||||
}
|
||||
sort.SliceStable(cars, func(i, j int) bool {
|
||||
ri, oki := rank[cars[i].ID]
|
||||
rj, okj := rank[cars[j].ID]
|
||||
if oki != okj {
|
||||
return oki // an arranged car sorts before an unarranged one
|
||||
}
|
||||
if !oki {
|
||||
return false // both unarranged: leave them as they are
|
||||
}
|
||||
return ri < rj
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) getCar(w http.ResponseWriter, r *http.Request) {
|
||||
level, rec, err := s.carAccessLevel(r.Context(), s.currentUserID(r), r.PathValue("id"))
|
||||
if err != nil {
|
||||
@@ -206,6 +240,103 @@ func (s *Server) updateCar(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, m)
|
||||
}
|
||||
|
||||
// hideableCarTabs are the car-detail tabs that can be switched off. "info" is
|
||||
// deliberately absent: it is the car itself, and a page with no tabs left would
|
||||
// be a dead end.
|
||||
var hideableCarTabs = map[string]bool{
|
||||
"provider": true, "services": true, "technical": true, "maintenance": true,
|
||||
"fuel": true, "documents": true, "parts": true, "reminders": true,
|
||||
}
|
||||
|
||||
// hideableCarFields are the Information rows that can be switched off — every
|
||||
// one of them, since unlike the tabs there is no row the page needs to keep.
|
||||
// Mirrors the car.info.* labels the web app renders.
|
||||
var hideableCarFields = map[string]bool{
|
||||
"oilSpec": true, "transmissionOil": true, "differentialOil": true,
|
||||
"brakeFluid": true, "coolant": true, "odometer": true, "serviceInterval": true,
|
||||
"nextDue": true, "registrationPlate": true, "registrationCountry": true,
|
||||
"vin": true, "fuelType": true, "buildDate": true, "firstRegistration": true,
|
||||
}
|
||||
|
||||
// normalizeHidden validates a switched-off set against the keys that exist.
|
||||
// Unknown keys are rejected rather than ignored: they can only come from a stale
|
||||
// or wrong client, and dropping them silently would hide the mistake while the
|
||||
// tab or field stayed visible.
|
||||
func normalizeHidden(in []string, allowed map[string]bool, what string) ([]string, error) {
|
||||
out := make([]string, 0, len(in))
|
||||
seen := make(map[string]bool, len(in))
|
||||
for _, key := range in {
|
||||
key = strings.TrimSpace(key)
|
||||
if key == "" || seen[key] {
|
||||
continue
|
||||
}
|
||||
if !allowed[key] {
|
||||
return nil, fmt.Errorf("%q is not a car %s that can be hidden", key, what)
|
||||
}
|
||||
seen[key] = true
|
||||
out = append(out, key)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// PUT /api/cars/{id}/view — choose what this car's page shows: which tabs, and
|
||||
// which rows of the Information tab. Body: {hiddenTabs?: [...], hiddenFields?:
|
||||
// [...]}; only the sets present are written, so a client can update one without
|
||||
// knowing the other. Its own endpoint rather than fields on the car edit, so an
|
||||
// ordinary save of the car form — which sends every other field — can never
|
||||
// reveal something somebody deliberately switched off. Needs write access: the
|
||||
// choice belongs to the car, so it is the same permission as editing it.
|
||||
func (s *Server) updateCarView(w http.ResponseWriter, r *http.Request) {
|
||||
var in struct {
|
||||
HiddenTabs *[]string `json:"hiddenTabs"`
|
||||
HiddenFields *[]string `json:"hiddenFields"`
|
||||
}
|
||||
if err := decodeJSON(r, &in); err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
level, _, err := s.carAccessLevel(r.Context(), s.currentUserID(r), r.PathValue("id"))
|
||||
if err != nil {
|
||||
writePBError(w, err)
|
||||
return
|
||||
}
|
||||
if !canWrite(level) {
|
||||
writeError(w, http.StatusForbidden, "you cannot edit this car")
|
||||
return
|
||||
}
|
||||
|
||||
payload := map[string]any{}
|
||||
if in.HiddenTabs != nil {
|
||||
tabs, err := normalizeHidden(*in.HiddenTabs, hideableCarTabs, "tab")
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
payload["hidden_tabs"] = tabs
|
||||
}
|
||||
if in.HiddenFields != nil {
|
||||
fields, err := normalizeHidden(*in.HiddenFields, hideableCarFields, "field")
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
payload["hidden_fields"] = fields
|
||||
}
|
||||
if len(payload) == 0 {
|
||||
writeError(w, http.StatusBadRequest, "no changes provided")
|
||||
return
|
||||
}
|
||||
|
||||
var rec carRecord
|
||||
if err := s.pb.Update(r.Context(), colCars, r.PathValue("id"), payload, &rec); err != nil {
|
||||
writePBError(w, err)
|
||||
return
|
||||
}
|
||||
m := rec.toModel()
|
||||
m.Access = level
|
||||
writeJSON(w, http.StatusOK, m)
|
||||
}
|
||||
|
||||
func (s *Server) deleteCar(w http.ResponseWriter, r *http.Request) {
|
||||
level, _, err := s.carAccessLevel(r.Context(), s.currentUserID(r), r.PathValue("id"))
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user