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

fix: avatar selection confirmation #849

Open
wants to merge 9 commits into
base: testnet
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
48 changes: 26 additions & 22 deletions components/UI/modalProfilePic.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useCallback, useEffect, useState } from "react";
import styles from "../../styles/components/modalMessage.module.css";
import ppStyles from "../../styles/components/profilePic.module.css";
import { FunctionComponent } from "react";
Expand All @@ -7,13 +7,13 @@ import ClickableAction from "./iconsComponents/clickableAction";
import theme from "../../styles/theme";
import DoneFilledIcon from "./iconsComponents/icons/doneFilledIcon";
import ArrowLeftIcon from "./iconsComponents/icons/arrowLeftIcon";
import { useContractWrite } from "@starknet-react/core";
import { Call } from "starknet";
import identityChangeCalls from "../../utils/callData/identityChangeCalls";
import { hexToDecimal, toUint256 } from "../../utils/feltService";
import { getImgUrl } from "../../utils/stringService";
import { useNotificationManager } from "../../hooks/useNotificationManager";
import { NotificationType, TransactionType } from "../../utils/constants";
import { useAccount } from "@starknet-react/core";

type ModalProfilePicProps = {
closeModal: (cancel: boolean) => void;
Expand All @@ -31,10 +31,8 @@ const ModalProfilePic: FunctionComponent<ModalProfilePicProps> = ({
tokenId,
}) => {
const [callData, setCallData] = useState<Call[]>([]);
const { account } = useAccount();
const { addTransaction } = useNotificationManager();
const { writeAsync: execute, data: updateData } = useContractWrite({
calls: callData,
});

useEffect(() => {
if (!nftData) return;
Expand All @@ -49,22 +47,28 @@ const ModalProfilePic: FunctionComponent<ModalProfilePicProps> = ({
]);
}, [nftData, tokenId]);

useEffect(() => {
if (!updateData?.transaction_hash) return;
addTransaction({
timestamp: Date.now(),
subtext: `For identity ${tokenId}`,
type: NotificationType.TRANSACTION,
data: {
type: TransactionType.SET_PFP,
hash: updateData.transaction_hash,
status: "pending",
},
});
setPfpTxHash(updateData.transaction_hash);
closeModal(false);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [updateData]); // We want to trigger this only when the tx passed
const confirm = useCallback(() => {
if (!account) return;
account
.execute(callData)
.then((tx) => {
addTransaction({
timestamp: Date.now(),
subtext: `For identity ${tokenId}`,
type: NotificationType.TRANSACTION,
data: {
type: TransactionType.SET_PFP,
hash: tx.transaction_hash,
status: "pending",
},
});
setPfpTxHash(tx.transaction_hash);
closeModal(false);
})
.catch((error) => {
console.error("Transaction execution failed", error);
});
}, [account, callData, addTransaction, setPfpTxHash, tokenId, closeModal]);
Marchand-Nicolas marked this conversation as resolved.
Show resolved Hide resolved

return (
<Modal
Expand Down Expand Up @@ -106,7 +110,7 @@ const ModalProfilePic: FunctionComponent<ModalProfilePicProps> = ({
/>
}
description=""
onClick={() => execute()}
onClick={confirm}
/>
<div className={ppStyles.modalBack} onClick={() => closeModal(true)}>
Marchand-Nicolas marked this conversation as resolved.
Show resolved Hide resolved
<ArrowLeftIcon width="24" color={theme.palette.secondary.main} />
Expand Down
3 changes: 2 additions & 1 deletion components/discount/freeRegisterCheckout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { FunctionComponent } from "react";
import { useEffect, useState } from "react";
import type { Call } from "starknet";
import Button from "../UI/button";
import { useAccount, useContractWrite } from "@starknet-react/core";
import { useAccount } from "@starknet-react/core";
Marchand-Nicolas marked this conversation as resolved.
Show resolved Hide resolved
import { utils } from "starknetid.js";
import { getDomainWithStark } from "../../utils/stringService";
import { posthog } from "posthog-js";
Expand All @@ -20,6 +20,7 @@ import { getFreeDomain } from "@/utils/campaignService";
import TermCheckbox from "../domains/termCheckbox";
import { useRouter } from "next/router";
import FreeRegisterSummary from "./freeRegisterSummary";
import { useContractWrite } from "@/hooks/useContract";

type FreeRegisterCheckoutProps = {
domain: string;
Expand Down
3 changes: 2 additions & 1 deletion components/discount/freeRenewalDiscount.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import { FunctionComponent, useEffect, useState } from "react";
import Button from "../UI/button";
import { useAccount, useContractWrite } from "@starknet-react/core";
import { useAccount } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
import {
formatHexString,
isValidEmail,
Expand Down
3 changes: 2 additions & 1 deletion components/discount/registerDiscount.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import { FunctionComponent, useEffect, useState } from "react";
import Button from "../UI/button";
import { useAccount, useContractWrite } from "@starknet-react/core";
import { useAccount } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
import { utils } from "starknetid.js";
import {
formatHexString,
Expand Down
3 changes: 2 additions & 1 deletion components/discount/renewalDiscount.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import { FunctionComponent, useEffect, useState } from "react";
import Button from "../UI/button";
import { useAccount, useContractWrite } from "@starknet-react/core";
import { useAccount } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
import {
formatHexString,
isValidEmail,
Expand Down
3 changes: 2 additions & 1 deletion components/domains/autorenewal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import { FunctionComponent, useEffect, useState } from "react";
import Button from "../UI/button";
import { useAccount, useContractWrite } from "@starknet-react/core";
import { useAccount } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
import {
formatHexString,
isValidEmail,
Expand Down
3 changes: 2 additions & 1 deletion components/domains/externalDomainActions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import { FunctionComponent, useEffect, useState } from "react";
import { useAccount, useContractWrite } from "@starknet-react/core";
import { useAccount } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
import styles from "../../styles/components/identityMenu.module.css";
import { utils } from "starknetid.js";
import ClickableAction from "../UI/iconsComponents/clickableAction";
Expand Down
2 changes: 1 addition & 1 deletion components/domains/externalDomainTransferModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React, {
useContext,
} from "react";
import { TextField, InputAdornment } from "@mui/material";
import { useContractWrite } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
import { useRouter } from "next/router";
import { isHexString, minifyAddress } from "../../utils/stringService";
import { utils } from "starknetid.js";
Expand Down
3 changes: 2 additions & 1 deletion components/domains/steps/checkoutCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
TransactionType,
swissVatRate,
} from "@/utils/constants";
import { useAccount, useContractWrite } from "@starknet-react/core";
import { useAccount } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
import Button from "@/components/UI/button";
import RegisterSummary from "../registerSummary";
import { useNotificationManager } from "@/hooks/useNotificationManager";
Expand Down
2 changes: 1 addition & 1 deletion components/identities/actions/addEvmModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Modal,
TextField,
} from "@mui/material";
import { useContractWrite } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
Marchand-Nicolas marked this conversation as resolved.
Show resolved Hide resolved
import React, { FunctionComponent, useEffect, useRef, useState } from "react";
import styles from "../../../styles/components/evmModalMessage.module.css";
import Button from "../../UI/button";
Expand Down
3 changes: 2 additions & 1 deletion components/identities/actions/changeAddressModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { FunctionComponent, useEffect, useState } from "react";
import { TextField } from "@mui/material";
import { useAccount, useContractWrite } from "@starknet-react/core";
import { useAccount } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
import { isHexString, minifyAddress } from "../../../utils/stringService";
import { hexToDecimal } from "../../../utils/feltService";
import { useNotificationManager } from "../../../hooks/useNotificationManager";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "@anima-protocol/personhood-sdk-react";
import { useAccount } from "@starknet-react/core";
import { Call, TypedData, constants } from "starknet";
import { useContractWrite } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
import { hexToDecimal } from "../../../../utils/feltService";
import { minifyDomain } from "../../../../utils/stringService";
import VerifiedIcon from "../../../UI/iconsComponents/icons/verifiedIcon";
Expand Down
3 changes: 2 additions & 1 deletion components/identities/actions/identityActions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useContext, useMemo } from "react";
import { FunctionComponent, useEffect, useState } from "react";
import { useAccount, useContractWrite } from "@starknet-react/core";
import { useAccount } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
import ChangeAddressModal from "./changeAddressModal";
import TransferFormModal from "./transferFormModal";
import SubdomainModal from "./subdomainModal";
Expand Down
3 changes: 2 additions & 1 deletion components/identities/actions/subdomainModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { FunctionComponent, useEffect, useState } from "react";
import { TextField } from "@mui/material";
import { useAccount, useContractWrite } from "@starknet-react/core";
import { useAccount } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
Marchand-Nicolas marked this conversation as resolved.
Show resolved Hide resolved
import { useIsValid } from "../../../hooks/naming";
import { numberToString } from "../../../utils/stringService";
import SelectIdentity from "../../domains/selectIdentity";
Expand Down
2 changes: 1 addition & 1 deletion components/identities/actions/transferFormModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React, {
useContext,
} from "react";
import { TextField, InputAdornment } from "@mui/material";
import { useContractWrite } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
import { useRouter } from "next/router";
import { isHexString, minifyAddress } from "../../../utils/stringService";
import { utils } from "starknetid.js";
Expand Down
3 changes: 2 additions & 1 deletion components/solana/changeAddressModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Modal, TextField } from "@mui/material";
import { useAccount, useContractWrite } from "@starknet-react/core";
import { useAccount } from "@starknet-react/core";
import { useContractWrite } from "@/hooks/useContract";
import React, { FunctionComponent, useEffect, useState } from "react";
import { isHexString, minifyAddress } from "../../utils/stringService";
import styles from "../../styles/components/modalMessage.module.css";
Expand Down
7 changes: 2 additions & 5 deletions components/solana/domainActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import React, { FunctionComponent, useEffect, useRef, useState } from "react";
import OptionIcon from "../UI/iconsComponents/icons/optionIcon";
import styles from "../../styles/solana.module.css";
import { Abi, Call } from "starknet";
import {
useAccount,
useContractRead,
useContractWrite,
} from "@starknet-react/core";
import { useAccount, useContractRead } from "@starknet-react/core";
Marchand-Nicolas marked this conversation as resolved.
Show resolved Hide resolved
import { useContractWrite } from "@/hooks/useContract";
import SolanaCalls from "../../utils/callData/solanaCalls";
import { utils } from "starknetid.js";
import ChangeAddressModal from "./changeAddressModal";
Expand Down
21 changes: 21 additions & 0 deletions hooks/useContract.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useAccount } from "@starknet-react/core";
import { useState } from "react";
import { Call, InvokeFunctionResponse } from "starknet";

export const useContractWrite = ({ calls }: { calls: Call[] }) => {
const { account } = useAccount();
const [txData, setTxData] = useState<InvokeFunctionResponse | null>(null);
const [error, setError] = useState<string | null>(null);

return {
data: txData,
writeAsync: async () =>
account
? account
.execute(calls)
.then((tx) => setTxData(tx))
.catch((e) => setError(e))
: null,
error,
};
};
Loading