Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create sharable findBindingIndex function for field selection #831

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/components/editor/Bindings/FieldSelection/index.tsx
Original file line number Diff line number Diff line change
@@ -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,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 (
Original file line number Diff line number Diff line change
@@ -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) {
11 changes: 11 additions & 0 deletions src/utils/workflow-utils.ts
Original file line number Diff line number Diff line change
@@ -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 } : {};
};