Cars: drag the Information rows into the order you want

The rows on a car's Information tab now take a drag: they reorder as the
pointer crosses them and the arrangement saves on drop — or on dragend,
since a row released in the gap between rows never produces a drop and
would otherwise revert on the next load. Same native drag events as the
garage, so also pointer-only, and it needs write access.

The order belongs to the car, like the choice of which rows show at all,
so everyone it is shared with sees the same page. It is stored as the
full list of the 14 keys, hidden rows included: a row switched off and
back on returns to where it was rather than to the end. A key the stored
arrangement doesn't mention — a row added in a later release — follows
the arranged ones, the same rule the garage uses for a car added since
the last drag.

fieldOrder rides on the existing PUT /api/cars/{id}/view, which writes
only the lists it is given, so a drag never has to resend what is hidden.
A partial arrangement is accepted; an invented key is still a 400, which
is why normalizeHidden is now normalizeKeys — it validates an order as
well as a switched-off set.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tajniak81
2026-08-17 20:52:21 +02:00
co-authored by Claude Opus 5
parent 049da69c83
commit bc798dae49
12 changed files with 210 additions and 47 deletions
+1 -1
View File
@@ -191,7 +191,7 @@ POST /api/vehicle-providers/{provider}/import
# which PATCH /api/me {carOrder} sets)
GET /api/cars POST /api/cars
GET /api/cars/{id} PATCH /api/cars/{id} DELETE /api/cars/{id}
PUT /api/cars/{id}/view # which tabs + Information rows this car shows
PUT /api/cars/{id}/view # which tabs + Information rows this car shows, and their order
GET /api/cars/{id}/provider POST /api/cars/{id}/provider
POST /api/cars/{id}/provider/sync
GET /api/cars/{id}/service-records GET /api/cars/{id}/technical-checks
+29 -15
View File
@@ -258,11 +258,11 @@ var hideableCarFields = map[string]bool{
"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) {
// normalizeKeys validates a list of tab or field keys against the keys that
// exist, trimming blanks and duplicates. 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 page carried on as before.
func normalizeKeys(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 {
@@ -271,7 +271,7 @@ func normalizeHidden(in []string, allowed map[string]bool, what string) ([]strin
continue
}
if !allowed[key] {
return nil, fmt.Errorf("%q is not a car %s that can be hidden", key, what)
return nil, fmt.Errorf("%q is not a car %s", key, what)
}
seen[key] = true
out = append(out, key)
@@ -279,17 +279,19 @@ func normalizeHidden(in []string, allowed map[string]bool, what string) ([]strin
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.
// PUT /api/cars/{id}/view — choose what this car's page shows: which tabs, which
// rows of the Information tab, and the order those rows are laid out in. Body:
// {hiddenTabs?: [...], hiddenFields?: [...], fieldOrder?: [...]}; only the lists
// present are written, so a client can rearrange the rows without resending what
// is hidden. 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"`
FieldOrder *[]string `json:"fieldOrder"`
}
if err := decodeJSON(r, &in); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
@@ -307,7 +309,7 @@ func (s *Server) updateCarView(w http.ResponseWriter, r *http.Request) {
payload := map[string]any{}
if in.HiddenTabs != nil {
tabs, err := normalizeHidden(*in.HiddenTabs, hideableCarTabs, "tab")
tabs, err := normalizeKeys(*in.HiddenTabs, hideableCarTabs, "tab")
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
@@ -315,13 +317,25 @@ func (s *Server) updateCarView(w http.ResponseWriter, r *http.Request) {
payload["hidden_tabs"] = tabs
}
if in.HiddenFields != nil {
fields, err := normalizeHidden(*in.HiddenFields, hideableCarFields, "field")
fields, err := normalizeKeys(*in.HiddenFields, hideableCarFields, "field")
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
payload["hidden_fields"] = fields
}
if in.FieldOrder != nil {
// The same key set as the hidden fields, since every Information row can
// be moved. A partial list is accepted rather than demanding all 14: the
// rows it leaves out follow the arranged ones, which is also what makes a
// row added in a later release land at the end instead of the middle.
order, err := normalizeKeys(*in.FieldOrder, hideableCarFields, "field")
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
payload["field_order"] = order
}
if len(payload) == 0 {
writeError(w, http.StatusBadRequest, "no changes provided")
return
+44 -18
View File
@@ -2,32 +2,33 @@ package api
import "testing"
// What a car's page shows — which tabs, and which rows of the Information tab
// is stored on the car as the hidden sets, so the validation has to keep those
// sets to keys the page actually renders. Information itself stays out of the
// hideable tabs: a car with no tabs left would be a dead end.
// What a car's page shows — which tabs, which rows of the Information tab, and
// the order those rows are laid out in — is stored on the car as key lists, so
// the validation has to keep them to keys the page actually renders.
// Information itself stays out of the hideable tabs: a car with no tabs left
// would be a dead end.
func TestNormalizeHiddenTabs(t *testing.T) {
got, err := normalizeHidden([]string{" fuel ", "parts", "fuel", ""}, hideableCarTabs, "tab")
got, err := normalizeKeys([]string{" fuel ", "parts", "fuel", ""}, hideableCarTabs, "tab")
if err != nil {
t.Fatalf("normalizeHidden: %v", err)
t.Fatalf("normalizeKeys: %v", err)
}
want := []string{"fuel", "parts"} // trimmed, blanks dropped, deduped
assertKeys(t, got, want)
// Clearing the list is how a car goes back to showing everything.
if empty, err := normalizeHidden(nil, hideableCarTabs, "tab"); err != nil || len(empty) != 0 {
t.Errorf("normalizeHidden(nil) = %v, %v; want empty and no error", empty, err)
if empty, err := normalizeKeys(nil, hideableCarTabs, "tab"); err != nil || len(empty) != 0 {
t.Errorf("normalizeKeys(nil) = %v, %v; want empty and no error", empty, err)
}
// Information is the car itself; hiding it would leave a page with no tabs.
if _, err := normalizeHidden([]string{"info"}, hideableCarTabs, "tab"); err == nil {
t.Error("normalizeHidden allowed hiding the info tab, want an error")
if _, err := normalizeKeys([]string{"info"}, hideableCarTabs, "tab"); err == nil {
t.Error("normalizeKeys allowed hiding the info tab, want an error")
}
// A key from a stale or wrong client is an error, not something to drop
// quietly while the tab stays visible.
if _, err := normalizeHidden([]string{"fuel", "nonsense"}, hideableCarTabs, "tab"); err == nil {
t.Error("normalizeHidden accepted an unknown tab, want an error")
if _, err := normalizeKeys([]string{"fuel", "nonsense"}, hideableCarTabs, "tab"); err == nil {
t.Error("normalizeKeys accepted an unknown tab, want an error")
}
// The hideable set is the contract the web app's HIDEABLE_TABS mirrors:
@@ -43,18 +44,18 @@ func TestNormalizeHiddenTabs(t *testing.T) {
}
func TestNormalizeHiddenFields(t *testing.T) {
got, err := normalizeHidden([]string{"vin", " differentialOil ", "vin"}, hideableCarFields, "field")
got, err := normalizeKeys([]string{"vin", " differentialOil ", "vin"}, hideableCarFields, "field")
if err != nil {
t.Fatalf("normalizeHidden: %v", err)
t.Fatalf("normalizeKeys: %v", err)
}
assertKeys(t, got, []string{"vin", "differentialOil"})
if _, err := normalizeHidden([]string{"oilSpec", "nonsense"}, hideableCarFields, "field"); err == nil {
t.Error("normalizeHidden accepted an unknown field, want an error")
if _, err := normalizeKeys([]string{"oilSpec", "nonsense"}, hideableCarFields, "field"); err == nil {
t.Error("normalizeKeys accepted an unknown field, want an error")
}
// A tab key is not a field key — the two sets are validated separately.
if _, err := normalizeHidden([]string{"fuel"}, hideableCarFields, "field"); err == nil {
t.Error("normalizeHidden accepted a tab key as a field, want an error")
if _, err := normalizeKeys([]string{"fuel"}, hideableCarFields, "field"); err == nil {
t.Error("normalizeKeys accepted a tab key as a field, want an error")
}
// Every Information row the web app renders must be hideable; unlike the
@@ -73,6 +74,31 @@ func TestNormalizeHiddenFields(t *testing.T) {
}
}
// The arrangement of the Information rows shares the field key set — every row
// can be moved — but not the meaning: here the order of the list is the point,
// so it has to survive validation exactly as it was sent.
func TestNormalizeFieldOrder(t *testing.T) {
got, err := normalizeKeys([]string{"vin", "odometer", "oilSpec"}, hideableCarFields, "field")
if err != nil {
t.Fatalf("normalizeKeys: %v", err)
}
assertKeys(t, got, []string{"vin", "odometer", "oilSpec"})
// A key repeated by a client that lost track keeps its first position; a
// second entry for the same row would put it in two places at once.
got, err = normalizeKeys([]string{"vin", "odometer", "vin"}, hideableCarFields, "field")
if err != nil {
t.Fatalf("normalizeKeys: %v", err)
}
assertKeys(t, got, []string{"vin", "odometer"})
// A partial arrangement is fine — the rows it leaves out follow the arranged
// ones — but an invented row is still an error.
if _, err := normalizeKeys([]string{"vin", "nonsense"}, hideableCarFields, "field"); err == nil {
t.Error("normalizeKeys accepted an unknown field in an arrangement, want an error")
}
}
func assertKeys(t *testing.T, got, want []string) {
t.Helper()
if len(got) != len(want) {
+5 -3
View File
@@ -68,11 +68,12 @@ type carRecord struct {
Created string `json:"created"`
Updated string `json:"updated"`
// Switched-off tabs and Information fields. Raw because PocketBase hands back
// whatever a json field holds — null on a car nobody has configured — which
// is not a []string.
// Switched-off tabs and Information fields, plus the arrangement of the
// Information rows. Raw because PocketBase hands back whatever a json field
// holds — null on a car nobody has configured — which is not a []string.
HiddenTabs json.RawMessage `json:"hidden_tabs"`
HiddenFields json.RawMessage `json:"hidden_fields"`
FieldOrder json.RawMessage `json:"field_order"`
}
func (rec carRecord) toModel() models.Car {
@@ -101,6 +102,7 @@ func (rec carRecord) toModel() models.Car {
ProviderVehicleID: rec.ProviderVehicleID,
HiddenTabs: decodeStringList(rec.HiddenTabs),
HiddenFields: decodeStringList(rec.HiddenFields),
FieldOrder: decodeStringList(rec.FieldOrder),
Owner: rec.Owner,
Created: rec.Created,
Updated: rec.Updated,
+3
View File
@@ -46,6 +46,9 @@ var collectionsSchema = map[string][]fieldDef{
// release is on by default. Keys are validated in internal/api/cars.go.
fJSON("hidden_tabs", 2000),
fJSON("hidden_fields", 2000),
// The order the Information rows are laid out in, as field keys. Empty
// means the page's own default order.
fJSON("field_order", 2000),
// Owner of this car. Non-cascading: deleting a user must not wipe their cars.
fRelation("owner", "users", false, false),
},
+8
View File
@@ -75,6 +75,14 @@ type Car struct {
HiddenTabs []string `json:"hiddenTabs"`
HiddenFields []string `json:"hiddenFields"`
// FieldOrder is the arrangement of the Information rows, as the field keys in
// the order they are laid out. Also a property of the car, and it covers the
// hidden rows too, so a row switched back on returns to where it was. Empty
// on a car nobody has rearranged, which means the page's own default order;
// a key it doesn't mention — a row added in a later release — follows the
// arranged ones rather than appearing in the middle.
FieldOrder []string `json:"fieldOrder"`
// Owner is the user id that owns this car. Access is the requesting user's
// permission on it — "owner", "write", or "read" — computed by the API at
// read time and never persisted (omitempty; not part of the write payload).
+4
View File
@@ -279,6 +279,10 @@ const DESIRED = {
// default. Keys are validated in internal/api/cars.go.
F.json("hidden_tabs", 2000),
F.json("hidden_fields", 2000),
// The order the Information rows are laid out in, as field keys — the
// hidden ones included, so a row switched back on returns to where it was.
// Empty means the page's own default order.
F.json("field_order", 2000),
// Owner of this car. Non-cascading on purpose: deleting a user must not
// wipe their cars (account deletion in me.go intentionally leaves cars).
// required:false at the DB level — the API always sets owner on create and