-
Notifications
You must be signed in to change notification settings - Fork 100
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 update customer mutation (#776)
- Loading branch information
1 parent
bfde483
commit 656693e
Showing
2 changed files
with
61 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 update customer 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,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<typeof UPDATE_CUSTOMER_MUTATION>; | ||
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; | ||
}; |