Unverified Commit 989a615f authored by davelopez's avatar davelopez
Browse files

Refactor handleIncoming function to use toDataOption

For reusability
parent 19c448ad
Loading
Loading
Loading
Loading
+47 −36
Original line number Diff line number Diff line
@@ -377,45 +377,19 @@ function handleIncoming(incoming: Record<string, unknown> | Record<string, unkno
            const incomingValues: Array<DataOption> = [];
            values.forEach((currVal) => {
                // Map incoming objects to data option values
                const { newSrc, datasetCollectionDataset } = getSrcAndContentType(currVal);
                let v: HistoryOrCollectionItem | HDAObject;
                if (datasetCollectionDataset) {
                    v = datasetCollectionDataset;
                } else {
                    v = currVal;
                }
                const newHid = isHistoryItem(v) ? v.hid : undefined;
                const newId = v.id;
                const newName = isHistoryItem(v) && v.name ? v.name : newId;
                const newValue: DataOption = {
                    id: newId,
                    src: newSrc,
                    batch: false,
                    map_over_type: undefined,
                    hid: newHid,
                    name: newName,
                    keep: true,
                    tags: [],
                };
                if (isHistoryItem(v) && isHDCA(v) && props.collectionTypes?.length > 0) {
                    const itemCollectionType = v.collection_type;
                    if (!props.collectionTypes.includes(itemCollectionType)) {
                        const mapOverType = props.collectionTypes.find((collectionType) =>
                            itemCollectionType.endsWith(collectionType)
                        );
                        if (!mapOverType) {
                const newValue = toDataOption(currVal);
                if (!newValue) {
                    return false;
                }
                        newValue["batch"] = true;
                        newValue["map_over_type"] = mapOverType;
                    }
                }
                // Verify that new value has corresponding option
                const keepKey = `${newId}_${newSrc}`;
                const existingOptions = props.options && props.options[newSrc];
                const foundOption = existingOptions && existingOptions.find((option) => option.id === newId);
                const keepKey = `${newValue.id}_${newValue.src}`;
                const existingOptions = props.options && props.options[newValue.src];
                const foundOption = existingOptions && existingOptions.find((option) => option.id === newValue.id);
                if (!foundOption && !(keepKey in keepOptions)) {
                    keepOptions[keepKey] = { label: `${newHid || "Selected"}: ${newName}`, value: newValue };
                    keepOptions[keepKey] = {
                        label: `${newValue.hid || "Selected"}: ${newValue.name}`,
                        value: newValue,
                    };
                }
                // Add new value to list
                incomingValues.push(newValue);
@@ -446,6 +420,43 @@ function handleIncoming(incoming: Record<string, unknown> | Record<string, unkno
    return true;
}

function toDataOption(item: HistoryOrCollectionItem): DataOption | null {
    const { newSrc, datasetCollectionDataset } = getSrcAndContentType(item);
    let v: HistoryOrCollectionItem | HDAObject;
    if (datasetCollectionDataset) {
        v = datasetCollectionDataset;
    } else {
        v = item;
    }
    const newHid = isHistoryItem(v) ? v.hid : undefined;
    const newId = v.id;
    const newName = isHistoryItem(v) && v.name ? v.name : newId;
    const newValue: DataOption = {
        id: newId,
        src: newSrc,
        batch: false,
        map_over_type: undefined,
        hid: newHid,
        name: newName,
        keep: true,
        tags: [],
    };
    if (isHistoryItem(v) && isHDCA(v) && props.collectionTypes?.length > 0) {
        const itemCollectionType = v.collection_type;
        if (!props.collectionTypes.includes(itemCollectionType)) {
            const mapOverType = props.collectionTypes.find((collectionType) =>
                itemCollectionType.endsWith(collectionType)
            );
            if (!mapOverType) {
                return null;
            }
            newValue["batch"] = true;
            newValue["map_over_type"] = mapOverType;
        }
    }
    return newValue;
}

/**
 * Open file dialog
 */