diff --git a/.changeset/bright-plants-lick.md b/.changeset/bright-plants-lick.md new file mode 100644 index 000000000..ed13272ea --- /dev/null +++ b/.changeset/bright-plants-lick.md @@ -0,0 +1,5 @@ +--- +"@bigcommerce/catalyst-core": patch +--- + +add update customer mutation diff --git a/apps/core/client/mutations/update-customer.ts b/apps/core/client/mutations/update-customer.ts new file mode 100644 index 000000000..d45c78a13 --- /dev/null +++ b/apps/core/client/mutations/update-customer.ts @@ -0,0 +1,56 @@ +import { getSessionCustomerId } from '~/auth'; + +import { client } from '..'; +import { graphql, VariablesOf } from '../graphql'; + +const UPDATE_CUSTOMER_MUTATION = graphql(` + mutation updateCustomer($input: UpdateCustomerInput!) { + customer { + updateCustomer(input: $input) { + customer { + firstName + lastName + } + errors { + __typename + ... on EmailAlreadyInUseError { + message + } + ... on ValidationError { + message + } + ... on CustomerDoesNotExistError { + message + } + ... on CustomerNotLoggedInError { + message + } + } + } + } + } +`); + +type Variables = VariablesOf; +type Input = Variables['input']; + +interface UpdateCustomer { + formFields: Input; + reCaptchaToken?: string; +} + +export const updateCustomer = async ({ formFields, reCaptchaToken }: UpdateCustomer) => { + const customerId = await getSessionCustomerId(); + + const response = await client.fetch({ + document: UPDATE_CUSTOMER_MUTATION, + customerId, + fetchOptions: { cache: 'no-store' }, + variables: { + input: formFields, + ...(reCaptchaToken && { reCaptchaV2: { token: reCaptchaToken } }), + }, + }); + + return response.data.customer.updateCustomer; +};