Skip to content

Commit

Permalink
type generation for launchpad code (#1001)
Browse files Browse the repository at this point in the history
types are now generated for launchpad code
  • Loading branch information
Kevin101Zhang authored Aug 12, 2024
1 parent ab5a836 commit e759080
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
54 changes: 26 additions & 28 deletions frontend/src/components/Editor/EditorComponents/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ const fetchWizardData = (req: string): Promise<WizardResponse> => {
return request<WizardResponse>('launchpad-create-indexer', req);
};

const fetchGeneratedCode = async (contractFilter: string, selectedMethods: Method[], selectedEvents?: Event[]) => {
try {
const response = await fetch('/api/generateCode', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ contractFilter, selectedMethods, selectedEvents }),
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();

if (!data.hasOwnProperty('jsCode') || !data.hasOwnProperty('sqlCode')) {
throw new Error('No code was returned from the server with properties jsCode and sqlCode');
}

return data;
} catch (error) {
throw error;
}
};

const Editor: React.FC = (): ReactElement => {
const { indexerDetails, isCreateNewIndexer } = useContext(IndexerDetailsContext);

Expand Down Expand Up @@ -131,38 +155,13 @@ const Editor: React.FC = (): ReactElement => {
return;
};

const generateCode = async (contractFilter: string, selectedMethods: Method[], selectedEvents?: Event[]) => {
try {
const response = await fetch('/api/generateCode', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ contractFilter, selectedMethods, selectedEvents }),
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();

if (!data.hasOwnProperty('jsCode') || !data.hasOwnProperty('sqlCode')) {
throw new Error('No code was returned from the server with properties jsCode and sqlCode');
}

return data;
} catch (error) {
throw error;
}
};

useEffect(() => {
(async () => {
try {
const { wizardContractFilter, wizardMethods, wizardEvents } = await fetchWizardData('');

if (wizardContractFilter === 'noFilter') return;

const { jsCode, sqlCode } = await generateCode(wizardContractFilter, wizardMethods, wizardEvents);
const { jsCode, sqlCode } = await fetchGeneratedCode(wizardContractFilter, wizardMethods, wizardEvents);
const wrappedIndexingCode = wrapCode(jsCode) ? wrapCode(jsCode) : jsCode;
const { validatedCode, validatedSchema } = reformatAll(wrappedIndexingCode, sqlCode);

Expand Down Expand Up @@ -207,15 +206,14 @@ const Editor: React.FC = (): ReactElement => {
useEffect(() => {
const { error: schemaError } = validateSQLSchema(schema);
const { error: codeError } = validateJSCode(indexingCode);

if (schemaError || codeError) {
if (schemaError) schemaErrorHandler(schemaError);
if (codeError) indexerErrorHandler(codeError);
return;
}

handleCodeGen();
}, [fileName]);
}, [fileName, launchPadDefaultSchema]);

useEffect(() => {
cacheToLocal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ export default function ResizableLayoutEditor({
sizeThresholdFirst: 60,
sizeThresholdSecond: 20,
});

const defaultCode = launchPadDefaultCode ? launchPadDefaultCode : contextCode ? contextCode : originalIndexingCode;
const defaultSchema = launchPadDefaultSchema ? launchPadDefaultSchema : contextSchema ? contextSchema : originalSQLCode;

return (
<div style={{ display: 'flex', flexDirection: 'column', height: '100%' }}>
{/* Code Editor */}
Expand Down

0 comments on commit e759080

Please sign in to comment.