fix(09): WR-06 default a relation list to its first sortable column
This commit is contained in:
@@ -544,10 +544,17 @@ func normalizeRelationQuery(schema *RelationSchema, candidates bool, in Relation
|
|||||||
panel = schema.Manage
|
panel = schema.Manage
|
||||||
}
|
}
|
||||||
sortKey := in.Sort
|
sortKey := in.Sort
|
||||||
if sortKey == "" && len(panel.List.Columns) > 0 {
|
if sortKey == "" {
|
||||||
sortKey = panel.List.Columns[0].Key
|
// No explicit sort: use the first sortable column, or none (the
|
||||||
}
|
// primary key then orders the page). Only a sort the client names is
|
||||||
if sortKey != "" {
|
// validated, so a panel whose first column is not sortable still loads.
|
||||||
|
for _, col := range panel.List.Columns {
|
||||||
|
if col.Sortable {
|
||||||
|
sortKey = col.Key
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
valid := false
|
valid := false
|
||||||
for _, col := range panel.List.Columns {
|
for _, col := range panel.List.Columns {
|
||||||
if col.Key == sortKey && col.Sortable {
|
if col.Key == sortKey && col.Sortable {
|
||||||
|
|||||||
@@ -94,6 +94,27 @@ func TestRelationStablePages(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestRelationDefaultSort pins WR-06: with no sort in the request the panel
|
||||||
|
// orders by its first sortable column, or by the primary key when none is
|
||||||
|
// sortable, instead of rejecting the request.
|
||||||
|
func TestRelationDefaultSort(t *testing.T) {
|
||||||
|
schema := &RelationSchema{
|
||||||
|
View: RelationPanel{List: RelationList{Columns: []RelationColumn{{Key: "label"}, {Key: "email", Sortable: true}}}},
|
||||||
|
Manage: RelationPanel{List: RelationList{Columns: []RelationColumn{{Key: "label"}}}},
|
||||||
|
}
|
||||||
|
_, _, key, _, err := normalizeRelationQuery(schema, false, RelationQuery{})
|
||||||
|
if err != nil || key != "email" {
|
||||||
|
t.Fatalf("view default sort = %q err=%v, want the first sortable column", key, err)
|
||||||
|
}
|
||||||
|
_, _, key, _, err = normalizeRelationQuery(schema, true, RelationQuery{})
|
||||||
|
if err != nil || key != "" {
|
||||||
|
t.Fatalf("manage default sort = %q err=%v, want none (primary key order)", key, err)
|
||||||
|
}
|
||||||
|
if _, _, _, _, err := normalizeRelationQuery(schema, false, RelationQuery{Sort: "label"}); err == nil {
|
||||||
|
t.Fatal("an explicit sort on a non-sortable column was accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRelationCandidateExclusions(t *testing.T) {
|
func TestRelationCandidateExclusions(t *testing.T) {
|
||||||
contract := relationTestController{}.AdminRelationContracts()[0]
|
contract := relationTestController{}.AdminRelationContracts()[0]
|
||||||
ids, err := contract.ExcludedRelatedIDs(&relationTestOwner{OwnerID: 41})
|
ids, err := contract.ExcludedRelatedIDs(&relationTestOwner{OwnerID: 41})
|
||||||
|
|||||||
Reference in New Issue
Block a user