Skip to content

Commit

Permalink
feat(core): add customer addresses query (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-alexsaiannyi committed Apr 16, 2024
1 parent 3f19124 commit 30c7624
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-flowers-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

Add customer addresses query
73 changes: 73 additions & 0 deletions apps/core/client/queries/get-customer-addresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client';
import { cache } from 'react';

import { getSessionCustomerId } from '~/auth';

import { client } from '..';
import { graphql } from '../graphql';

const GET_CUSTOMER_ADDRESSES_QUERY = graphql(`
query getCustomerAddresses($after: String, $before: String, $first: Int, $last: Int) {
customer {
entityId
addresses(before: $before, after: $after, first: $first, last: $last) {
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
collectionInfo {
totalItems
}
edges {
node {
entityId
firstName
lastName
address1
address2
city
stateOrProvince
countryCode
phone
postalCode
company
}
}
}
}
}
`);

export interface CustomerAddressesArgs {
after?: string;
before?: string;
limit?: number;
}

export const getCustomerAddresses = cache(
async ({ before = '', after = '', limit = 9 }: CustomerAddressesArgs) => {
const customerId = await getSessionCustomerId();
const paginationArgs = before ? { last: limit, before } : { first: limit, after };

const response = await client.fetch({
document: GET_CUSTOMER_ADDRESSES_QUERY,
variables: { ...paginationArgs },
customerId,
fetchOptions: { cache: 'no-store' },
});

const addresses = response.data.customer?.addresses;

if (!addresses) {
return undefined;
}

return {
pageInfo: addresses.pageInfo,
addressesCount: addresses.collectionInfo?.totalItems ?? 0,
addresses: removeEdgesAndNodes({ edges: addresses.edges }),
};
},
);

0 comments on commit 30c7624

Please sign in to comment.