Skip to content

Commit

Permalink
added state checks on button
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinsga committed Jan 22, 2022
1 parent aa5282d commit 617016a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
20 changes: 11 additions & 9 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
const IMAGE_LINK = "/example.gif";
/***********************************/
const { solana } = window as any;
let { solana } = window as any;
const rpcUrl = import.meta.env.VITE_APP_SOLANA_RPC_HOST?.toString();
const cluster = import.meta.env.VITE_APP_SOLANA_NETWORK?.toString();
const candyMachineId = import.meta.env.VITE_APP_CANDY_MACHINE_ID?.toString();
Expand Down Expand Up @@ -63,6 +63,7 @@
}
onMount(async () => {
solana = (window as any).solana;
// Check if environement variables are populated
errorOcurred = checkEnvironmentVariables();
if (errorOcurred) {
Expand All @@ -83,7 +84,6 @@
candyMachinePublicKey,
provider
);
// Establish connection to wallet
if (solana?.isPhantom) {
$userState.walletPublicKey = await checkWalletConnected(solana);
Expand All @@ -93,12 +93,14 @@
$userState.walletPublicKey,
connection
);
// Check if user is whitelisted (ie. check if they have token)
$userState.isWhiteListed = await existsOwnerSPLToken(
$userState.walletPublicKey,
connection,
$candyMachineState.state.whitelistMintSettings?.mint
);
// If whitelist config populated, check if user is whitelisted (ie. check if they have token)
if ($candyMachineState.state.whitelistMintSettings) {
$userState.isWhiteListed = await existsOwnerSPLToken(
$userState.walletPublicKey,
connection,
$candyMachineState.state.whitelistMintSettings?.mint
);
}
}
}
Expand Down Expand Up @@ -150,7 +152,7 @@
<div class="text-sm sm:text-md font-semibold pb-5 text-gray-600 ">
{DESCRTIPTION}
</div>
<Button {solana} {connection} />
<Button {connection} />

<div class=" tracking-widest font-bold text-sm pt-3 text-gray-400">
{itemsRedeemed}/{itemsAvailable} claimed
Expand Down
33 changes: 25 additions & 8 deletions src/components/Button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { awaitTransactionSignatureConfirmation } from "../lib/connection";
import confetti from "canvas-confetti";
import { LAMPORTS_PER_SOL } from "@solana/web3.js";
import { onMount } from "svelte";
const txTimeout = 30000;
const cluster = import.meta.env.VITE_APP_SOLANA_NETWORK?.toString();
Expand All @@ -24,7 +25,7 @@
let isMinting = false;
let mintSuccessful = false;
export let solana;
let { solana } = window as any;
export let connection;
async function connectWalletButton() {
Expand All @@ -36,11 +37,13 @@
connection
);
// Check if user is whitelisted (ie. check if they have token)
$userState.isWhiteListed = await existsOwnerSPLToken(
$userState.walletPublicKey,
connection,
$candyMachineState.state.whitelistMintSettings?.mint
);
if ($candyMachineState.state.whitelistMintSettings) {
$userState.isWhiteListed = await existsOwnerSPLToken(
$userState.walletPublicKey,
connection,
$candyMachineState.state.whitelistMintSettings?.mint
);
}
}
}
Expand Down Expand Up @@ -99,14 +102,28 @@
origin: { y: 0.6 },
});
}
function getPhantomWallet() {
// Here we check for the solana object again.
// If its present, reload the page so state is refreshed.
solana = (window as any).solana;
if (solana) {
location.reload();
} else {
window.open("https://phantom.app/", "_blank");
}
}
onMount(() => {
solana = (window as any).solana;
});
</script>

<div class="flex flex-col">
{#if !solana}
<button
class=" px-3 py-2 rounded-md bg-sky-600 hover:bg-sky-700 text-white font-bold"
on:click={() => window.open("https://phantom.app/", "_blank")}
>Get Phantom Wallet</button
on:click={() => getPhantomWallet()}>Get Phantom Wallet</button
>
{:else if !$userState.walletPublicKey}
<button
Expand Down
2 changes: 1 addition & 1 deletion src/lib/state-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function checkWalletConnected(solana: any) {
const response = await solana.connect({ onlyIfTrusted: true });
return response.publicKey.toString();
} catch (error) {
console.error(error);
console.log("Unlock your wallet!");
}
}

Expand Down

1 comment on commit 617016a

@vercel
Copy link

@vercel vercel bot commented on 617016a Jan 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.