import { useState } from "react"; import { ApiRoute } from "@/utils/type"; import axios from "@/utils/axios"; import { Endpoint } from "./endpoint"; import { Request } from "./request"; import { Response } from "./response"; import { useRequest } from "./hooks/useRequest"; import { useMount, useUpdateEffect } from "react-use"; export const EditorMain = ({ collections, endpoint, onCollections, onEndpoint, }: { collections: string[]; endpoint: ApiRoute; onCollections: (collections: string[]) => void; onEndpoint: (endpoint: ApiRoute) => void; }) => { const [formattedEndpoint, setFormattedEndpoint] = useState( endpoint.path ); const [formattedParameters, setFormattedParameters] = useState( endpoint?.parameters ? { ...endpoint.parameters } : undefined ); const { loading, submit, data } = useRequest( formattedEndpoint, formattedParameters ); useMount(() => { if ( endpoint?.path && endpoint?.method === "GET" && !endpoint?.path?.includes("{") && !endpoint?.path?.includes("}") ) { submit(); } }); useUpdateEffect(() => { if (endpoint?.path) { setFormattedEndpoint(endpoint.path); } }, [endpoint.path]); useUpdateEffect(() => { if (endpoint?.parameters) { setFormattedParameters(endpoint.parameters); } }, [endpoint.parameters]); return (
{ setFormattedParameters({ ...formattedParameters, [k]: v, }); }} >
); };