-
Notifications
You must be signed in to change notification settings - Fork 79
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 address mutation (#777)
- Loading branch information
1 parent
d3cb5bd
commit fe5c221
Showing
2 changed files
with
58 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 address mutation |
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,53 @@ | ||
import { getSessionCustomerId } from '~/auth'; | ||
|
||
import { client } from '..'; | ||
import { graphql, VariablesOf } from '../graphql'; | ||
|
||
const ADD_CUSTOMER_ADDRESS_MUTATION = graphql(` | ||
mutation addCustomerAddress($input: AddCustomerAddressInput!, $reCaptchaV2: ReCaptchaV2Input) { | ||
customer { | ||
addCustomerAddress(input: $input, reCaptchaV2: $reCaptchaV2) { | ||
errors { | ||
... on CustomerAddressCreationError { | ||
message | ||
} | ||
... on CustomerNotLoggedInError { | ||
message | ||
} | ||
... on ValidationError { | ||
message | ||
path | ||
} | ||
} | ||
address { | ||
entityId | ||
firstName | ||
lastName | ||
} | ||
} | ||
} | ||
} | ||
`); | ||
|
||
type AddCustomerAddressInput = VariablesOf<typeof ADD_CUSTOMER_ADDRESS_MUTATION>['input']; | ||
|
||
interface AddCustomerAddress { | ||
input: AddCustomerAddressInput; | ||
reCaptchaToken?: string; | ||
} | ||
|
||
export const addCustomerAddress = async ({ input, reCaptchaToken }: AddCustomerAddress) => { | ||
const customerId = await getSessionCustomerId(); | ||
|
||
const response = await client.fetch({ | ||
document: ADD_CUSTOMER_ADDRESS_MUTATION, | ||
customerId, | ||
fetchOptions: { cache: 'no-store' }, | ||
variables: { | ||
input, | ||
...(reCaptchaToken && { reCaptchaV2: { token: reCaptchaToken } }), | ||
}, | ||
}); | ||
|
||
return response.data.customer.addCustomerAddress; | ||
}; |