From c300005e4650b12e310933c9b92c46ba97d6b32f Mon Sep 17 00:00:00 2001 From: Travis Jenkins Date: Mon, 27 Nov 2023 14:48:30 -0500 Subject: [PATCH 1/2] Getting the proper index for field selection Sharing the functionality in a new function --- .../editor/Bindings/FieldSelection/index.tsx | 17 +++++++++++------ .../FieldSelection/useFieldSelection.ts | 9 +++++++-- src/utils/workflow-utils.ts | 6 ++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/components/editor/Bindings/FieldSelection/index.tsx b/src/components/editor/Bindings/FieldSelection/index.tsx index 9fcc61c06..035c03e97 100644 --- a/src/components/editor/Bindings/FieldSelection/index.tsx +++ b/src/components/editor/Bindings/FieldSelection/index.tsx @@ -45,7 +45,10 @@ import { } from 'stores/FormState/hooks'; import { FormStatus } from 'stores/FormState/types'; import { Schema } from 'types'; -import { evaluateRequiredIncludedFields } from 'utils/workflow-utils'; +import { + evaluateRequiredIncludedFields, + getBindingIndex, +} from 'utils/workflow-utils'; interface Props { collectionName: string; @@ -162,11 +165,13 @@ function FieldSelectionViewer({ collectionName }: Props) { ) )?.constraints; - const selectedBinding: Schema | undefined = - draftSpecs[0].spec.bindings.find( - (binding: any) => binding.source === collectionName - ); - + const bindingIndex: number = getBindingIndex( + draftSpecs[0].spec.bindings, + collectionName + ); + const selectedBinding: Schema | undefined = bindingIndex + ? draftSpecs[0].spec.bindings[bindingIndex] + : undefined; let evaluatedFieldMetadata: FieldMetadata | undefined; if ( diff --git a/src/components/editor/Bindings/FieldSelection/useFieldSelection.ts b/src/components/editor/Bindings/FieldSelection/useFieldSelection.ts index afeee4456..c351f60b6 100644 --- a/src/components/editor/Bindings/FieldSelection/useFieldSelection.ts +++ b/src/components/editor/Bindings/FieldSelection/useFieldSelection.ts @@ -12,6 +12,7 @@ import { omit } from 'lodash'; import { useCallback } from 'react'; import { Schema } from 'types'; import { hasLength } from 'utils/misc-utils'; +import { getBindingIndex } from 'utils/workflow-utils'; function useFieldSelection(collectionName: string) { // Bindings Editor Store @@ -24,8 +25,12 @@ function useFieldSelection(collectionName: string) { return useCallback( async (draftSpec: DraftSpecQuery) => { - const bindingIndex: number = draftSpec.spec.bindings.findIndex( - (binding: any) => binding.source === collectionName + // TODO (field selection) we should make it so this does not need to be figured out + // every call as it is pretty wasteful. Since we are passing in the spec already maybe + // we just pass in the index along with it? Not sure how to do this and make it feel good. + const bindingIndex: number = getBindingIndex( + draftSpec.spec.bindings, + collectionName ); if (!mutateDraftSpecs || bindingIndex === -1) { diff --git a/src/utils/workflow-utils.ts b/src/utils/workflow-utils.ts index 68cf25134..e24679b59 100644 --- a/src/utils/workflow-utils.ts +++ b/src/utils/workflow-utils.ts @@ -44,6 +44,12 @@ export const getCollectionName = (binding: any) => { return getCollectionNameDirectly(scopedBinding); }; +export const getBindingIndex = (bindings: any[], collectionName: string) => { + return bindings.findIndex( + (binding: any) => getCollectionName(binding) === collectionName + ); +}; + export const getDisableProps = (disable: boolean | undefined) => { return disable ? { disable } : {}; }; From d89d337b6ad9d1da828591a4c295e9f2373e7649 Mon Sep 17 00:00:00 2001 From: Travis Jenkins Date: Mon, 27 Nov 2023 19:43:12 -0500 Subject: [PATCH 2/2] Making the funtion super safe --- .../editor/Bindings/FieldSelection/index.tsx | 7 ++++--- src/utils/workflow-utils.ts | 13 +++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/components/editor/Bindings/FieldSelection/index.tsx b/src/components/editor/Bindings/FieldSelection/index.tsx index 035c03e97..ca8a6fc02 100644 --- a/src/components/editor/Bindings/FieldSelection/index.tsx +++ b/src/components/editor/Bindings/FieldSelection/index.tsx @@ -169,9 +169,10 @@ function FieldSelectionViewer({ collectionName }: Props) { draftSpecs[0].spec.bindings, collectionName ); - const selectedBinding: Schema | undefined = bindingIndex - ? draftSpecs[0].spec.bindings[bindingIndex] - : undefined; + const selectedBinding: Schema | undefined = + bindingIndex > -1 + ? draftSpecs[0].spec.bindings[bindingIndex] + : undefined; let evaluatedFieldMetadata: FieldMetadata | undefined; if ( diff --git a/src/utils/workflow-utils.ts b/src/utils/workflow-utils.ts index e24679b59..c50e1522b 100644 --- a/src/utils/workflow-utils.ts +++ b/src/utils/workflow-utils.ts @@ -44,10 +44,15 @@ export const getCollectionName = (binding: any) => { return getCollectionNameDirectly(scopedBinding); }; -export const getBindingIndex = (bindings: any[], collectionName: string) => { - return bindings.findIndex( - (binding: any) => getCollectionName(binding) === collectionName - ); +export const getBindingIndex = ( + bindings: any[] | null | undefined, + collectionName: string +) => { + return bindings?.findIndex + ? bindings.findIndex( + (binding: any) => getCollectionName(binding) === collectionName + ) + : -1; }; export const getDisableProps = (disable: boolean | undefined) => {