From 7f0681600853a95862e493c4f9dd2de425f39882 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Fri, 9 Jun 2023 12:56:07 +0300 Subject: [PATCH] normalized active sort on collection schema change --- ui/src/components/records/PageRecords.svelte | 35 ++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/ui/src/components/records/PageRecords.svelte b/ui/src/components/records/PageRecords.svelte index 629a7cf0..8d6602dd 100644 --- a/ui/src/components/records/PageRecords.svelte +++ b/ui/src/components/records/PageRecords.svelte @@ -46,6 +46,10 @@ reset(); } + $: if ($activeCollection?.id) { + normalizeSort(); + } + // keep the url params in sync $: if (sort || filter || $activeCollection?.id) { const query = new URLSearchParams({ @@ -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 = ""; + } } }