Skip to content

Commit

Permalink
Merge pull request #137 from Tauffer-Consulting/fix/upstream-options-…
Browse files Browse the repository at this point in the history
…complex-array

Fix/upstream options complex array
  • Loading branch information
vinicvaz authored Nov 1, 2023
2 parents 0bf99f4 + 9d35edc commit 2190fe4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docker-compose-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ services:
- DOMINO_DB_HOST=domino_postgres
- DOMINO_DB_PORT=5432
- DOMINO_DB_NAME=postgres
- DOMINO_DEFAULT_PIECES_REPOSITORY_VERSION=0.4.3
- DOMINO_DEFAULT_PIECES_REPOSITORY_VERSION=0.4.5
- DOMINO_DEFAULT_PIECES_REPOSITORY_TOKEN=${DOMINO_DEFAULT_PIECES_REPOSITORY_TOKEN}
- DOMINO_GITHUB_ACCESS_TOKEN_WORKFLOWS=${DOMINO_GITHUB_ACCESS_TOKEN_WORKFLOWS}
- DOMINO_GITHUB_WORKFLOWS_REPOSITORY=${DOMINO_GITHUB_WORKFLOWS_REPOSITORY}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import {
useWatch,
} from "react-hook-form";

import { type ArrayOption } from "../upstreamOptions";
import {
type ComplexArrayOption,
type ArrayOption,
type Option,
} from "../upstreamOptions";

import { disableCheckboxOptions } from "./disableCheckboxOptions";
import ObjectInputComponent from "./objectInput";
Expand All @@ -32,7 +36,7 @@ interface ArrayInputItemProps {
schema: any;
control: Control<IWorkflowPieceData, any>;
definitions?: any;
upstreamOptions: ArrayOption;
upstreamOptions: ArrayOption | ComplexArrayOption;
}

const ArrayInput: React.FC<ArrayInputItemProps> = ({
Expand Down Expand Up @@ -206,7 +210,7 @@ const ArrayInput: React.FC<ArrayInputItemProps> = ({
<SelectUpstreamInput
name={`${name}.${index}`}
label={schema?.title}
options={upstreamOptions.items}
options={upstreamOptions.items as Option[]}
/>
</Grid>
)}
Expand Down Expand Up @@ -313,7 +317,7 @@ const ArrayInput: React.FC<ArrayInputItemProps> = ({
name={`${name}.${index}`}
schema={schema}
definitions={definitions}
upstreamOptions={upstreamOptions.items}
upstreamOptions={upstreamOptions as ComplexArrayOption}
/>
</Grid>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, { useCallback, useMemo, useState } from "react";
import { useWatch } from "react-hook-form";
import { getDefinition } from "utils";

import { type Option } from "../upstreamOptions";
import { type ComplexArrayOption } from "../upstreamOptions";

import { disableCheckboxOptions } from "./disableCheckboxOptions";
import SelectUpstreamInput from "./selectUpstreamInput";
Expand All @@ -15,7 +15,7 @@ interface Prop {
name: `inputs.${string}.value.${number}`;
schema: ArrayObjectProperty;
definitions: Definitions;
upstreamOptions: Option[];
upstreamOptions: ComplexArrayOption;
}

const ObjectInputComponent: React.FC<Prop> = ({
Expand Down Expand Up @@ -92,7 +92,11 @@ const ObjectInputComponent: React.FC<Prop> = ({
<SelectUpstreamInput
label={key}
name={`${name}.upstreamValue.${key}`}
options={upstreamOptions}
options={
upstreamOptions[key] !== undefined
? upstreamOptions[key].items
: []
}
object
/>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ export interface ArrayOption {
items: Option[];
}

export type ComplexArrayOption = Record<string, ArrayOption>;

export type UpstreamOptions = Record<string, Option[] | ArrayOption>;

const getInputType = (schema: Record<string, any>) => {
let type = schema.format ? schema.format : schema.type;
if ("allOf" in schema || "oneOf" in schema || "anyOf" in schema) {
if ("allOf" in schema || "oneOf" in schema) {
type = "enum";
} else if ("anyOf" in schema) {
type = [];
for (const item of schema.anyOf) {
type.push(item.type);
}
}
return type === "number" ? "float" : (type as string);
};
Expand All @@ -36,7 +43,11 @@ const getOptions = (
for (const property in upSchema) {
const upType = getInputType(upSchema[property]);

if (upType === type || (upType === "string" && type === "object")) {
if (
upType === type ||
(upType === "string" && type === "object") ||
(Array.isArray(type) && type.includes(upType))
) {
const value = `${upPiece?.name} (${getUuidSlice(upPiece.id)}) - ${
upSchema[property].title
}`;
Expand Down Expand Up @@ -88,16 +99,24 @@ export const getUpstreamOptions = (
itemsSchema = schema.definitions?.[subItemSchemaName];
}

const itemsType = getInputType(itemsSchema);

const array = getOptions(upstreamPieces, currentType);
const items = getOptions(upstreamPieces, itemsType);

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
upstreamOptions[key] = { array, items } as ArrayOption;
if (itemsSchema.type === "object") {
const __data: any = {};
Object.keys(itemsSchema.properties).forEach((subKey) => {
const subSchema = itemsSchema.properties[subKey];
const subType = getInputType(subSchema);
const items = getOptions(upstreamPieces, subType);
__data[subKey] = { array, items };
});
upstreamOptions[key] = __data;
} else {
const itemsType = getInputType(itemsSchema);
const items = getOptions(upstreamPieces, itemsType);
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
upstreamOptions[key] = { array, items } as ArrayOption;
}
} else {
const options = getOptions(upstreamPieces, currentType);

upstreamOptions[key] = options;
}
});
Expand Down

0 comments on commit 2190fe4

Please sign in to comment.