-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Invoke Contract] Create a dropdown & retrieve function methods
- Loading branch information
1 parent
b6eb1e3
commit 88a35f0
Showing
9 changed files
with
230 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/components/FormElements/ContractMethodSelectPicker.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useState } from "react"; | ||
|
||
import { Select } from "@stellar/design-system"; | ||
|
||
export const ContractMethodSelectPicker = ({ | ||
methods, | ||
id, | ||
}: { | ||
methods: string[]; | ||
id: string; | ||
}) => { | ||
const [selectedValue, setSelectedValue] = useState<string>(""); | ||
|
||
const onChange = (e: React.ChangeEvent<HTMLSelectElement>) => { | ||
setSelectedValue(e.target.value); | ||
}; | ||
|
||
return ( | ||
<Select | ||
fieldSize="md" | ||
label="Select a function method" | ||
id={id} | ||
value={selectedValue} | ||
onChange={onChange} | ||
> | ||
{methods.map((method) => ( | ||
<option key={method} value={method}> | ||
{method} | ||
</option> | ||
))} | ||
</Select> | ||
|
||
// @TODO | ||
// Render func args | ||
); | ||
}; |
117 changes: 117 additions & 0 deletions
117
src/components/FormElements/FetchContractMethodPickerWithQuery.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import React, { useState } from "react"; | ||
import { Button } from "@stellar/design-system"; | ||
|
||
import { Box } from "@/components/layout/Box"; | ||
import { ContractMethodSelectPicker } from "@/components/FormElements/ContractMethodSelectPicker"; | ||
import { TextPicker } from "@/components/FormElements/TextPicker"; | ||
import { MessageField } from "@/components/MessageField"; | ||
|
||
import { | ||
fetchContractFunctionMethods, | ||
ContractFunctionMethods, | ||
} from "@/helpers/sorobanUtils"; | ||
|
||
import { useStore } from "@/store/useStore"; | ||
import { validate } from "@/validate"; | ||
|
||
interface FetchContractMethodPickerWithQueryProps { | ||
id: string; | ||
value: string; | ||
error?: string | undefined; | ||
label?: string; | ||
disabled?: boolean; | ||
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void; | ||
} | ||
|
||
export const FetchContractMethodPickerWithQuery = ({ | ||
id, | ||
value, | ||
error, | ||
label, | ||
disabled, | ||
onChange, | ||
}: FetchContractMethodPickerWithQueryProps) => { | ||
const { network } = useStore(); | ||
|
||
const [contractIdError, setContractIdError] = useState<string>(""); | ||
|
||
const [contractMethods, setContractMethods] = useState<string[]>([]); | ||
const [fetchError, setFetchError] = useState<string>(""); | ||
|
||
const onContractIdChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
// reset the error and methods | ||
setFetchError(""); | ||
setContractMethods([]); | ||
|
||
// call the onChange callback | ||
onChange?.(e); | ||
|
||
// validate the contract id | ||
if (e.target.value) { | ||
const validatedContractIdError = validate.getContractIdError( | ||
e.target.value, | ||
); | ||
|
||
if (validatedContractIdError) { | ||
setContractIdError(validatedContractIdError); | ||
} else { | ||
setContractIdError(""); | ||
} | ||
} | ||
}; | ||
|
||
const handleFetchContractMethods = async () => { | ||
const contractMethods: ContractFunctionMethods = | ||
await fetchContractFunctionMethods({ | ||
contractId: value, | ||
networkPassphrase: network.passphrase, | ||
rpcUrl: network.rpcUrl, | ||
}); | ||
|
||
if (contractMethods.methods) { | ||
setContractMethods(contractMethods.methods); | ||
} | ||
|
||
if (contractMethods.error) { | ||
setFetchError(contractMethods.error); | ||
} | ||
}; | ||
|
||
return ( | ||
<Box gap="md"> | ||
<TextPicker | ||
key={id} | ||
id={id} | ||
label={label} | ||
placeholder="Ex: CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC" | ||
value={value || ""} | ||
error={error || contractIdError} | ||
onChange={onContractIdChange} | ||
disabled={disabled} | ||
/> | ||
|
||
<Box gap="md" direction="row" wrap="wrap"> | ||
<Button | ||
disabled={!value || Boolean(contractIdError)} | ||
variant="secondary" | ||
size="md" | ||
onClick={handleFetchContractMethods} | ||
type="button" | ||
> | ||
Fetch contract methods | ||
</Button> | ||
|
||
<>{fetchError ? <MessageField message={fetchError} isError /> : null}</> | ||
</Box> | ||
|
||
<> | ||
{contractMethods.length ? ( | ||
<ContractMethodSelectPicker | ||
methods={contractMethods} | ||
id={`${id}-method`} | ||
/> | ||
) : null} | ||
</> | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters