Skip to content

Commit

Permalink
normalized active sort on collection schema change
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Jun 9, 2023
1 parent e4a90f6 commit 7f06816
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions ui/src/components/records/PageRecords.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
reset();
}
$: if ($activeCollection?.id) {
normalizeSort();
}
// keep the url params in sync
$: if (sort || filter || $activeCollection?.id) {
const query = new URLSearchParams({
Expand All @@ -63,12 +67,31 @@
filter = "";
sort = "-created";
// clear default sort if created field is not available
if (
$activeCollection?.$isView &&
!CommonHelper.extractColumnsFromQuery($activeCollection.options.query).includes("created")
) {
sort = "";
normalizeSort();
}
// ensures that the sort fields exist in the collection
async function normalizeSort() {
if (!sort) {
return; // nothing to normalize
}
const collectionFields = CommonHelper.getAllCollectionIdentifiers($activeCollection);
const sortFields = sort.split(",").map((f) => {
if (f.startsWith("+") || f.startsWith("-")) {
return f.substring(1);
}
return f;
});
// invalid sort expression or missing sort field
if (sortFields.filter((f) => collectionFields.includes(f)).length != sortFields.length) {
if (collectionFields.includes("created")) {
sort = "-created";
} else {
sort = "";
}
}
}
Expand Down

0 comments on commit 7f06816

Please sign in to comment.