Skip to content

Commit

Permalink
Merge pull request #122 from map3xyz/phil/map-177-pass-payment-method…
Browse files Browse the repository at this point in the history
…-in-init

feat: payment-method in config
  • Loading branch information
plondon authored Feb 4, 2023
2 parents 0deb33a + afa63ef commit 1077e1a
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 91 deletions.
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface Map3InitConfig {
onClose?: () => void;
onFailure?: (error: string, networkCode: string, address?: string) => void;
onSuccess?: (txHash: string, networkCode: string, address?: string) => void;
paymentMethod?: 'binance-pay' | 'show-address';
rainbowRoad?: boolean;
theme?: 'dark' | 'light';
userId: string;
Expand Down
1 change: 1 addition & 0 deletions src/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ root.render(
address: '0x67ef9a8d7f4ed931ac63e873b75d8f8dea64cdb2',
};
},
paymentMethod: 'binance-pay',
theme: 'dark',
userId: 'preview-user-id',
});
Expand Down
6 changes: 6 additions & 0 deletions src/providers/Store/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type State = {
};
providerChainId?: number;
requiredAmount?: string;
requiredPaymentMethod?: 'binance-pay' | 'show-address';
slug?: string;
step: number;
steps: (keyof typeof Steps)[];
Expand Down Expand Up @@ -260,6 +261,7 @@ export const Store: React.FC<
network?: Network;
onFailure?: (error: string, networkCode: string, address?: string) => void;
onSuccess?: (txHash: string, networkCode: string, address?: string) => void;
paymentMethod?: 'binance-pay' | 'show-address';
theme?: 'dark' | 'light';
userId: string;
}>
Expand All @@ -275,6 +277,7 @@ export const Store: React.FC<
network,
onFailure,
onSuccess,
paymentMethod,
theme,
userId,
}) => {
Expand All @@ -293,6 +296,8 @@ export const Store: React.FC<
requiredAmount = ethers.utils.formatUnits(amount, asset.decimals);
}

const requiredPaymentMethod = paymentMethod;

const [state, dispatch] = useReducer(
(state: State, action: Action): State => {
switch (action.type) {
Expand Down Expand Up @@ -515,6 +520,7 @@ export const Store: React.FC<
fiat,
network,
requiredAmount,
requiredPaymentMethod,
step,
theme,
userId,
Expand Down
209 changes: 118 additions & 91 deletions src/steps/PaymentMethod/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import ErrorWrapper from '../../components/ErrorWrapper';
import InnerWrapper from '../../components/InnerWrapper';
import LoadingWrapper from '../../components/LoadingWrapper';
import MethodIcon from '../../components/MethodIcon';
import { useGetPaymentMethodsQuery } from '../../generated/apollo-gql';
import {
PaymentMethod,
useGetPaymentMethodsQuery,
} from '../../generated/apollo-gql';
import { useWeb3 } from '../../hooks/useWeb3';
import { Context, Steps } from '../../providers/Store';

Expand All @@ -20,13 +23,126 @@ const PaymentMethod: React.FC<Props> = () => {
variables: { chainId },
});

const selectMethod = (method: PaymentMethod) => {
if (!method.flags?.enabled) {
return;
}
dispatch({
payload: method,
type: 'SET_PAYMENT_METHOD',
});
if (method.value === 'show-address') {
if (state.requiredAmount) {
dispatch({
payload: [
'AssetSelection',
'NetworkSelection',
'PaymentMethod',
'ConfirmRequiredAmount',
'ShowAddress',
'Result',
],
type: 'SET_STEPS',
});
dispatch({
payload: Steps.ConfirmRequiredAmount,
type: 'SET_STEP',
});
} else {
dispatch({
payload: [
'AssetSelection',
'NetworkSelection',
'PaymentMethod',
'ShowAddress',
'Result',
],
type: 'SET_STEPS',
});
dispatch({
payload: Steps.ShowAddress,
type: 'SET_STEP',
});
}
} else if (method.value === 'binance-pay') {
dispatch({
payload: [
'AssetSelection',
'NetworkSelection',
'PaymentMethod',
'EnterAmount',
'BinancePay',
'Result',
],
type: 'SET_STEPS',
});
dispatch({
payload: Steps.EnterAmount,
type: 'SET_STEP',
});
} else if (method.value === 'isWalletConnect') {
dispatch({
payload: [
'AssetSelection',
'NetworkSelection',
'PaymentMethod',
'WalletConnect',
'EnterAmount',
'Result',
],
type: 'SET_STEPS',
});
dispatch({
payload: Steps.WalletConnect,
type: 'SET_STEP',
});
} else {
dispatch({
payload: [
'AssetSelection',
'NetworkSelection',
'PaymentMethod',
'EnterAmount',
'Result',
],
type: 'SET_STEPS',
});
dispatch({
payload: Steps.EnterAmount,
type: 'SET_STEP',
});
}
};

useEffect(() => {
state.provider?.data?.off?.('network');
dispatch({ type: 'SET_ACCOUNT_IDLE' });
dispatch({ type: 'SET_PROVIDER_IDLE' });
dispatch({ payload: undefined, type: 'SET_PROVIDER_CHAIN_ID' });
}, []);

useEffect(() => {
const method = data?.methodsForNetwork?.find(
(method) => method?.value === state.requiredPaymentMethod
);
if (state.requiredPaymentMethod && method) {
// @ts-ignore
if (state.prevStep >= state.steps.indexOf(Steps[Steps.PaymentMethod])) {
dispatch({ payload: Steps.AssetSelection, type: 'SET_STEP' });
} else {
selectMethod(method);
}
}
}, [data?.methodsForNetwork?.length]);

if (
state.requiredPaymentMethod &&
data?.methodsForNetwork?.find(
(method) => method?.value === state.requiredPaymentMethod
)
)
return null;

if (!state.asset || !state.network) {
dispatch({ payload: Steps.AssetSelection, type: 'SET_STEP' });
return null;
Expand Down Expand Up @@ -164,96 +280,7 @@ const PaymentMethod: React.FC<Props> = () => {
: '!cursor-not-allowed opacity-50 hover:bg-white dark:hover:bg-neutral-900'
}`}
key={method.name + '-' + method.value}
onClick={() => {
if (!method.flags?.enabled) {
return;
}
dispatch({
payload: method,
type: 'SET_PAYMENT_METHOD',
});
if (method.value === 'show-address') {
if (state.requiredAmount) {
dispatch({
payload: [
'AssetSelection',
'NetworkSelection',
'PaymentMethod',
'ConfirmRequiredAmount',
'ShowAddress',
'Result',
],
type: 'SET_STEPS',
});
dispatch({
payload: Steps.ConfirmRequiredAmount,
type: 'SET_STEP',
});
} else {
dispatch({
payload: [
'AssetSelection',
'NetworkSelection',
'PaymentMethod',
'ShowAddress',
'Result',
],
type: 'SET_STEPS',
});
dispatch({
payload: Steps.ShowAddress,
type: 'SET_STEP',
});
}
} else if (method.value === 'binance-pay') {
dispatch({
payload: [
'AssetSelection',
'NetworkSelection',
'PaymentMethod',
'EnterAmount',
'BinancePay',
'Result',
],
type: 'SET_STEPS',
});
dispatch({
payload: Steps.EnterAmount,
type: 'SET_STEP',
});
} else if (method.value === 'isWalletConnect') {
dispatch({
payload: [
'AssetSelection',
'NetworkSelection',
'PaymentMethod',
'WalletConnect',
'EnterAmount',
'Result',
],
type: 'SET_STEPS',
});
dispatch({
payload: Steps.WalletConnect,
type: 'SET_STEP',
});
} else {
dispatch({
payload: [
'AssetSelection',
'NetworkSelection',
'PaymentMethod',
'EnterAmount',
'Result',
],
type: 'SET_STEPS',
});
dispatch({
payload: Steps.EnterAmount,
type: 'SET_STEP',
});
}
}}
onClick={() => selectMethod(method)}
role="button"
>
<div className="flex items-center gap-2">
Expand Down

0 comments on commit 1077e1a

Please sign in to comment.