-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add customer addresses query (#743)
- Loading branch information
1 parent
3f19124
commit 30c7624
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@bigcommerce/catalyst-core": patch | ||
--- | ||
|
||
Add customer addresses query |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }), | ||
}; | ||
}, | ||
); |