|
| 1 | +import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client'; |
1 | 2 | import { cache } from 'react'; |
2 | 3 |
|
3 | 4 | import { getSessionCustomerAccessToken } from '~/auth'; |
4 | 5 | import { client } from '~/client'; |
5 | 6 | import { graphql, VariablesOf } from '~/client/graphql'; |
6 | 7 | import { revalidate } from '~/client/revalidate-target'; |
7 | 8 | import { TAGS } from '~/client/tags'; |
| 9 | +import { getPreferredCurrencyCode } from '~/lib/currency'; |
8 | 10 |
|
9 | 11 | export const PhysicalItemFragment = graphql(` |
10 | 12 | fragment PhysicalItemFragment on CartPhysicalItem { |
@@ -202,6 +204,9 @@ const CartPageQuery = graphql( |
202 | 204 | discountedAmount { |
203 | 205 | ...MoneyFieldsFragment |
204 | 206 | } |
| 207 | + amount { |
| 208 | + value |
| 209 | + } |
205 | 210 | lineItems { |
206 | 211 | physicalItems { |
207 | 212 | ...PhysicalItemFragment |
@@ -291,6 +296,101 @@ const SupportedShippingDestinationsQuery = graphql(` |
291 | 296 | } |
292 | 297 | } |
293 | 298 | `); |
| 299 | +const PaymentWalletsQuery = graphql(` |
| 300 | + query PaymentWalletsQuery($filters: PaymentWalletsFilterInput) { |
| 301 | + site { |
| 302 | + paymentWallets(filter: $filters) { |
| 303 | + edges { |
| 304 | + node { |
| 305 | + entityId |
| 306 | + methodName |
| 307 | + id |
| 308 | + } |
| 309 | + } |
| 310 | + } |
| 311 | + } |
| 312 | + } |
| 313 | +`); |
| 314 | + |
| 315 | +type PaymentWalletsVariables = VariablesOf<typeof PaymentWalletsQuery>; |
| 316 | + |
| 317 | +export const getPaymentWallets = async (variables: PaymentWalletsVariables) => { |
| 318 | + const customerAccessToken = await getSessionCustomerAccessToken(); |
| 319 | + |
| 320 | + const { data } = await client.fetch({ |
| 321 | + document: PaymentWalletsQuery, |
| 322 | + customerAccessToken, |
| 323 | + fetchOptions: { cache: 'no-store' }, |
| 324 | + variables, |
| 325 | + }); |
| 326 | + |
| 327 | + console.log(data.site.paymentWallets, 'data.site.paymentWallets'); |
| 328 | + |
| 329 | + return removeEdgesAndNodes(data.site.paymentWallets).map(({ entityId }) => entityId); |
| 330 | +}; |
| 331 | + |
| 332 | +const PaymentWalletWithInitializationDataQuery = graphql(` |
| 333 | + query PaymentWalletWithInitializationDataQuery($entityId: String!, $cartId: String!) { |
| 334 | + site { |
| 335 | + paymentWalletWithInitializationData( |
| 336 | + filter: { paymentWalletEntityId: $entityId, cartEntityId: $cartId } |
| 337 | + ) { |
| 338 | + clientToken |
| 339 | + initializationData |
| 340 | + } |
| 341 | + } |
| 342 | + } |
| 343 | +`); |
| 344 | + |
| 345 | +export const getPaymentWalletWithInitializationData = async (entityId: string, cartId: string) => { |
| 346 | + const { data } = await client.fetch({ |
| 347 | + document: PaymentWalletWithInitializationDataQuery, |
| 348 | + variables: { |
| 349 | + entityId, |
| 350 | + cartId, |
| 351 | + }, |
| 352 | + customerAccessToken: await getSessionCustomerAccessToken(), |
| 353 | + fetchOptions: { cache: 'no-store' }, |
| 354 | + }); |
| 355 | + |
| 356 | + return data.site.paymentWalletWithInitializationData; |
| 357 | +}; |
| 358 | + |
| 359 | +const CurrencyQuery = graphql(` |
| 360 | + query Currency($currencyCode: currencyCode!) { |
| 361 | + site { |
| 362 | + currency(currencyCode: $currencyCode) { |
| 363 | + display { |
| 364 | + decimalPlaces |
| 365 | + symbol |
| 366 | + } |
| 367 | + name |
| 368 | + code |
| 369 | + } |
| 370 | + } |
| 371 | + } |
| 372 | +`); |
| 373 | + |
| 374 | +export const getCurrencyData = async (currencyCode?: string) => { |
| 375 | + const code = await getPreferredCurrencyCode(currencyCode); |
| 376 | + |
| 377 | + if (!code) { |
| 378 | + throw new Error('Could not get currency code'); |
| 379 | + } |
| 380 | + |
| 381 | + const customerAccessToken = await getSessionCustomerAccessToken(); |
| 382 | + |
| 383 | + const { data } = await client.fetch({ |
| 384 | + document: CurrencyQuery, |
| 385 | + fetchOptions: { cache: 'no-store' }, |
| 386 | + variables: { |
| 387 | + currencyCode: code, |
| 388 | + }, |
| 389 | + customerAccessToken, |
| 390 | + }); |
| 391 | + |
| 392 | + return data.site.currency; |
| 393 | +}; |
294 | 394 |
|
295 | 395 | export const getShippingCountries = cache(async () => { |
296 | 396 | const { data } = await client.fetch({ |
|
0 commit comments