-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into rugh/talon-pagination
- Loading branch information
Showing
7 changed files
with
106 additions
and
84 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
38 changes: 38 additions & 0 deletions
38
packages/peregrine/lib/talons/CreateAccount/useCreateAccount.js
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,38 @@ | ||
import { useMemo } from 'react'; | ||
import { useUserContext } from '@magento/peregrine/lib/context/user'; | ||
|
||
/** | ||
* Returns props necessary to render CreateAccount component. | ||
* | ||
* @param {Object} props.initialValues initial values to sanitize and seed the form | ||
* @returns {{ | ||
* hasError: boolean, | ||
* isDisabled: boolean, | ||
* isSignedIn: boolean, | ||
* initialValues: object | ||
* }} | ||
*/ | ||
export const useCreateAccount = props => { | ||
const { initialValues = {} } = props; | ||
|
||
const [ | ||
{ createAccountError, isCreatingAccount, isSignedIn } | ||
] = useUserContext(); | ||
const hasError = !!createAccountError; | ||
|
||
const sanitizedInitialValues = useMemo(() => { | ||
const { email, firstName, lastName, ...rest } = initialValues; | ||
|
||
return { | ||
customer: { email, firstname: firstName, lastname: lastName }, | ||
...rest | ||
}; | ||
}, [initialValues]); | ||
|
||
return { | ||
hasError, | ||
isDisabled: isCreatingAccount, | ||
isSignedIn, | ||
initialValues: sanitizedInitialValues | ||
}; | ||
}; |
46 changes: 46 additions & 0 deletions
46
packages/peregrine/lib/talons/CreateAccountPage/useCreateAccountPage.js
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,46 @@ | ||
import { useCallback, useMemo } from 'react'; | ||
import { useUserContext } from '@magento/peregrine/lib/context/user'; | ||
|
||
const validCreateAccountParams = ['email', 'firstName', 'lastName']; | ||
|
||
const getCreateAccountInitialValues = search => { | ||
const params = new URLSearchParams(search); | ||
|
||
return validCreateAccountParams.reduce( | ||
(values, param) => ({ ...values, [param]: params.get(param) }), | ||
{} | ||
); | ||
}; | ||
|
||
/** | ||
* Returns props necessary to render CreateAccountPage component. | ||
* | ||
* @param {Object} props.history router history object | ||
* @returns {{ | ||
* handleCreateAccount: function, | ||
* initialValues: object | ||
* }} | ||
*/ | ||
export const useCreateAccountPage = props => { | ||
const [, { createAccount }] = useUserContext(); | ||
// TODO replace with useHistory in React Router 5.1 | ||
const { history } = props; | ||
|
||
const handleCreateAccount = useCallback( | ||
async accountInfo => { | ||
await createAccount(accountInfo); | ||
history.push('/'); | ||
}, | ||
[createAccount, history] | ||
); | ||
|
||
const initialValues = useMemo( | ||
() => getCreateAccountInitialValues(window.location.search), | ||
[] | ||
); | ||
|
||
return { | ||
handleCreateAccount, | ||
initialValues | ||
}; | ||
}; |
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
28 changes: 0 additions & 28 deletions
28
packages/venia-ui/lib/components/CreateAccountPage/__tests__/helpers.spec.js
This file was deleted.
Oops, something went wrong.
25 changes: 7 additions & 18 deletions
25
packages/venia-ui/lib/components/CreateAccountPage/createAccountPage.js
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
10 changes: 0 additions & 10 deletions
10
packages/venia-ui/lib/components/CreateAccountPage/helpers.js
This file was deleted.
Oops, something went wrong.