Skip to content

Commit

Permalink
Merge branch 'develop' into rugh/talon-pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
dpatil-magento authored Oct 1, 2019
2 parents 4c07037 + 8c645c7 commit cdf2933
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 84 deletions.
8 changes: 4 additions & 4 deletions packages/peregrine/lib/store/reducers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const initialState = {
},
createAccountError: null,
getDetailsError: null,
isCreatingAccount: null,
isGettingDetails: null,
isResettingPassword: null,
isCreatingAccount: false,
isGettingDetails: false,
isResettingPassword: false,
isSignedIn: isSignedIn(),
isSigningIn: null,
isSigningIn: false,
resetPasswordError: null,
signInError: null
};
Expand Down
38 changes: 38 additions & 0 deletions packages/peregrine/lib/talons/CreateAccount/useCreateAccount.js
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
};
};
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
};
};
35 changes: 11 additions & 24 deletions packages/venia-ui/lib/components/CreateAccount/createAccount.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo } from 'react';
import React from 'react';
import { Redirect } from '@magento/venia-drivers';
import { func, shape, string } from 'prop-types';
import { Form } from 'informed';
Expand All @@ -17,28 +17,17 @@ import {
hasLengthAtLeast
} from '../../util/formValidators';
import defaultClasses from './createAccount.css';
import { useUserContext } from '@magento/peregrine/lib/context/user';
import { useCreateAccount } from '@magento/peregrine/lib/talons/CreateAccount/useCreateAccount';

const LEAD =
'Check out faster, use multiple addresses, track orders and more by creating an account!';

const CreateAccount = props => {
const { initialValues = {}, onSubmit } = props;
const talonProps = useCreateAccount({
initialValues: props.initialValues
});

const [
{ createAccountError, isCreatingAccount, isSignedIn }
] = useUserContext();
const hasError = !!createAccountError;

const classes = mergeClasses(defaultClasses, props.classes);
const sanitizedInitialValues = useMemo(() => {
const { email, firstName, lastName, ...rest } = initialValues;

return {
customer: { email, firstname: firstName, lastname: lastName },
...rest
};
}, [initialValues]);
const { hasError, isDisabled, isSignedIn, initialValues } = talonProps;

const errorMessage = hasError
? 'An error occurred. Please try again.'
Expand All @@ -48,11 +37,13 @@ const CreateAccount = props => {
return <Redirect to="/" />;
}

const classes = mergeClasses(defaultClasses, props.classes);

return (
<Form
className={classes.root}
initialValues={sanitizedInitialValues}
onSubmit={onSubmit}
initialValues={initialValues}
onSubmit={props.onSubmit}
>
<p className={classes.lead}>{LEAD}</p>
<Field label="First Name" required={true}>
Expand Down Expand Up @@ -108,11 +99,7 @@ const CreateAccount = props => {
</div>
<div className={classes.error}>{errorMessage}</div>
<div className={classes.actions}>
<Button
disabled={isCreatingAccount}
type="submit"
priority="high"
>
<Button disabled={isDisabled} type="submit" priority="high">
{'Submit'}
</Button>
</div>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
import React, { useCallback, useMemo } from 'react';
import React from 'react';
import { shape } from 'prop-types';
import { withRouter } from '@magento/venia-drivers';
import { compose } from 'redux';
import CreateAccountForm from '../CreateAccount';
import { mergeClasses } from '../../classify';
import defaultClasses from './createAccountPage.css';
import { getCreateAccountInitialValues } from './helpers';
import { useUserContext } from '@magento/peregrine/lib/context/user';
import { useCreateAccountPage } from '@magento/peregrine/lib/talons/CreateAccountPage/useCreateAccountPage';

const CreateAccountPage = props => {
const [, { createAccount }] = useUserContext();
const classes = mergeClasses(defaultClasses, props.classes);
const { history } = props;
const talonProps = useCreateAccountPage({
history: props.history
});

const handleCreateAccount = useCallback(
async accountInfo => {
await createAccount(accountInfo);
history.push('/');
},
[createAccount, history]
);

const initialValues = useMemo(
() => getCreateAccountInitialValues(window.location.search),
[]
);
const { initialValues, handleCreateAccount } = talonProps;

const classes = mergeClasses(defaultClasses, props.classes);
return (
<div className={classes.container}>
<CreateAccountForm
Expand Down
10 changes: 0 additions & 10 deletions packages/venia-ui/lib/components/CreateAccountPage/helpers.js

This file was deleted.

0 comments on commit cdf2933

Please # to comment.