forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderOneOf.tsx
More file actions
150 lines (132 loc) · 3.79 KB
/
Copy pathRenderOneOf.tsx
File metadata and controls
150 lines (132 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { JSONSchema7 } from "json-schema";
import { get } from "lodash";
import { Select } from "@stellar/design-system";
import { Box } from "../../components/layout/Box";
import { jsonSchema } from "../util/jsonSchema";
import {
AnyObject,
JsonSchemaFormProps,
SorobanInvokeValue,
} from "../types/types";
import { renderTupleType } from "./RenderTupleType";
// oneOf is used for union and enum types
// https://github.com/stellar/js-stellar-sdk/blob/master/src/contract/spec.ts
export const renderOneOf = ({
name,
schema,
path,
parsedSorobanOperation,
renderer,
onChange,
formError,
setFormError,
}: {
name: string;
schema: JSONSchema7;
path: string[];
parsedSorobanOperation: SorobanInvokeValue;
renderer: (props: JsonSchemaFormProps) => React.ReactNode;
onChange: (value: SorobanInvokeValue) => void;
formError: AnyObject;
setFormError: (error: AnyObject) => void;
}) => {
if (!schema?.oneOf) {
return null;
}
const invokeContractBaseProps = {
contract_id: parsedSorobanOperation.contract_id,
function_name: parsedSorobanOperation.function_name,
};
const keyName = Object.keys(parsedSorobanOperation.args)[0];
let tagName;
if (path.length > 1) {
tagName = get(parsedSorobanOperation.args[keyName], path.join("."))?.tag;
} else {
tagName = (parsedSorobanOperation.args[keyName] as AnyObject)?.tag;
}
const selectedSchema = schema.oneOf.find(
(oneOf) =>
jsonSchema.isSchemaObject(oneOf as AnyObject) &&
(oneOf as AnyObject)?.title === tagName,
);
const onSelectChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
const pathSegments = path[0].split(".");
if (pathSegments.length > 1) {
const keyName = Object.keys(parsedSorobanOperation.args)[0];
const updatedPath =
keyName === pathSegments[0]
? pathSegments.slice(1).join(".")
: pathSegments.join(".");
const updatedTupleList = jsonSchema.setDeepValue(
parsedSorobanOperation.args[keyName] as AnyObject,
updatedPath,
{
tag: e.target.value,
// values: [],
},
);
onChange({
...invokeContractBaseProps,
args: {
...parsedSorobanOperation.args,
[keyName]: updatedTupleList,
},
});
} else {
onChange({
...invokeContractBaseProps,
args: {
...parsedSorobanOperation.args,
[name]: {
tag: e.target.value,
// values: [],
},
},
});
}
};
return (
<Box gap="md" key={`${path.join(".")}`}>
<Select
id="json-schema-form-renderer-one-of"
fieldSize="md"
label={name}
value={
(get(parsedSorobanOperation.args, path.join(".")) as AnyObject)
?.tag as string
}
onChange={onSelectChange}
>
{/* title is the tag */}
<option value="">Select</option>
{schema.oneOf.map((oneOf, index) => {
if (typeof oneOf === "boolean") return null;
return (
<option
id={oneOf?.title}
value={oneOf?.title}
key={`${oneOf?.title}-${index}`}
>
{oneOf?.title}
</option>
);
})}
</Select>
{selectedSchema &&
jsonSchema.isSchemaObject(selectedSchema as AnyObject) &&
jsonSchema.isTaggedUnion(selectedSchema as JSONSchema7)
? renderTupleType({
path,
schema: selectedSchema as JSONSchema7,
onChange,
parsedSorobanOperation,
renderer,
formError,
setFormError,
})
: null}
</Box>
);
};