From 2ef014b0700eb51d47d6c8772e3f9f408c07f3a5 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 21 Sep 2023 13:15:42 +0200 Subject: [PATCH] Prevent extra fetches when not required --- .../app/forms/RelationshipField.svelte | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/client/src/components/app/forms/RelationshipField.svelte b/packages/client/src/components/app/forms/RelationshipField.svelte index 3dcb640a55..481849db2a 100644 --- a/packages/client/src/components/app/forms/RelationshipField.svelte +++ b/packages/client/src/components/app/forms/RelationshipField.svelte @@ -45,14 +45,25 @@ $: expandedDefaultValue = expand(defaultValue) $: primaryDisplay = tableDefinition?.primaryDisplay || "_id" - let prevTerm + let lastFetchedTerm $: { - // Check if the fetch changed, ignoring differences between empty/null/undefined - if (primaryDisplay && prevTerm != fetchTerm) { - prevTerm = fetchTerm - fetch.update({ - query: { string: { [primaryDisplay]: fetchTerm } }, - }) + const termChangedSinceFetch = (lastFetchedTerm || "") !== (fetchTerm || "") + + const allRowsFetched = + !lastFetchedTerm && $fetch.rows.length === $fetch.totalRows + if (!allRowsFetched && termChangedSinceFetch) { + debugger + // Don't request until we have the primary display + if (primaryDisplay) { + lastFetchedTerm = fetchTerm + fetch.update({ + query: { string: { [primaryDisplay]: fetchTerm } }, + }) + } + } else { + options = fetchTerm + ? $fetch.rows.filter(row => row[primaryDisplay].includes(fetchTerm)) + : $fetch.rows } }