Skip to content

PoC HWB #2298

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

Draft
wants to merge 1 commit into
base: canary
Choose a base branch
from
Draft

PoC HWB #2298

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
95 changes: 95 additions & 0 deletions core/app/[locale]/(default)/cart/page-data.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client';

import { getSessionCustomerAccessToken } from '~/auth';
import { client } from '~/client';
import { FragmentOf, graphql, VariablesOf } from '~/client/graphql';
import { getShippingZones } from '~/client/management/get-shipping-zones';
import { TAGS } from '~/client/tags';
import { getPreferredCurrencyCode } from '~/lib/currency';

export const PhysicalItemFragment = graphql(`
fragment PhysicalItemFragment on CartPhysicalItem {
Expand Down Expand Up @@ -262,6 +265,98 @@ export const getCart = async (variables: Variables) => {
return data;
};

const PaymentWalletsQuery = graphql(`
query PaymentWalletsQuery($filters: PaymentWalletsFilterInput) {
site {
paymentWallets(filter: $filters) {
edges {
node {
entityId
}
}
}
}
}
`);

type PaymentWalletsVariables = VariablesOf<typeof PaymentWalletsQuery>;

export const getPaymentWallets = async (variables: PaymentWalletsVariables) => {
const customerAccessToken = await getSessionCustomerAccessToken();

const { data } = await client.fetch({
document: PaymentWalletsQuery,
customerAccessToken,
fetchOptions: { cache: 'no-store' },
variables,
});

return removeEdgesAndNodes(data.site.paymentWallets).map(({ entityId }) => entityId);
};

const PaymentWalletWithInitializationDataQuery = graphql(`
query PaymentWalletWithInitializationDataQuery($entityId: String!, $cartId: String!) {
site {
paymentWalletWithInitializationData(
filter: { paymentWalletEntityId: $entityId, cartEntityId: $cartId }
) {
clientToken
initializationData
}
}
}
`);

export const getPaymentWalletWithInitializationData = async (entityId: string, cartId: string) => {
const { data } = await client.fetch({
document: PaymentWalletWithInitializationDataQuery,
variables: {
entityId,
cartId,
},
customerAccessToken: await getSessionCustomerAccessToken(),
fetchOptions: { cache: 'no-store' },
});

return data.site.paymentWalletWithInitializationData;
};

const CurrencyQuery = graphql(`
query Currency($currencyCode: currencyCode!) {
site {
currency(currencyCode: $currencyCode) {
display {
decimalPlaces
symbol
}
name
code
}
}
}
`);

export const getCurrencyData = async (currencyCode?: string) => {
const code = await getPreferredCurrencyCode(currencyCode);

if (!code) {
throw new Error('Could not get currency code');
}

const customerAccessToken = await getSessionCustomerAccessToken();

const { data } = await client.fetch({
document: CurrencyQuery,
fetchOptions: { cache: 'no-store' },
variables: {
currencyCode: code,
},
customerAccessToken,
});

return data.site.currency;
};

export const getShippingCountries = async (geography: FragmentOf<typeof GeographyFragment>) => {
const hasAccessToken = Boolean(process.env.BIGCOMMERCE_ACCESS_TOKEN);
const shippingZones = hasAccessToken ? await getShippingZones() : [];
Expand Down
49 changes: 48 additions & 1 deletion core/app/[locale]/(default)/cart/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import { updateCouponCode } from './_actions/update-coupon-code';
import { updateLineItem } from './_actions/update-line-item';
import { updateShippingInfo } from './_actions/update-shipping-info';
import { CartViewed } from './_components/cart-viewed';
import { getCart, getShippingCountries } from './page-data';
import {
getCart,
getCurrencyData,
getPaymentWallets,
getPaymentWalletWithInitializationData,
getShippingCountries,
} from './page-data';

interface Props {
params: Promise<{ locale: string }>;
Expand All @@ -28,6 +34,36 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
};
}

const createWalletButtonsInitOptions = async (
walletButtons: string[],
cart: {
entityId: string;
currencyCode: string;
},
) => {
const currencyData = await getCurrencyData(cart.currencyCode);

Choose a reason for hiding this comment

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

It seems this request can be made concurrently, as the requests below don't depend on the result of this request.


return Streamable.all(
walletButtons.map(async (entityId) => {
const initData = await getPaymentWalletWithInitializationData(entityId, cart.entityId);
const methodId = entityId.split('.').join('');

return {
methodId,
containerId: `${methodId}-button`,
[methodId]: {
cartId: cart.entityId,
currency: {
code: currencyData?.code,
decimalPlaces: currencyData?.display.decimalPlaces,
},
...initData,
},
};
}),
);
};

const getAnalyticsData = async (cartId: string) => {
const data = await getCart({ cartId });

Expand Down Expand Up @@ -88,6 +124,16 @@ export default async function Cart({ params }: Props) {
);
}

const walletButtons = await getPaymentWallets({
filters: {
cartEntityId: cartId,
},
});

const walletButtonsInitOptions = Streamable.from(() =>
createWalletButtonsInitOptions(walletButtons, cart),
);

const lineItems = [...cart.lineItems.physicalItems, ...cart.lineItems.digitalItems];

const formattedLineItems = lineItems.map((item) => ({
Expand Down Expand Up @@ -277,6 +323,7 @@ export default async function Cart({ params }: Props) {
}}
summaryTitle={t('CheckoutSummary.title')}
title={t('title')}
walletButtonsInitOptions={walletButtonsInitOptions}
/>
</CartAnalyticsProvider>
<CartViewed
Expand Down
Loading
Loading