Skip to content

Commit

Permalink
fix(reference): act-1487 - fixes for new reference pages (#1459)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrofimovAnton85 authored Jul 29, 2024
1 parent 89fb15a commit cd2c05e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
32 changes: 17 additions & 15 deletions src/components/ParserOpenRPC/InteractiveBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,21 @@ export default function InteractiveBox({
}: InteractiveBoxProps) {
const [parsedSchema, setParsedSchema] = useState<RJSFSchema>(null);
const [defaultFormData, setDefaultFormData] = useState<any>({});
const [currentFormData, setCurrentFormData] = useState<any>({});
const [isFormReseted, setIsFormReseted] = useState(false);
const formRef = useRef(null);
const { colorMode } = useColorMode();
const { isComplexTypeView } = useContext(ParserOpenRPCContext);

const defaultExampleFormData = examples
? Object.fromEntries(
examples[0].params.map(({ name, value }) => [name, value])
)
: {};
useEffect(() => {
if (examples && examples.length > 0 && examples[0].params) {
const defaultValues = Object.fromEntries(examples[0].params.map(({ name, value }) => [name, value]));
setDefaultFormData({...defaultValues});
setCurrentFormData({...defaultValues});
onParamChange({...defaultValues});
}
}, [examples]);

const schema: RJSFSchema = {
components: {
schemas: components,
Expand Down Expand Up @@ -85,7 +90,8 @@ export default function InteractiveBox({
};
const handleResetForm = (e) => {
e.preventDefault();
setDefaultFormData(defaultExampleFormData);
setCurrentFormData({...defaultFormData});
onParamChange({...defaultFormData});
setIsFormReseted(true);
};
const handleClearForm = (e) => {
Expand All @@ -107,15 +113,11 @@ export default function InteractiveBox({
}
};
dereferenceSchema();
if (examples) {
setDefaultFormData(defaultExampleFormData);
onParamChange(defaultExampleFormData);
}
}, []);

const onChangeHandler = (data) => {
onParamChange(data);
setDefaultFormData(data);
setCurrentFormData({...data});
onParamChange({...data});
};

const cloneAndSetNullIfExists = (obj, key) => {
Expand All @@ -137,8 +139,8 @@ export default function InteractiveBox({

const handleCancelClick = () => {
if (drawerLabel) {
const upData = cloneAndSetNullIfExists(defaultFormData, drawerLabel);
setDefaultFormData(upData);
const upData = cloneAndSetNullIfExists(currentFormData, drawerLabel);
setCurrentFormData(upData);
}
closeComplexTypeView();
};
Expand All @@ -151,7 +153,7 @@ export default function InteractiveBox({
</div>
<Form
schema={parsedSchema}
formData={defaultFormData}
formData={currentFormData}
formContext={{ isFormReseted }}
validator={validator}
liveValidate
Expand Down
3 changes: 1 addition & 2 deletions src/components/ParserOpenRPC/RequestBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ export default function RequestBox({
}: RequestBoxProps) {
const exampleRequest = useMemo(() => {
const preparedParams = JSON.stringify(paramsData, null, 2);
return `await window.ethereum.request({\n "method": "${method}",\n "params": ${preparedParams},\n});`;
return `await window.ethereum.request({\n "method": "${method}",\n "params": ${preparedParams.replace(/"([^"]+)":/g, '$1:')},\n});`;
}, [method, paramsData]);

const exampleResponse = useMemo(() => {
if (!response || response === null) return false;
return JSON.stringify(response, null, 2);
}, [response]);

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/plugin-json-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const requests = [
name: NETWORK_NAMES.linea,
},
{
url: "https://metamask.github.io/api-specs/0.9.3/openrpc.json",
url: "https://metamask.github.io/api-specs/latest/openrpc.json",
name: NETWORK_NAMES.metamask,
},
];
Expand Down

0 comments on commit cd2c05e

Please sign in to comment.