-
Notifications
You must be signed in to change notification settings - Fork 687
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[chore?]: Use createCustomer gql mutation instead of create-account REST endpoint #1898
Changes from 9 commits
7004853
1d2705a
e5ec0b2
929dc7b
474c65a
5636f1e
21585ef
7a8f799
4544f8f
df60e02
7868bad
6d5b362
a1e5b8e
5179f5c
71b461b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ export const useAuthModal = props => { | |
} = props; | ||
|
||
const [username, setUsername] = useState(''); | ||
const [{ currentUser }, { createAccount, signOut }] = useUserContext(); | ||
const [{ currentUser }, { signOut }] = useUserContext(); | ||
|
||
// If the user is authed, the only valid view is "MY_ACCOUNT". | ||
// view an also be `MENU` but in that case we don't want to act. | ||
|
@@ -49,13 +49,9 @@ export const useAuthModal = props => { | |
closeDrawer(); | ||
}, [closeDrawer, showMainMenu]); | ||
|
||
const handleCreateAccount = useCallback( | ||
async values => { | ||
await createAccount(values); | ||
showMyAccount(); | ||
}, | ||
[createAccount, showMyAccount] | ||
); | ||
const handleCreateAccount = useCallback(() => { | ||
showMyAccount(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See how this callback no longer cares about the |
||
}, [showMyAccount]); | ||
|
||
const handleSignOut = useCallback(() => { | ||
// TODO: Get history from router context when implemented. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,61 @@ | ||
import { useMemo } from 'react'; | ||
import { useCallback, useMemo, useState } from 'react'; | ||
import { useMutation } from '@apollo/react-hooks'; | ||
import { useUserContext } from '@magento/peregrine/lib/context/user'; | ||
|
||
/** | ||
* Returns props necessary to render CreateAccount component. | ||
* Returns props necessary to render CreateAccount component. In particular this | ||
* talon handles the submission flow by first doing a pre-submisson validation | ||
* and then, on success, invokes the `onSubmit` prop, which is usually the action. | ||
* | ||
* @param {Object} props.initialValues initial values to sanitize and seed the form | ||
* @param {Function} props.onSubmit the post submit callback | ||
* @param {String} query the graphql query for creating the account | ||
* @returns {{ | ||
* hasError: boolean, | ||
* errors: array, | ||
* handleSubmit: function, | ||
* isDisabled: boolean, | ||
* isSignedIn: boolean, | ||
* initialValues: object | ||
* }} | ||
*/ | ||
export const useCreateAccount = props => { | ||
const { initialValues = {} } = props; | ||
const { initialValues = {}, onSubmit, query } = props; | ||
const [isSubmitting, setIsSubmitting] = useState(false); | ||
|
||
const [ | ||
{ createAccountError, isCreatingAccount, isSignedIn } | ||
] = useUserContext(); | ||
const hasError = !!createAccountError; | ||
const [{ isSignedIn }, { signIn }] = useUserContext(); | ||
const [createAccount, { error }] = useMutation(query); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm so glad we didn't have mutations prior to official hooks. 😅 |
||
|
||
const handleSubmit = useCallback( | ||
async formValues => { | ||
setIsSubmitting(true); | ||
try { | ||
// Try to create an account with the mutation. | ||
await createAccount({ | ||
variables: { | ||
email: formValues.customer.email, | ||
firstname: formValues.customer.firstname, | ||
lastname: formValues.customer.lastname, | ||
password: formValues.password | ||
} | ||
}); | ||
|
||
// Then sign the user in. | ||
await signIn({ | ||
username: formValues.customer.email, | ||
password: formValues.password | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic used to exist within the redux action but now lives in the talon! |
||
|
||
// Finally, invoke the post-submission callback. | ||
onSubmit(); | ||
sirugh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (error) { | ||
if (process.env.NODE_ENV === 'development') { | ||
console.error(error); | ||
} | ||
setIsSubmitting(false); | ||
} | ||
}, | ||
[createAccount, onSubmit, signIn] | ||
); | ||
|
||
const sanitizedInitialValues = useMemo(() => { | ||
const { email, firstName, lastName, ...rest } = initialValues; | ||
|
@@ -30,8 +67,9 @@ export const useCreateAccount = props => { | |
}, [initialValues]); | ||
|
||
return { | ||
hasError, | ||
isDisabled: isCreatingAccount, | ||
errors: (error && error.graphQLErrors) || [], | ||
handleSubmit, | ||
isDisabled: isSubmitting, | ||
isSignedIn, | ||
initialValues: sanitizedInitialValues | ||
}; | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,19 +18,31 @@ import { | |
} from '../../util/formValidators'; | ||
import defaultClasses from './createAccount.css'; | ||
import { useCreateAccount } from '@magento/peregrine/lib/talons/CreateAccount/useCreateAccount'; | ||
import CREATE_ACCOUNT_MUTATION from '../../queries/createAccount.graphql'; | ||
|
||
const LEAD = | ||
'Check out faster, use multiple addresses, track orders and more by creating an account!'; | ||
|
||
const CreateAccount = props => { | ||
const talonProps = useCreateAccount({ | ||
initialValues: props.initialValues | ||
initialValues: props.initialValues, | ||
query: CREATE_ACCOUNT_MUTATION, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only because in GraphQL parlance "queries" and "mutations" are two distinctly separate things, this reads funny. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea -- I thought about changing the import name. I don't really think the UI needs to care about whether the query is a mutation/query/subscription. I'd like to get @jimbo's take on naming here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 pending @jimbo 's verdict 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm pretty convinced by @supernova-at's argument here. As long as we have a
That said, this is a reminder that there is no good reason to have a Ideally, queries and mutations would reside in the component folder alongside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I agree. I think we moved them into the How about this compromise - we merge this as is and I open an issue to move all the queries/mutations to the component folders where they are used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made #1928 :D |
||
onSubmit: props.onSubmit | ||
}); | ||
|
||
const { hasError, isDisabled, isSignedIn, initialValues } = talonProps; | ||
const { | ||
errors, | ||
handleSubmit, | ||
isDisabled, | ||
isSignedIn, | ||
initialValues | ||
} = talonProps; | ||
|
||
const errorMessage = hasError | ||
? 'An error occurred. Please try again.' | ||
// Map over any errors we get and display an appropriate error. | ||
const errorMessage = errors.length | ||
? errors | ||
.map(({ message }) => message) | ||
.reduce((acc, msg) => acc + '\n' + msg, '') | ||
sirugh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
: null; | ||
|
||
if (isSignedIn) { | ||
|
@@ -43,7 +55,7 @@ const CreateAccount = props => { | |
<Form | ||
className={classes.root} | ||
initialValues={initialValues} | ||
onSubmit={props.onSubmit} | ||
onSubmit={handleSubmit} | ||
> | ||
<p className={classes.lead}>{LEAD}</p> | ||
<Field label="First Name" required={true}> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It didn't make sense that the
CreateAccount
page took a callback that was just a reference to this action. In all scenarios, it was pulled off context and passed in anyways. I can't imagine a scenario where we would want to change the action passed in to the component. So now, theuseCreateAccount
talon uses thecreateCustomer
mutation (no more REST!) And components that want to use theCreateAccount
form can pass it a "post submit" callback.Bye Felicia!