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

Block UI on click #6

Merged
merged 5 commits into from
Nov 3, 2022
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
46 changes: 23 additions & 23 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { COMMANDS } from "../cmds"
import useCurrentUser from "../hooks/use-current-user"
import useConfig from "../hooks/use-config"
import { init } from "@onflow/fcl-wc"
import Loading from "./loading"
import Image from "next/image"
import "../flow/config"

Expand All @@ -16,31 +17,30 @@ const WC_METADATA = {
icons: ["https://avatars.githubusercontent.com/u/62387156?s=280&v=4"],
}

const renderCommand = (d, setStatus) => {
return (
<li key={d.LABEL}>
<button
onClick={async () => {
setStatus("pending...")
setStatus(await d.CMD())
}}
>
{d.LABEL}
</button>
</li>
)
}

export default function Home() {
const currentUser = useCurrentUser()
const config = useConfig()
const [services, setServices] = useState([])
const [isLoading, setIsLoading] = useState(false)
const discoveryWalletInputRef = useRef(null)
const [status, setStatus] = useState(null)

const renderCommand = d => {
return (
<li key={d.LABEL}>
<button onClick={() => clickHandler(d.CMD)}>{d.LABEL}</button>
</li>
)
}

async function clickHandler(fn, args = null) {
setIsLoading(true)
await fn(args)
setIsLoading(false)
}

useEffect(() => {
const initAdapter = async () => {
const { FclWcServicePlugin, client } = await init({
const { FclWcServicePlugin } = await init({
projectId: WC_PROJECT_ID,
metadata: WC_METADATA,
includeBaseWC: true,
Expand Down Expand Up @@ -73,8 +73,7 @@ export default function Home() {

return (
<div>
<ul>{COMMANDS.map(cmd => renderCommand(cmd, setStatus))}</ul>
<pre>Status: {status ?? ""}</pre>
<ul>{COMMANDS.map(cmd => renderCommand(cmd))}</ul>
<div>
{services?.map(service => (
<span key={service.provider.address}>
Expand All @@ -84,20 +83,20 @@ export default function Home() {
width={25}
height={25}
/>
<button onClick={() => fcl.authenticate({ service })}>
<button onClick={() => clickHandler(fcl.authenticate, { service })}>
Login with {service.provider.name}
</button>
</span>
))}
</div>
<div style={{ marginTop: "12px" }}>
<label htmlFor="manual-wallet">
Manually set "discovery.wallet" config:{" "}
{'Manually set "discovery.wallet" config:'}
</label>
<input ref={discoveryWalletInputRef} name="manual-wallet"></input>
<button
onClick={async () =>
await fcl
onClick={() =>
fcl
.config()
.put("discovery.wallet", discoveryWalletInputRef?.current?.value)
}
Expand All @@ -106,6 +105,7 @@ export default function Home() {
</button>
</div>
<pre>{JSON.stringify({ currentUser, config }, null, 2)}</pre>
{isLoading ? <Loading /> : null}
</div>
)
}
9 changes: 9 additions & 0 deletions pages/loading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function Loading() {
return (
<div>
<div className="loading style-2">
<div className="loading-wheel"></div>
</div>
</div>
)
}
47 changes: 47 additions & 0 deletions styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,50 @@ a {
* {
box-sizing: border-box;
}

.loading {
width: 100%;
height: 100%;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.5);
}
.loading-wheel {
width: 20px;
height: 20px;
margin-top: -40px;
margin-left: -40px;

position: absolute;
top: 50%;
left: 50%;

border-width: 30px;
border-radius: 50%;
-webkit-animation: spin 1s linear infinite;
animation: spin 1s linear infinite;
}
.style-2 .loading-wheel {
border-style: double;
border-color: #ccc transparent;
}
@-webkit-keyframes spin {
0% {
-webkit-transform: rotate(0);
}
100% {
-webkit-transform: rotate(-360deg);
}
}

@keyframes spin {
0% {
-webkit-transform: rotate(0);
}
100% {
-webkit-transform: rotate(-360deg);
}
}