Skip to content

Commit

Permalink
feat(core): add customer addresses query
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-alexsaiannyi committed Apr 9, 2024
1 parent 71140c9 commit b976210
Show file tree
Hide file tree
Showing 2 changed files with 79 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
74 changes: 74 additions & 0 deletions apps/core/client/queries/get-customer-addresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client';
import { cache } from 'react';

import { getSessionCustomerId } from '~/auth';

import { client } from '..';
import { graphql } from '../graphql';
import { revalidate } from '../revalidate-target';

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: { next: { revalidate } },
});

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 b976210

Please sign in to comment.