Skip to content

Commit

Permalink
Create sharable findBindingIndex function for field selection (#831)
Browse files Browse the repository at this point in the history
* Getting the proper index for field selection
Sharing the functionality in a new function

* Making the funtion super safe
  • Loading branch information
travjenkins authored Nov 28, 2023
1 parent 0203a0e commit 31c2971
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/components/editor/Bindings/FieldSelection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -162,11 +165,14 @@ function FieldSelectionViewer({ collectionName }: Props) {
)
)?.constraints;

const bindingIndex: number = getBindingIndex(
draftSpecs[0].spec.bindings,
collectionName
);
const selectedBinding: Schema | undefined =
draftSpecs[0].spec.bindings.find(
(binding: any) => binding.source === collectionName
);

bindingIndex > -1
? draftSpecs[0].spec.bindings[bindingIndex]
: undefined;
let evaluatedFieldMetadata: FieldMetadata | undefined;

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/workflow-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ export const getCollectionName = (binding: any) => {
return getCollectionNameDirectly(scopedBinding);
};

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) => {
return disable ? { disable } : {};
};
Expand Down

0 comments on commit 31c2971

Please sign in to comment.