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

feat:integrate wallet and nft example contract #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
78 changes: 71 additions & 7 deletions components/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,63 @@ import Link from "next/link";
import { useRouter } from "next/router";
import useTranslation from "next-translate/useTranslation";
import ChangeLanguage from "./ChangeLanguage";
import * as nearAPI from "near-api-js";
import { useState, useEffect, useCallback } from "react";

// refactor
const nearConfig = {
networkId: "testnet",
nodeUrl: "https://rpc.testnet.near.org",
contractName: "dellwatson.testnet", //contract id, change it later
walletUrl: "https://wallet.testnet.near.org",
helperUrl: "https://helper.testnet.near.org",
};

export default function Menu({ toggled, setToggled }) {
const { asPath } = useRouter();
const { t, lang } = useTranslation("common");

const [user, setUser] = useState(null);
const [wallet, setWallet] = useState(null);

useEffect(() => {
getWallet();
}, []);

// refactor to hooks
const getWallet = useCallback(async () => {
const keyStore = new nearAPI.keyStores.BrowserLocalStorageKeyStore();
const near = await nearAPI.connect({ keyStore, ...nearConfig });
const walletConnection = new nearAPI.WalletConnection(near);
setWallet(walletConnection);

if (walletConnection.getAccountId()) {
console.log(walletConnection, "walletConnection");
setUser({
// Gets the accountId as a string
accountId: walletConnection.getAccountId(),
// Gets the user's token balance
// balance: (await walletConnection.account().state()).amount,
});
}
}, []);

const signIn = () => {
wallet.requestSignIn(
{
contractId: nearConfig.contractName,
}, //contract requesting access
"NEAR AERX", //optional name
null, //optional URL to redirect to if the sign in was successful
null //optional URL to redirect to if the sign in was NOT successful
);
};

const signOut = () => {
wallet.signOut();
window.location.replace(window.location.origin + window.location.pathname);
};

return (
<div
className={`ml-2 md:ml-[142px] md:flex md:items-center w-full ${
Expand Down Expand Up @@ -56,13 +108,25 @@ export default function Menu({ toggled, setToggled }) {
<div className="hidden md:block">
<ChangeLanguage />
</div>
<div
className="p-2 mt-4 ml-auto"
onClick={() => setToggled((prev) => !prev)}
>
<Link href={{ pathname: "/", hash: "login" }}>
<a className="btn-login">{t("navLinkLogin")}</a>
</Link>
{/* TODO: change translation */}

{!user ? (
<div className="p-2 mt-4 ml-auto" onClick={signIn}>
<a className="btn-login">Connect Wallet</a>
</div>
) : (
<>
<div className="p-2 mt-4 ml-auto" onClick={signOut}>
<a className="btn-login">Logout Wallet</a>
</div>
</>
)}
<br />
<div>
{/* UPDATE: UI shows network, display name */}
{wallet && <div>Hello {wallet?._authData?.accountId}</div>}
<br />
<p>Network: testnet</p>
</div>
</div>
);
Expand Down
Loading