Skip to content

Commit

Permalink
handle both connection flows with new hook
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlec committed Sep 9, 2024
1 parent 9909e91 commit 12022ee
Showing 1 changed file with 14 additions and 37 deletions.
51 changes: 14 additions & 37 deletions src/pay/components/PayProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ import {
createContext,
useCallback,
useContext,
useEffect,
useRef,
useState,
} from 'react';
import type { ContractFunctionParameters } from 'viem';
import { useAccount, useConnect } from 'wagmi';
import { useWriteContracts } from 'wagmi/experimental';
import { buildPayTransaction } from '../../api';
import { useValue } from '../../internal/hooks/useValue';
import { useCommerceContracts } from '../hooks/useCommerceContracts';
import type { LifeCycleStatus } from '../types';
import { getCommerceContracts } from '../utils/getCommerceContracts';

type PayContextType = {
handleSubmit: () => void;
Expand Down Expand Up @@ -47,13 +45,11 @@ export function PayProvider({
const contractsRef = useRef<ContractFunctionParameters[] | undefined>(
undefined,
);

const { address, isConnected } = useAccount();
const { connectAsync, connectors } = useConnect({
mutation: {
onSuccess: async () => {
const commerceContracts = await fetchData();
contractsRef.current = commerceContracts;
await fetchContracts();
},
},
});
Expand All @@ -62,40 +58,23 @@ export function PayProvider({
// TODO: use the transaction hook
const { writeContractsAsync } = useWriteContracts();

const fetchData = useCallback(async () => {
if (!address) {
console.error('Address is not available');
return;
}
const response = await buildPayTransaction({
address,
chainId,
chargeId,
});
if ('error' in response) {
console.error('Error in Commerce API call:', response.error);
return;
}
return getCommerceContracts({ transaction: response });
}, [address, chainId, chargeId, isConnected]);

useEffect(() => {
const fetchContracts = async () => {
if (address && isConnected) {
const commerceContracts = await fetchData();
contractsRef.current = commerceContracts;
console.log('Successfully fetched contracts');
}
};

fetchContracts();
}, [address, isConnected, fetchData]);
const fetchContracts = useCommerceContracts({
address,
chainId,
chargeId,
contractsRef,
});

const handleSubmit = useCallback(async () => {
setLifecycleStatus('loading');
if (!isConnected) {
if (isConnected) {
// Fetch contracts
await fetchContracts();
} else {
// Prompt for wallet connection
await connectAsync({ connector: connectors[0] });
}

if (contractsRef.current) {
await writeContractsAsync({
contracts: contractsRef.current,
Expand All @@ -104,10 +83,8 @@ export function PayProvider({
console.error('Contracts are not available');
}
}, [
address,
isConnected,
connectAsync,
writeContractsAsync,
connectors,
writeContractsAsync,
setLifecycleStatus,
Expand Down

0 comments on commit 12022ee

Please sign in to comment.