Skip to content

Commit

Permalink
feat: allow to edit indexer when forking
Browse files Browse the repository at this point in the history
  • Loading branch information
roshaans committed Jul 14, 2023
1 parent 3ab0a84 commit 75c35f9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 49 deletions.
32 changes: 19 additions & 13 deletions frontend/src/components/Editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const Editor = ({
debugMode,
isCreateNewIndexer,
indexerNameField,
setAccountId,
} = useContext(IndexerDetailsContext);

const DEBUG_LIST_STORAGE_KEY = `QueryAPI:debugList:${indexerDetails.accountId}#${indexerDetails.indexerName}`
Expand Down Expand Up @@ -127,10 +128,23 @@ const Editor = ({
}
};


const forkIndexer = async(indexerName) => {
let code = indexingCode;

setAccountId(currentUserAccountId)

let prevAccountId = indexerDetails.accountId.replaceAll(".", "_");
let newAccountId = currentUserAccountId.replaceAll(".", "_");
let prevIndexerName = indexerDetails.indexerName.replaceAll("-", "_").trim().toLowerCase();
let newIndexerName = indexerName.replaceAll("-", "_").trim().toLowerCase();
code = code.replaceAll(prevAccountId, newAccountId);
code = code.replaceAll(prevIndexerName, newIndexerName);
setIndexingCode(formatIndexingCode(code))
}

const registerFunction = async (indexerName, indexerConfig) => {
let formatted_schema = checkSQLSchemaFormatting();
let isForking = indexerDetails.accountId !== currentUserAccountId;

let innerCode = indexingCode.match(/getBlock\s*\([^)]*\)\s*{([\s\S]*)}/)[1];
indexerName = indexerName.replaceAll(" ", "_");
if (formatted_schema == undefined) {
Expand All @@ -140,14 +154,6 @@ const Editor = ({
);
return;
}

if (isForking) {
let prevAccountName = indexerDetails.accountId.replace(".", "_");
let newAccountName = currentUserAccountId.replace(".", "_");

innerCode = innerCode.replaceAll(prevAccountName, newAccountName);
}

setError(() => undefined);

request("register-function", {
Expand All @@ -170,8 +176,8 @@ const Editor = ({
const handleReload = async () => {
if (isCreateNewIndexer) {
setShowResetCodeModel(false);
setIndexingCode(defaultCode);
setSchema(defaultSchema);
setIndexingCode((formatIndexingCode(indexerDetails.code)));
setSchema(formatSQL(indexerDetails.schema))
return;
}

Expand Down Expand Up @@ -331,7 +337,7 @@ const Editor = ({
blockHeightError={blockHeightError}
/>
<ForkIndexerModal
registerFunction={registerFunction}
forkIndexer={forkIndexer}
/>

<div
Expand Down
23 changes: 17 additions & 6 deletions frontend/src/components/Editor/EditorButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const EditorButtons = ({
<Breadcrumb.Item className="flex align-center " href="#">
{accountId}
</Breadcrumb.Item>
{!isCreateNewIndexer && (
{indexerName && (
<Breadcrumb.Item href="#" active style={{ display: "flex" }}>
{indexerName}
</Breadcrumb.Item>
Expand Down Expand Up @@ -169,21 +169,33 @@ const EditorButtons = ({
<Justify style={{ paddingRight: "2px" }} size={24} />
</Button>
</OverlayTrigger>
{currentUserAccountId && (
{(!isUserIndexer && !isCreateNewIndexer) ? (
<OverlayTrigger
placement="bottom"
overlay={<Tooltip>{getActionButtonText()}</Tooltip>}
overlay={<Tooltip>Fork Indexer</Tooltip>}
>
<Button
variant="primary"
className="px-3"
onClick={() => setShowForkIndexerModal(true)}
>
Fork Indexer
</Button>
</OverlayTrigger>
) : (
<OverlayTrigger
placement="bottom"
overlay={<Tooltip>Publish</Tooltip>}
>
<Button
variant="primary"
className="px-3"
onClick={() => setShowPublishModal(true)}
>
{getActionButtonText()}
Publish
</Button>
</OverlayTrigger>
)}

</ButtonGroup>
</Col>
</Row>
Expand All @@ -208,6 +220,5 @@ const EditorButtons = ({
</>
);
};


export default EditorButtons;
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const IndexerConfigOptions = ({ updateConfig }) => {
<InputGroup.Text> Indexer Name </InputGroup.Text>
<Form.Control
type="text"
placeholder="Indexer Name"
placeholder="indexer_name"
aria-label="IndexerName"
value={indexerNameField}
disabled={!isCreateNewIndexer && showPublishModal}
Expand Down
75 changes: 46 additions & 29 deletions frontend/src/components/Modals/ForkIndexerModal.jsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
import React, { useContext, useState } from "react";
import { Button, Modal, Alert } from "react-bootstrap";
import { Button, Modal, Alert, InputGroup, Form } from "react-bootstrap";
import IndexerConfigOptions from "../Form/IndexerConfigOptionsInputGroup";
import { IndexerDetailsContext } from '../../contexts/IndexerDetailsContext';
import { IndexerDetailsContext } from "../../contexts/IndexerDetailsContext";
import { validateContractId } from "../../utils/validators";

export const ForkIndexerModal = ({
registerFunction,
}) => {
export const ForkIndexerModal = ({ registerFunction, forkIndexer }) => {
const {
indexerDetails,
showForkIndexerModal,
setShowForkIndexerModal,
setIsCreateNewIndexer,
setIndexerName,
setIndexerConfig,
isCreateNewIndexer,
} = useContext(IndexerDetailsContext);
const [indexerConfig, setIndexerConfig] = useState({ filter: "social.near", startBlockHeight: null })
const [indexerName, setIndexerName] = useState("")
const [error, setError] = useState(null)
const [indexerConfig, setIndexerConfigField] = useState({
filter: "social.near",
startBlockHeight: null,
});
const [indexerName, setIndexerNameField] = useState("");
const [error, setError] = useState(null);

const updateConfig = (indexerName, filter, startBlockHeight, option) => {
let finalStartBlockHeight = option === "latestBlockHeight" ? null : startBlockHeight;
setIndexerConfig({ filter, startBlockHeight: finalStartBlockHeight })
setIndexerName(indexerName)
}
const register = async () => {

const fork = async () => {
if (!indexerName) {
setError("Please provide an Indexer Name")
return
setError("Please provide an Indexer Name");
return;
}

if (indexerName === indexerDetails.indexerName) {
setError("Please provide a different Indexer Name than the orginal Indexer")
return
setError(
"Please provide a different Indexer Name than the orginal Indexer"
);
return;
}


if (!validateContractId(indexerConfig.filter)) {
setError("Please provide a valid contract name")
return
setError("Please provide a valid contract name");
return;
}
setError(null)
registerFunction(indexerName, indexerConfig)
setShowForkIndexerModal(false)
}
setError(null);
setIndexerName(indexerName);
setIsCreateNewIndexer(true);
forkIndexer(indexerName, indexerConfig);
setShowForkIndexerModal(false);
};

// <IndexerConfigOptions updateConfig={updateConfig} />
return (
<Modal
centered={true}
Expand All @@ -52,19 +57,31 @@ export const ForkIndexerModal = ({
<Modal.Title> Enter Indexer Details</Modal.Title>
</Modal.Header>
<Modal.Body>
<IndexerConfigOptions updateConfig={updateConfig} />
<InputGroup size="sm">
<InputGroup.Text> Indexer Name </InputGroup.Text>
<Form.Control
type="text"
placeholder="indexer_name"
aria-label="IndexerName"
value={indexerName}
onChange={(e) => setIndexerNameField(e.target.value.trim().toLowerCase())}
/>
</InputGroup>
{error && (
<Alert className="px-3 mt-3" variant="danger">
{error}
</Alert>
)}
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={() => setShowPublishModal(false)}>
<Button
variant="secondary"
onClick={() => setShowForkIndexerModal(false)}
>
Cancel
</Button>
<Button variant="primary" onClick={() => register()}>
Fork Your Own Indexer
<Button variant="primary" onClick={() => fork()}>
Fork Indexer
</Button>
</Modal.Footer>
</Modal>
Expand Down

0 comments on commit 75c35f9

Please sign in to comment.