From 656693ed1ac30a162025b58763fa7beb4dfaad18 Mon Sep 17 00:00:00 2001 From: Yurii Zusik <66325265+yurytut1993@users.noreply.github.com> Date: Thu, 18 Apr 2024 15:37:46 +0300 Subject: [PATCH] feat(core): add update customer mutation (#776) --- .changeset/bright-plants-lick.md | 5 ++ apps/core/client/mutations/update-customer.ts | 56 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 .changeset/bright-plants-lick.md create mode 100644 apps/core/client/mutations/update-customer.ts 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; +};