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

[NO-CHANGELOG] feat: primary revenue widget hooks - part 2 #900

Merged
merged 30 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
bac6541
add event types for primary revenue widget
Sep 25, 2023
0fe667c
add text and view types for primary revenue widget
Sep 25, 2023
004d8bf
scaffold primary revenue widget with params
Sep 25, 2023
e92e3fd
update PrimaryRevenueSuccess type
Sep 25, 2023
9b4a84a
fix Item type imports
Sep 25, 2023
8cf95f5
remove deprecated widget param
Sep 25, 2023
71b122b
Merge branch 'main' into gpr/primary-revenue-widget-setup
jhesgodi Sep 25, 2023
8daf07b
remove duplicated route
Sep 26, 2023
8ea4f30
add shared context for primary revenue views
Sep 26, 2023
b27b867
add hook to sign orders
Sep 26, 2023
836b5ea
update primary revnue main render
Sep 26, 2023
ed6e3d2
add life cycle methods
Sep 26, 2023
6869ba3
fix shadow variable
Sep 26, 2023
887f2c7
Merge branch 'main' into gpr/primary-revenue-widget-setup
jhesgodi Sep 27, 2023
d7b62d0
fix file name typo
Sep 26, 2023
37ba9be
update view context name
Sep 26, 2023
2c975d0
provide eventTarget to event handlers
Sep 26, 2023
9577dab
use biome theme reducer value
Sep 27, 2023
381e0c1
Merge branch 'gpr/primary-revenue-widget-setup' into gpr/primary-reve…
Sep 27, 2023
efa2f6c
remove unused try/catch
Sep 28, 2023
2b9b1c0
Merge branch 'main' into gpr/primary-revenue-widget-setup
jhesgodi Sep 28, 2023
04353fd
Merge branch 'main' into gpr/primary-revenue-widget-setup
jhesgodi Sep 28, 2023
5619743
update widget params
Sep 28, 2023
3c210ff
safe check products input
Sep 28, 2023
5cc937d
Merge branch 'gpr/primary-revenue-widget-setup' into gpr/primary-reve…
Sep 28, 2023
ee8bda2
fix widget initialisation
Sep 28, 2023
dd5ccbb
Merge branch 'gpr/primary-revenue-widget-setup' into gpr/primary-reve…
Sep 28, 2023
33252fe
update params & sign order request
Sep 28, 2023
a1ad3df
Merge branch 'main' into gpr/primary-revenue-widget-hooks
Oct 2, 2023
016ae71
Merge branch 'main' into gpr/primary-revenue-widget-hooks
jhesgodi Oct 4, 2023
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
2 changes: 1 addition & 1 deletion packages/checkout/widgets-lib/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function App() {
</h2>
<br />
<h2>
<a href="/primary-revenue?amount=0.01&fromCurrency=USDC&envId=123">Primary Revenue Widget</a>
<a href="/primary-revenue">Primary Revenue Widget</a>
</h2>
<br />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ConnectLoaderContext } from '../../context/connect-loader-context/Conne
import { PrimaryRevenueWidgetViews } from '../../context/view-context/PrimaryRevenueViewContextTypes';
import { Item } from './types';
import { widgetTheme } from '../../lib/theme';
import { SharedContextProvider } from './context/SharedContextProvider';

export interface PrimaryRevenueWidgetProps {
config: StrongCheckoutWidgetsConfig;
Expand All @@ -35,7 +36,15 @@ export function PrimaryRevenueWidget(props: PrimaryRevenueWidgetProps) {
config, amount, items, fromContractAddress, env, environmentId,
} = props;

console.log('@@@ PrimaryRevenueWidget', config, amount, items, fromContractAddress, env, environmentId);
console.log(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

keeping it to skip eslint check, gets remove in the next PRs.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can add an eslint ignore above the console log

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks.

'@@@ PrimaryRevenueWidget',
config,
amount,
items,
fromContractAddress,
env,
environmentId,
);

const { connectLoaderState } = useContext(ConnectLoaderContext);
const { checkout, provider } = connectLoaderState;
Expand All @@ -51,7 +60,7 @@ export function PrimaryRevenueWidget(props: PrimaryRevenueWidgetProps) {
[viewState, viewDispatch],
);

const mount = useCallback(() => {
const onMount = useCallback(() => {
if (!checkout || !provider) return;

viewDispatch({
Expand All @@ -66,16 +75,35 @@ export function PrimaryRevenueWidget(props: PrimaryRevenueWidgetProps) {

useEffect(() => {
if (!checkout || !provider) return;
mount();

onMount();
}, [checkout, provider]);

return (
<BiomeCombinedProviders theme={{ base: biomeTheme }}>
<ViewContext.Provider value={viewReducerValues}>
{viewState.view.type === SharedViews.LOADING_VIEW && (
<LoadingView loadingText={loadingText} />
)}
Primary Revenue Widget
<SharedContextProvider
value={{
items,
amount,
fromContractAddress,
env,
environmentId,
provider,
checkout,
}}
>
{viewState.view.type === SharedViews.LOADING_VIEW && (
<LoadingView loadingText={loadingText} />
)}
{viewState.view.type === SharedViews.ERROR_VIEW && (
<div>{viewState.view.error.message}</div>
)}
{viewState.view.type
=== PrimaryRevenueWidgetViews.PAYMENT_METHODS && (
<div>Payment methods</div>
)}
</SharedContextProvider>
</ViewContext.Provider>
</BiomeCombinedProviders>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import {
useContext,
createContext,
useMemo,
ReactNode,
useEffect,
useState,
} from 'react';

import { PrimaryRevenueSuccess } from '@imtbl/checkout-widgets';

import { Item, PaymentTypes, SignResponse } from '../types';
import { useSignOrder } from '../hooks/useSignOrder';
import { ConnectLoaderState } from '../../../context/connect-loader-context/ConnectLoaderContext';

type SharedContextProps = {
env: string;
environmentId: string;
items: Item[];
amount: string;
fromContractAddress: string;
provider: ConnectLoaderState['provider'];
checkout: ConnectLoaderState['checkout'];
};

type SharedContextValues = SharedContextProps & {
sign: (paymentType: PaymentTypes) => Promise<SignResponse | undefined>
execute: () => Promise<PrimaryRevenueSuccess>;
recipientAddress: string;
signResponse: SignResponse | undefined;
};

// eslint-disable-next-line @typescript-eslint/naming-convention
const SharedContext = createContext<SharedContextValues>({
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a question on naming, any reason why you are not calling this PrimaryRevenueContext? Or something like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No reason, we can name to PrimaryRevenueContext as per your suggestion.

items: [],
amount: '',
fromContractAddress: '',
provider: undefined,
checkout: undefined,
environmentId: '',
env: '',
recipientAddress: '',
sign: () => Promise.resolve(undefined),
execute: () => Promise.resolve({} as PrimaryRevenueSuccess),
signResponse: undefined,
});

SharedContext.displayName = 'PrimaryRevenueSharedContext';

export function SharedContextProvider(props: {
children: ReactNode;
value: SharedContextProps;
}) {
const {
children,
value: {
env, environmentId, items, amount, fromContractAddress, provider, checkout,
},
} = props;

const [recipientAddress, setRecipientAddress] = useState<string>('');

// Get recipient address
useEffect(() => {
const getRecipientAddress = async () => {
const signer = provider?.getSigner();
const address = await signer?.getAddress();
if (address) {
setRecipientAddress(address);
}
};

getRecipientAddress();
}, [provider]);

const { sign, execute, signResponse } = useSignOrder({
items,
provider,
fromContractAddress,
recipientAddress,
environmentId,
env,
});

const values = useMemo(
() => ({
items,
amount,
fromContractAddress,
sign,
execute,
signResponse,
environmentId,
env,
provider,
checkout,
recipientAddress,
}),
[env, environmentId, items, amount, fromContractAddress, provider, checkout, recipientAddress, signResponse],
);

return (
<SharedContext.Provider value={values}>{children}</SharedContext.Provider>
);
}

export function useSharedContext() {
return useContext(SharedContext);
}
Loading
Loading