From 8caa837a84dc10129a36516f6f4fca809a79d68f Mon Sep 17 00:00:00 2001 From: Alexander Saiannyi Date: Wed, 17 Apr 2024 16:51:40 +0200 Subject: [PATCH] feat(core): add customer address mutation --- .changeset/many-parrots-lay.md | 5 ++ .../client/mutations/add-customer-address.ts | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 .changeset/many-parrots-lay.md create mode 100644 apps/core/client/mutations/add-customer-address.ts diff --git a/.changeset/many-parrots-lay.md b/.changeset/many-parrots-lay.md new file mode 100644 index 0000000000..67ca16bf11 --- /dev/null +++ b/.changeset/many-parrots-lay.md @@ -0,0 +1,5 @@ +--- +"@bigcommerce/catalyst-core": patch +--- + +Add customer address mutation diff --git a/apps/core/client/mutations/add-customer-address.ts b/apps/core/client/mutations/add-customer-address.ts new file mode 100644 index 0000000000..c657956e8e --- /dev/null +++ b/apps/core/client/mutations/add-customer-address.ts @@ -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['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; +};