Skip to content

Commit

Permalink
feat(docs): act-1397 - synced params with request section
Browse files Browse the repository at this point in the history
  • Loading branch information
TrofimovAnton85 committed Jun 21, 2024
1 parent 48581af commit 1c3ac0d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
13 changes: 11 additions & 2 deletions src/components/ParserOpenRPC/InteractiveBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ interface InteractiveBoxProps {
params: MethodParam[];
components: SchemaComponents;
examples: MethodExample[];
onParamChange: (data) => void;
}

export default function InteractiveBox({ params, components, examples }: InteractiveBoxProps) {
export default function InteractiveBox({ params, components, examples, onParamChange }: InteractiveBoxProps) {
const [parsedSchema, setParsedSchema] = useState<RJSFSchema>(null);
const [defaultFormData, setDefaultFormData] = useState<any>({});
const [isFormReseted, setIsFormReseted] = useState(false);
Expand Down Expand Up @@ -68,9 +69,14 @@ export default function InteractiveBox({ params, components, examples }: Interac
dereferenceSchema();
if (examples) {
setDefaultFormData(defaultExampleFormData);
onParamChange(defaultExampleFormData);
}
}, []);

const onChangeHandler = (data) => {
onParamChange(data);
};

return parsedSchema ? (
<>
<div className={styles.tableHeadingRow}>
Expand All @@ -88,7 +94,10 @@ export default function InteractiveBox({ params, components, examples }: Interac
validator={validator}
liveValidate
noHtml5Validate
onChange={log("changed")}
onChange={(data) => {
onChangeHandler(data.formData);
setDefaultFormData(data.formData);
}}
onSubmit={() => {log("submitted");}}
onError={log("errors")}
templates={{
Expand Down
2 changes: 1 addition & 1 deletion src/components/ParserOpenRPC/ModalDrawer/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@

.modalContent {
height: 394px;
overflow-y: scroll;
overflow-y: auto;
}
8 changes: 5 additions & 3 deletions src/components/ParserOpenRPC/RequestBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ interface RequestBoxProps {
method: string;
params: MethodParam[];
response?: any;
paramsData: any;
openModal: () => void;
submitRequest: () => void;
}

export default function RequestBox({ isMetamaskInstalled, method, params, response, openModal, submitRequest }: RequestBoxProps) {
export default function RequestBox({ isMetamaskInstalled, method, params, response, paramsData, openModal, submitRequest }: RequestBoxProps) {
const exampleRequest = useMemo(() => {
return `await window.ethereum.request({\n "method": "${method}",\n "params": [${params}],\n});`;
}, [method, params]);
const preparedParams = JSON.stringify(paramsData, null, 2);
return `await window.ethereum.request({\n "method": "${method}",\n "params": ${preparedParams},\n});`;
}, [method, paramsData]);

const exampleResponse = useMemo(() => {
if (!response || response === null) return false
Expand Down
14 changes: 12 additions & 2 deletions src/components/ParserOpenRPC/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default function ParserOpenRPC({ network, method }: ParserProps) {
if (!method || !network) return null;
const [metamaskInstalled, setMetamaskInstalled] = useState(false);
const [isModalOpen, setModalOpen] = useState(false);
const [reqResult, setReqResult] = useState(null)
const [reqResult, setReqResult] = useState(null);
const [paramsData, setParamsData] = useState([]);
const openModal = () => setModalOpen(true);
const closeModal = () => setModalOpen(false);

Expand Down Expand Up @@ -63,11 +64,18 @@ export default function ParserOpenRPC({ network, method }: ParserProps) {
setMetamaskInstalled(installed);
}, []);

const onParamsChangeHandle = (data) => {
if (typeof data !== 'object' || data === null || Object.keys(data).length === 0) {
setParamsData([]);
}
setParamsData(Object.values(data));
}

const onSubmitRequestHandle = async () => {
try {
const response = await (window as any).ethereum.request({
method: method,
params: []
params: paramsData
})
setReqResult(response);
} catch (e) {
Expand Down Expand Up @@ -96,6 +104,7 @@ export default function ParserOpenRPC({ network, method }: ParserProps) {
params={currentMethodData.params}
components={currentMethodData.components.schemas}
examples={currentMethodData.examples}
onParamChange={onParamsChangeHandle}
/>
</ModalDrawer>
</div>
Expand All @@ -106,6 +115,7 @@ export default function ParserOpenRPC({ network, method }: ParserProps) {
isMetamaskInstalled={metamaskInstalled}
method={method}
params={currentMethodData.params}
paramsData={paramsData}
response={reqResult}
openModal={openModal}
submitRequest={onSubmitRequestHandle}
Expand Down
2 changes: 1 addition & 1 deletion wallet/reference/new-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ sidebar_class_name: "hidden"
import ParserOpenRPC from "@site/src/components/ParserOpenRPC";
import { NETWORK_NAMES } from "@site/src/plugins/plugin-json-rpc";

<ParserOpenRPC network={NETWORK_NAMES.metamask} method="eth_getBlockByHash" />
<ParserOpenRPC network={NETWORK_NAMES.metamask} method="wallet_switchEthereumChain" />

0 comments on commit 1c3ac0d

Please sign in to comment.