-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor React components and update dependencies
- Loading branch information
Showing
8 changed files
with
422 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,31 @@ | ||
import Login from "Login" | ||
import Notes from "Notes" | ||
import AddNote from "./AddNote" | ||
import { backend } from "declarations/candid" | ||
import { AgentProvider, ActorProvider, useQueryCall } from "@ic-reactor/react" | ||
import { AgentProvider, ActorProvider } from "@ic-reactor/react" | ||
import { NoteActorProvider } from "NoteActor" | ||
import { ICPLedger } from "IcpBalance" | ||
import { ICPTransfer } from "IcpTransfer" | ||
|
||
const publicKey = crypto.getRandomValues(new Uint8Array(48)) | ||
|
||
export type Backend = typeof backend | ||
|
||
const App = () => { | ||
return ( | ||
<AgentProvider> | ||
<Login /> | ||
{/* idlFactory can be fetched from the network if you not provide it */} | ||
<ActorProvider canisterId="ryjl3-tyaaa-aaaaa-aaaba-cai"> | ||
<ActorProvider | ||
canisterId="ryjl3-tyaaa-aaaaa-aaaba-cai" | ||
loadingComponent={<div>Loading Icp Ledger...</div>} | ||
> | ||
<ICPLedger /> | ||
<ICPTransfer /> | ||
</ActorProvider> | ||
<ActorProvider canisterId="xeka7-ryaaa-aaaal-qb57a-cai"> | ||
<NoteActorProvider loadingComponent={<div>Loading Note Actor...</div>}> | ||
<Notes publicKey={publicKey} /> | ||
<AddNote publicKey={publicKey} /> | ||
</ActorProvider> | ||
</NoteActorProvider> | ||
</AgentProvider> | ||
) | ||
} | ||
|
||
export default App | ||
|
||
const ICPLedger = () => { | ||
const { call, data, loading, error } = useQueryCall({ | ||
functionName: "name", | ||
}) | ||
|
||
return ( | ||
<div> | ||
<h2>Test:</h2> | ||
<div> | ||
Loading: {loading.toString()} | ||
<br /> | ||
Error: {error?.toString()} | ||
<br /> | ||
Data:{" "} | ||
{data | ||
? JSON.stringify(data, (_, v) => | ||
typeof v === "bigint" ? v.toString() : v | ||
) | ||
: null} | ||
</div> | ||
<button onClick={call}>Get Name</button> | ||
</div> | ||
) | ||
} |
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,29 @@ | ||
import { useQueryCall, useUserPrincipal } from "@ic-reactor/react" | ||
|
||
export const ICPLedger = () => { | ||
const principal = useUserPrincipal() | ||
|
||
const { call, data, loading, error } = useQueryCall({ | ||
functionName: "icrc1_balance_of", | ||
args: [{ owner: principal, subaccount: [] }], | ||
}) | ||
|
||
return ( | ||
<div> | ||
<h2>ICP Balance:</h2> | ||
<div> | ||
Loading: {loading.toString()} | ||
<br /> | ||
Error: {error?.toString()} | ||
<br /> | ||
balance:{" "} | ||
{data !== undefined | ||
? JSON.stringify(data, (_, v) => | ||
typeof v === "bigint" ? v.toString() : v | ||
) | ||
: null} | ||
</div> | ||
<button onClick={call}>Get Balance</button> | ||
</div> | ||
) | ||
} |
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,62 @@ | ||
import { Principal } from "@dfinity/principal" | ||
import { useUpdateCall } from "@ic-reactor/react" | ||
import { useState } from "react" | ||
|
||
export const ICPTransfer = () => { | ||
const [principal, setPrincipal] = useState("") | ||
const [amount, setAmount] = useState("") | ||
|
||
const { call, data, loading, error } = useUpdateCall({ | ||
functionName: "icrc1_transfer", | ||
}) | ||
|
||
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault() | ||
call([ | ||
{ | ||
to: { | ||
owner: Principal.fromText(principal), | ||
subaccount: [], | ||
}, | ||
amount: BigInt(amount), | ||
fee: [], | ||
memo: [], | ||
created_at_time: [], | ||
from_subaccount: [], | ||
}, | ||
]) | ||
} | ||
|
||
return ( | ||
<div> | ||
<h2>Transfer:</h2> | ||
<div> | ||
Loading: {loading.toString()} | ||
<br /> | ||
Error: {error?.toString()} | ||
<br /> | ||
response:{" "} | ||
{data | ||
? JSON.stringify(data, (_, v) => | ||
typeof v === "bigint" ? v.toString() : v | ||
) | ||
: null} | ||
</div> | ||
<form onSubmit={onSubmit}> | ||
<input | ||
type="text" | ||
value={principal} | ||
name="principal" | ||
onChange={(e) => setPrincipal(e.target.value)} | ||
/> | ||
<input | ||
type="text" | ||
value={amount} | ||
name="amount" | ||
onChange={(e) => setAmount(e.target.value)} | ||
/> | ||
<button>Transfer</button> | ||
</form> | ||
</div> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { createActorContext } from "@ic-reactor/react" | ||
import { backend } from "declarations/candid" | ||
|
||
export type Backend = typeof backend | ||
|
||
export const { | ||
ActorProvider: NoteActorProvider, | ||
useQueryCall: useNoteQueryCall, | ||
useUpdateCall: useNoteUpdateCall, | ||
} = createActorContext<Backend>({ | ||
canisterId: "xeka7-ryaaa-aaaal-qb57a-cai", | ||
}) |
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
Oops, something went wrong.