diff --git a/.changeset/green-pets-sneeze.md b/.changeset/green-pets-sneeze.md new file mode 100644 index 0000000000..d6e0ddd038 --- /dev/null +++ b/.changeset/green-pets-sneeze.md @@ -0,0 +1,5 @@ +--- +"@bigcommerce/catalyst-core": patch +--- + +Add delete customer address mutation diff --git a/apps/core/client/mutations/delete-customer-address.ts b/apps/core/client/mutations/delete-customer-address.ts new file mode 100644 index 0000000000..bc42cb7cf0 --- /dev/null +++ b/apps/core/client/mutations/delete-customer-address.ts @@ -0,0 +1,47 @@ +import { getSessionCustomerId } from '~/auth'; + +import { client } from '..'; +import { graphql, VariablesOf } from '../graphql'; + +const DELETE_CUSTOMER_ADDRESS_MUTATION = graphql(` + mutation DeleteCustomerAddress( + $reCaptcha: ReCaptchaV2Input + $input: DeleteCustomerAddressInput! + ) { + customer { + deleteCustomerAddress(reCaptchaV2: $reCaptcha, input: $input) { + errors { + __typename + ... on CustomerAddressDeletionError { + __typename + message + } + ... on CustomerNotLoggedInError { + __typename + message + } + } + } + } + } +`); + +type AddressId = VariablesOf['input']['addressEntityId']; + +export const deleteCustomerAddress = async (addressId: AddressId, reCaptchaToken?: string) => { + const customerId = await getSessionCustomerId(); + + const response = await client.fetch({ + document: DELETE_CUSTOMER_ADDRESS_MUTATION, + customerId, + fetchOptions: { cache: 'no-store' }, + variables: { + input: { + addressEntityId: addressId, + }, + ...(reCaptchaToken && { reCaptcha: { token: reCaptchaToken } }), + }, + }); + + return response.data; +};