Skip to content

Commit

Permalink
Merge pull request #3245 from ingef/use-selectconfig-for-concept-init…
Browse files Browse the repository at this point in the history
…ialization

Take selectConfig into account when initializing concepts in form dropzones
  • Loading branch information
Kadrian authored Dec 11, 2023
2 parents 87c66b2 + 8e96df1 commit 9fc89bb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ const FormConceptGroup = (props: Props) => {
allowlistedTables: props.allowlistedTables,
blocklistedTables: props.blocklistedTables,
};
const selectConfig = {
allowlistedSelects: props.allowlistedSelects,
blocklistedSelects: props.blocklistedSelects,
};

// indicator if it should be scrolled down back to the dropZone
const [scrollToDropzone, setScrollToDropzone] = useState<boolean>(false);
Expand Down Expand Up @@ -163,6 +167,7 @@ const FormConceptGroup = (props: Props) => {
onChange: props.onChange,
defaults,
tableConfig,
selectConfig,
isValidConcept: props.isValidConcept,
});

Expand Down Expand Up @@ -216,7 +221,7 @@ const FormConceptGroup = (props: Props) => {

const concept = isMovedObject(item)
? copyConcept(item)
: initializeConcept(item, defaults, tableConfig);
: initializeConcept(item, defaults, tableConfig, selectConfig);

let insertIndex = i;
let newPropsValue = props.value;
Expand Down Expand Up @@ -301,7 +306,7 @@ const FormConceptGroup = (props: Props) => {

const concept = isMovedObject(item)
? copyConcept(item)
: initializeConcept(item, defaults, tableConfig);
: initializeConcept(item, defaults, tableConfig, selectConfig);
return props.onChange(
addConcept(
addValue(props.value, newValue),
Expand Down Expand Up @@ -435,7 +440,12 @@ const FormConceptGroup = (props: Props) => {
props.value,
i,
j,
initializeConcept(item, defaults, tableConfig),
initializeConcept(
item,
defaults,
tableConfig,
selectConfig,
),
),
);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { TreesT } from "../../concept-trees/reducer";
import { mergeFilterOptions } from "../../model/filter";
import { NodeResetConfig } from "../../model/node";
import { resetSelects } from "../../model/select";
import { SelectConfig, resetSelects } from "../../model/select";
import { resetTables, tableWithDefaults } from "../../model/table";
import { filterSuggestionToSelectOption } from "../../query-node-editor/suggestionsHelper";
import type {
Expand Down Expand Up @@ -278,6 +278,7 @@ export const addConceptsFromFile = (
resolvedConcepts: string[],

tableConfig: TableConfig,
selectConfig: SelectConfig,
defaults: ConceptListDefaultsType,
isValidConcept: ((item: FormConceptNodeT) => boolean) | undefined,

Expand All @@ -301,7 +302,12 @@ export const addConceptsFromFile = (

if (!queryElement) return value;

const concept = initializeConcept(queryElement, defaults, tableConfig);
const concept = initializeConcept(
queryElement,
defaults,
tableConfig,
selectConfig,
);

if (!concept || (!!isValidConcept && !isValidConcept(concept))) return value;

Expand All @@ -325,6 +331,7 @@ export const initializeConcept = (
item: FormConceptNodeT,
defaults: ConceptListDefaultsType,
tableConfig: TableConfig,
selectConfig: SelectConfig,
) => {
if (!item) return item;

Expand All @@ -336,8 +343,8 @@ export const initializeConcept = (
...item,
excludeFromSecondaryId: false,
excludeTimestamps: false,
tables: resetTables(item.tables, { useDefaults: true }),
selects: resetSelects(item.selects, { useDefaults: true }),
tables: resetTables(item.tables, { useDefaults: true, selectConfig }),
selects: resetSelects(item.selects, { useDefaults: true, selectConfig }),
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "../../upload-concept-list-modal/actions";
import type { ConceptListDefaults as ConceptListDefaultsType } from "../config-types";

import { SelectConfig } from "../../model/select";
import {
addConceptsFromFile,
FormConceptGroupT,
Expand All @@ -30,13 +31,15 @@ export const useUploadConceptListModal = ({
newValue,
defaults,
tableConfig,
selectConfig,
isValidConcept,
}: {
value: FormConceptGroupT[];
onChange: (value: FormConceptGroupT[]) => void;
newValue: FormConceptGroupT;
defaults: ConceptListDefaultsType;
tableConfig: TableConfig;
selectConfig: SelectConfig;
isValidConcept?: (concept: DragItemConceptTreeNode) => boolean;
}) => {
const { t } = useTranslation();
Expand Down Expand Up @@ -108,6 +111,7 @@ export const useUploadConceptListModal = ({
resolvedConcepts,

tableConfig,
selectConfig,
defaults,
isValidConcept,

Expand Down
3 changes: 2 additions & 1 deletion frontend/src/js/model/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {
} from "../standard-query-editor/types";
import { PossibleDroppableObject } from "../ui-components/Dropzone";

import { objectHasNonDefaultSelects } from "./select";
import { SelectConfig, objectHasNonDefaultSelects } from "./select";
import {
tablesHaveEmptySettings,
tablesHaveFilterValues,
Expand All @@ -28,6 +28,7 @@ import {

export interface NodeResetConfig {
useDefaults?: boolean;
selectConfig?: SelectConfig;
}

export const nodeIsConceptQueryNode = (
Expand Down
34 changes: 20 additions & 14 deletions frontend/src/js/model/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@ export function sortSelects(selects: SelectorT[]) {
.sort((a, b) => (a.label < b.label ? -1 : 1));
}

const resetSelected = (select: SelectorT, config: NodeResetConfig) => ({
...select,
selected: config.useDefaults ? !!select.default : false,
});

export const resetSelects = (
selects: SelectorT[],
config: NodeResetConfig = {},
): SelectedSelectorT[] =>
selects.map((select) => resetSelected(select, config));

function selectTypesMatch(
resultType1: SelectorResultType,
resultType2: SelectorResultType,
Expand All @@ -63,20 +52,37 @@ export function selectIsWithinTypes(
);
}

interface AllowBlocklistedSelects {
export interface SelectConfig {
blocklistedSelects?: SelectorResultType[];
allowlistedSelects?: SelectorResultType[];
}

export const isSelectDisabled = (
select: SelectorT,
{ blocklistedSelects, allowlistedSelects }: AllowBlocklistedSelects,
{ blocklistedSelects, allowlistedSelects }: SelectConfig,
) =>
(!!allowlistedSelects && !selectIsWithinTypes(select, allowlistedSelects)) ||
(!!blocklistedSelects && selectIsWithinTypes(select, blocklistedSelects));

export const isValidSelect =
({ blocklistedSelects, allowlistedSelects }: AllowBlocklistedSelects) =>
({ blocklistedSelects, allowlistedSelects }: SelectConfig) =>
(select: SelectedSelectorT) =>
!!select.selected &&
!isSelectDisabled(select, { blocklistedSelects, allowlistedSelects });

const resetSelected = (select: SelectorT, config: NodeResetConfig) => {
if (config.selectConfig && !isValidSelect(config.selectConfig)(select)) {
return { ...select, selected: false };
}

return {
...select,
selected: config.useDefaults ? !!select.default : false,
};
};

export const resetSelects = (
selects: SelectorT[],
config: NodeResetConfig = {},
): SelectedSelectorT[] =>
selects.map((select) => resetSelected(select, config));

0 comments on commit 9fc89bb

Please sign in to comment.