Skip to content

Commit

Permalink
Update .gitignore and package.json, and refactor React code
Browse files Browse the repository at this point in the history
  • Loading branch information
b3hr4d committed Feb 24, 2024
1 parent 61f100f commit c744fa2
Show file tree
Hide file tree
Showing 20 changed files with 430 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main

jobs:
build-and-deploy:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
68 changes: 68 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Build and Release

on:
workflow_dispatch: # Allows manual triggering from the GitHub UI
push:
tags:
- "v*" # Triggers on tag push matching v1.0, v1.1, etc.

jobs:
build-and-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: "20"
cache: "yarn"

- name: Install Yarn
run: npm install -g yarn

- name: Install dependencies
run: yarn install

- name: Build
run: yarn bundle

- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref_name }}
draft: false
prerelease: false

- name: Upload Production Bundle
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./packages/core/umd/production/index.min.js
asset_name: core.min.js
asset_content_type: application/javascript

- name: Upload Development Bundle
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./packages/core/umd/development/index.js
asset_name: core.js
asset_content_type: application/javascript

- name: Upload Development Source Map
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./packages/core/umd/development/index.js.map
asset_name: core.js.map
asset_content_type: application/json
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ examples/candid-ui
examples/*/deps/candid
docs
.nx
bundle
umd
18 changes: 9 additions & 9 deletions examples/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ <h1>ICRC Token</h1>
</fieldset>
</div>

<script src="../../packages/core/bundle/bundle.js"></script>
<script src="../../packages/core/umd/core.js"></script>
<script>
const agentManager = reactor.createAgentManager({ withDevtools: true })
const candidAdapter = reactor.createCandidAdapter({ agentManager })
const agentManager = Reactor.createAgentManager({ withDevtools: true })
const candidAdapter = Reactor.createCandidAdapter({ agentManager })

let previousActorCleanup = null
let balanceUnsub = null
Expand Down Expand Up @@ -161,8 +161,8 @@ <h1>ICRC Token</h1>
agentManager.updateAgent({
host:
networkSelect.value === "local"
? reactor.utils.LOCAL_HOST_NETWORK_URI
: reactor.utils.IC_HOST_NETWORK_URI,
? Reactor.utils.LOCAL_HOST_NETWORK_URI
: Reactor.utils.IC_HOST_NETWORK_URI,
})

const canisterId = canisterIdInput.value
Expand All @@ -171,7 +171,7 @@ <h1>ICRC Token</h1>
const { idlFactory } = await candidAdapter.getCandidDefinition(canisterId)
resultDiv.innerHTML = `Loaded ${canisterId}`

const reactorCore = reactor.createReactorCore({
const reactorCore = Reactor.createReactorCore({
agentManager,
canisterId,
idlFactory,
Expand Down Expand Up @@ -216,7 +216,7 @@ <h1>ICRC Token</h1>
? "Loading..."
: error
? `Error: ${error.message}`
: `${functionName}: ${reactor.utils.jsonToString(data)}`
: `${functionName}: ${Reactor.utils.jsonToString(data)}`
})

resultDiv.appendChild(container)
Expand Down Expand Up @@ -251,7 +251,7 @@ <h1>ICRC Token</h1>
? "Loading..."
: error
? `Error: ${error.message}`
: reactor.utils.jsonToString(data)
: Reactor.utils.jsonToString(data)
})

transferUnsub = transferSubscribe(({ data, loading, error }) => {
Expand All @@ -260,7 +260,7 @@ <h1>ICRC Token</h1>
: error
? `Error: ${error.message}`
: data
? `Transfer Result: ${reactor.utils.jsonToString(data)}`
? `Transfer Result: ${Reactor.utils.jsonToString(data)}`
: ""
})

Expand Down
10 changes: 4 additions & 6 deletions examples/javascript/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,10 @@ agentManager.subscribeAuthState(
agentManager.authenticate()

const renderActor = async (event) => {
if (event) {
event.preventDefault()
// previousActorCleanup?.()
balanceUnsub?.()
transferUnsub?.()
}
event.preventDefault()
previousActorCleanup?.()
balanceUnsub?.()
transferUnsub?.()

agentManager.updateAgent({
host:
Expand Down
1 change: 1 addition & 0 deletions examples/javascript/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require("path")
const HtmlWebpackPlugin = require("html-webpack-plugin")

/** @type {import("webpack").Configuration} */
module.exports = {
entry: "./src/main.js",
output: {
Expand Down
219 changes: 219 additions & 0 deletions examples/react-html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IC Reactor Example</title>
</head>

<body>
<h1>IC Reactor Example</h1>
<div id="app"></div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/7.23.10/babel.min.js"
integrity="sha512-aKni+N2bgewoe8CGZlB9c0IDLU5LQaMEZFNjP6FbDK8gZhdTdNKhYKgZaY/EYL9GKJeJigN/wA7WDAOJShgMAQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"
integrity="sha512-8Q6Y9XnTbOE+JNvjBQwJ2H8S+UV4uA6hiRykhdtIyDYZ2TprdNmWOUaKdGzOhyr4dCyk287OejbPvwl7lrfqrQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js"
integrity="sha512-MOCpqoRoisCTwJ8vQQiciZv0qcpROCidek3GTFS6KTk2+y7munJIlKCVkFCYY+p3ErYFXCjmFjnfTTRSC1OHWQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="../../packages/react/bundle/bundle.js"></script>
<script type="text/babel">
const { ActorProvider, useAgentManager, useUpdateCall, useQueryCall, useUserPrincipal, useAuth, AgentProvider, utils } = Reactor

const Login = () => {
const {
login,
logout,
loginLoading,
loginError,
identity,
authenticating,
authenticated
} = useAuth()

return (
<div>
{loginLoading && <div>Loading...</div>}
{loginError ? <div>{JSON.stringify(loginError)}</div> : null}
{identity && <div>{identity.getPrincipal().toText()}</div>}
{
authenticated ? (
<div>
<button onClick={() => logout()}>Logout</button>
</div>
) : (
<div>
<button onClick={() => login()} disabled={authenticating}>
Login
</button>
</div>
)
}
</div>
)
}

const ICRC1Call = ({ functionName }) => {
const { call, data, loading } = useQueryCall({
functionName,
})

return (
<div>
<span>
<strong>{functionName}</strong>:{" "}
<button onClick={call} disabled={loading}>
</button>{" "}
{loading ? "Loading..." : utils.jsonToString(data)}
</span>
</div>
)
}


const UserWallet = ({ principal }) => {
const toRef = React.useRef(null)
const amountRef = React.useRef(null)

const {
call: refetchBalance,
data: balance,
loading: balanceLoading,
} = useQueryCall({
functionName: "icrc1_balance_of",
args: [{ owner: principal, subaccount: [] }],
})

const {
call: transfer,
loading: transferLoading,
data: transferResult,
} = useUpdateCall({
functionName: "icrc1_transfer",
})

const onSubmit = (event) => {
event.preventDefault()
transfer([
{
to: {
owner: Principal.fromText(toRef.current?.value || ""),
subaccount: [],
},
amount: BigInt(amountRef.current?.value || "0"),
fee: [],
memo: [],
created_at_time: [],
from_subaccount: [],
},
])
}

return (
<div>
<h2>User Wallet</h2>
<span>Principal: {principal?.toString()}</span>
<div>
<span>
<strong>Balance</strong>:{" "}
<button onClick={refetchBalance} disabled={balanceLoading}>
</button>{" "}
{balanceLoading ? "Loading..." : utils.jsonToString(balance)}
</span>
</div>
<form onSubmit={onSubmit}>
<input ref={toRef} type="text" placeholder="To" />
<input ref={amountRef} type="text" placeholder="Amount" />
<button>Transfer</button>
</form>
<div>
<span>
<strong>Transfer Result</strong>:{" "}
{transferLoading ? "Loading..." : utils.jsonToString(transferResult)}
</span>
</div>
</div>
)
}


const functionNames = [
"icrc1_name",
"icrc1_symbol",
"icrc1_fee",
"icrc1_decimals",
"icrc1_metadata",
"icrc1_total_supply",
"icrc1_minting_account",
]

const App = () => {
const inputRef = React.useRef(null)
const networkRef = React.useRef(null)
const [canisterId, setCanisterId] = React.useState(
"ryjl3-tyaaa-aaaaa-aaaba-cai"
)

const { updateAgent } = useAgentManager()

const onSubmit = (event) => {
event.preventDefault()
try {
const principal = Principal.fromText(inputRef.current?.value || "")
console.log(networkRef.current?.value)

updateAgent({
host:
networkRef.current?.value === "local"
? utils.LOCAL_HOST_NETWORK_URI
: utils.IC_HOST_NETWORK_URI,
})
setCanisterId(principal.toText())
} catch (e) {
console.error(e)
}
}

const principal = useUserPrincipal()

return (
<ActorProvider canisterId={canisterId}>
<h1>ICRC Token</h1>
<Login />
<form onSubmit={onSubmit}>
<select ref={networkRef}>
<option value="ic">IC</option>
<option value="local">Local</option>
</select>
<input
id="canisterId"
required
ref={inputRef}
defaultValue={canisterId}
/>
<button type="submit">Fetch</button>
</form>
{functionNames.map((functionName) => (
<ICRC1Call key={functionName} functionName={functionName} />
))}
{principal && <UserWallet principal={principal} />}
</ActorProvider>
)
}


ReactDOM.render(
<AgentProvider>
<App />
</AgentProvider>,
document.getElementById("app")
);
</script>

</html>
Loading

0 comments on commit c744fa2

Please sign in to comment.