Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: events workflow in wizard #969

Merged
merged 8 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions frontend/src/components/Editor/EditorComponents/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { request, useInitialPayload } from 'near-social-bridge';

Check warning on line 1 in frontend/src/components/Editor/EditorComponents/Editor.tsx

View workflow job for this annotation

GitHub Actions / lint

Run autofix to sort these imports!
import type { ReactElement } from 'react';
import type { Method, Event } from '@/pages/api/generateCode';

Expand Down Expand Up @@ -31,7 +31,7 @@

declare const monaco: any;
const INDEXER_TAB_NAME = 'indexer.js';
const SCHEMA_TAB_NAME = 'schema.sql';

Check warning on line 34 in frontend/src/components/Editor/EditorComponents/Editor.tsx

View workflow job for this annotation

GitHub Actions / lint

'SCHEMA_TAB_NAME' is assigned a value but never used. Allowed unused vars must match /^_/u
const originalSQLCode = formatSQL(defaultSchema);
const originalIndexingCode = formatIndexingCode(defaultCode);
const pgSchemaTypeGen = new PgSchemaTypeGen();
Expand Down Expand Up @@ -71,7 +71,7 @@
const [heights, setHeights] = useState<number[]>(initialHeights);

const [diffView, setDiffView] = useState<boolean>(false);
const [blockView, setBlockView] = useState<boolean>(false);

Check warning on line 74 in frontend/src/components/Editor/EditorComponents/Editor.tsx

View workflow job for this annotation

GitHub Actions / lint

'setBlockView' is assigned a value but never used. Allowed unused vars must match /^_/u
const { showModal } = useModal();

const [isExecutingIndexerFunction, setIsExecutingIndexerFunction] = useState<boolean>(false);
Expand Down Expand Up @@ -144,13 +144,13 @@
const fetchData = async () => {
try {
const response = await fetchWizardData('');
const { wizardContractFilter, wizardMethods } = response;
const { wizardContractFilter, wizardMethods, wizardEvents } = response;

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

const codeResponse = await generateCode(wizardContractFilter, wizardMethods);
const codeResponse = await generateCode(wizardContractFilter, wizardMethods, wizardEvents);
setIndexingCode(codeResponse.jsCode);
setSchema(codeResponse.sqlCode);
} catch (error: unknown) {
Expand All @@ -177,7 +177,7 @@
monacoEditorRef.current.setPosition(savedCursorPosition || { lineNumber: 1, column: 1 });
monacoEditorRef.current.focus();
}
}, [indexerDetails.code]);

Check warning on line 180 in frontend/src/components/Editor/EditorComponents/Editor.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has missing dependencies: 'fileName' and 'storageManager'. Either include them or remove the dependency array

useEffect(() => {
//* Load saved schema from local storage if it exists else load code from context
Expand All @@ -188,7 +188,7 @@
schemaErrorHandler(schemaError);
formattedSchema && setSchema(formattedSchema);
}
}, [indexerDetails.schema]);

Check warning on line 191 in frontend/src/components/Editor/EditorComponents/Editor.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'storageManager'. Either include it or remove the dependency array

useEffect(() => {
const { error: schemaError } = validateSQLSchema(schema);
Expand All @@ -201,11 +201,11 @@
}

handleCodeGen();
}, [fileName]);

Check warning on line 204 in frontend/src/components/Editor/EditorComponents/Editor.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has missing dependencies: 'handleCodeGen', 'indexingCode', and 'schema'. Either include them or remove the dependency array

useEffect(() => {
cacheToLocal();
}, [indexingCode, schema]);

Check warning on line 208 in frontend/src/components/Editor/EditorComponents/Editor.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'cacheToLocal'. Either include it or remove the dependency array

useEffect(() => {
if (!monacoEditorRef.current) return;
Expand All @@ -216,16 +216,16 @@
return () => {
editorInstance.dispose();
};
}, [monacoEditorRef.current]);

Check warning on line 219 in frontend/src/components/Editor/EditorComponents/Editor.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'handleCursorChange'. Either include it or remove the dependency array. Mutable values like 'monacoEditorRef.current' aren't valid dependencies because mutating them doesn't re-render the component

useEffect(() => {
storageManager?.setSchemaTypes(schemaTypes);
handleCodeGen();
}, [schemaTypes, monacoMount]);

Check warning on line 224 in frontend/src/components/Editor/EditorComponents/Editor.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has missing dependencies: 'handleCodeGen' and 'storageManager'. Either include them or remove the dependency array

useEffect(() => {
storageManager?.setDebugList(heights);
}, [heights]);

Check warning on line 228 in frontend/src/components/Editor/EditorComponents/Editor.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'storageManager'. Either include it or remove the dependency array

const cacheToLocal = () => {
if (!storageManager || !monacoEditorRef.current) return;
Expand Down
164 changes: 0 additions & 164 deletions frontend/src/components/Editor/EditorComponents/GenerateCode.tsx

This file was deleted.

6 changes: 2 additions & 4 deletions frontend/src/pages/api/generateCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export const validateRequestBody = (body: any): body is RequestBody => {
isStringOrArray(body.contractFilter) &&
Array.isArray(body.selectedMethods) &&
body.selectedMethods.every(isValidMethod)
// && Array.isArray(body.selectedEvents) &&
// body.selectedEvents.every(isValidEvent)
&& Array.isArray(body.selectedEvents) &&
body.selectedEvents.every(isValidEvent)
);
};

Expand All @@ -59,7 +59,6 @@ export default function handler(req: NextApiRequest, res: NextApiResponse): void
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');

if (req.method === 'OPTIONS') {
res.status(200).end();
return;
Expand All @@ -78,7 +77,6 @@ export default function handler(req: NextApiRequest, res: NextApiResponse): void
}

const { contractFilter, selectedMethods, selectedEvents } = req.body;

const filterString = Array.isArray(contractFilter) ? contractFilter.join(', ') : contractFilter;

const generator = new WizardCodeGenerator(filterString, selectedMethods, selectedEvents);
Expand Down
5 changes: 0 additions & 5 deletions frontend/src/pages/query-api-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { useContext, useEffect } from 'react';
import { Alert } from 'react-bootstrap';

import Editor from '@/components/Editor/EditorComponents/Editor';
import GenerateCode from '@/components/Editor/EditorComponents/GenerateCode';
import IndexerLogsContainer from '@/components/Logs/LogsViewContainer/IndexerLogsContainer';
import { IndexerDetailsContext } from '@/contexts/IndexerDetailsContext';

Expand All @@ -25,10 +24,6 @@ const QueryApiEditorPage = ({ router }) => {
);
}

if (accountId == 'test' && indexerName == 'test') {
return <GenerateCode />;
}

return showLogsView ? <IndexerLogsContainer /> : <Editor />;
};

Expand Down
55 changes: 53 additions & 2 deletions frontend/src/test/api/generateCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const createRequestResponseMocks = (
};
};

describe('generateCode API', () => {
it('should return generated JS and SQL code for valid input', async () => {
describe('generateCode API ', () => {
it('should return generated JS and SQL code for valid input methods', async () => {
const { req, res } = createRequestResponseMocks('POST', {
contractFilter: 'filter',
selectedMethods: [
Expand Down Expand Up @@ -65,6 +65,56 @@ describe('generateCode API', () => {
},
},
],
selectedEvents: [],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there really no other testing that can be performed? It feels odd to see that the only modification to tests needed is adding one line, when enabling some feature.

});

handler(req, res);

expect(res._getStatusCode()).toBe(200);
const responseData = JSON.parse(res._getData());
console.log(responseData);
});

it('should return generated JS and SQL code for valid input events', async () => {
const { req, res } = createRequestResponseMocks('POST', {
contractFilter: 'filter',
selectedMethods: [],
selectedEvents: [{
event_name: 'register',
schema: {
type: 'object',
properties: {
function_name: {
type: 'string',
},
code: {
type: 'string',
},
schema: {
type: 'string',
},
start_block_height: {
type: 'integer',
},
filter_json: {
type: 'string',
},
},
required: ['function_name', 'code', 'schema', 'start_block_height', 'filter_json'],
},
},
{
event_name: 'remove_indexer_function',
schema: {
type: 'object',
properties: {
function_name: {
type: 'string',
},
},
required: ['function_name'],
},
},],
});

handler(req, res);
Expand All @@ -73,6 +123,7 @@ describe('generateCode API', () => {
const responseData = JSON.parse(res._getData());
console.log(responseData);
});

it('should handle empty arrays correctly because I mean maybe they just want something to do with contractName?', async () => {
const { req, res } = createRequestResponseMocks('POST', {
contractFilter: 'filter',
Expand Down
3 changes: 3 additions & 0 deletions frontend/widgets/src/QueryApi.Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const [activeTab, setActiveTab] = useState(props.view === "create-new-indexer" ?
const [activeIndexerTabView, setActiveIndexerTabView] = useState(props.activeIndexerView ?? "editor");
const [selectedIndexer, setSelectedIndexer] = useState(props.selectedIndexerPath);

const [wizardEvents, setWizardEvents] = useState({});
const [wizardMethods, setWizardMethods] = useState({});
const [wizardContractFilter, setWizardContractFilter] = useState('');

Expand Down Expand Up @@ -109,6 +110,7 @@ return (
setSelectedIndexer: setSelectedIndexer,
setWizardContractFilter: setWizardContractFilter,
setWizardMethods: setWizardMethods,
setWizardEvents: setWizardEvents,
}}
/>
</Section>
Expand All @@ -131,6 +133,7 @@ return (
path: "create-new-indexer",
wizardContractFilter: wizardContractFilter,
wizardMethods: wizardMethods,
wizardEvents: wizardEvents,
}}
/>
</Section>
Expand Down
5 changes: 3 additions & 2 deletions frontend/widgets/src/QueryApi.Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const activeView = props.activeView || "editor";
let accountId = props.accountId || context.accountId;
let externalAppUrl = `${REPL_EXTERNAL_APP_URL}/${path}?accountId=${accountId}`;

const { wizardContractFilter, wizardMethods } = props;
const { wizardContractFilter, wizardMethods, wizardEvents } = props;

if (props.indexerName) {
externalAppUrl += `&indexerName=${props.indexerName}`;
Expand Down Expand Up @@ -60,7 +60,8 @@ let deleteIndexer = (request) => {
const getLaunchpadCode = (request, response) => {
const wizardContractFilter = wizardContractFilter ?? 'noFilter';
const wizardMethods = wizardMethods;
response(request).send({ wizardContractFilter, wizardMethods });
const wizardEvents = wizardEvents;
response(request).send({ wizardContractFilter, wizardMethods, wizardEvents });
}

/**
Expand Down
Loading
Loading