Created
April 20, 2021 08:27
-
-
Save Frodigo/e106254dc9d30b774475155370f51430 to your computer and use it in GitHub Desktop.
Re-factor Edit shipping to use Dialog [WIP]
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
import React, { Fragment } from 'react'; | |
import { FormattedMessage, useIntl } from 'react-intl'; | |
import { Form } from 'informed'; | |
import { func, shape, string, arrayOf, number } from 'prop-types'; | |
import { useGuestForm } from '@magento/peregrine/lib/talons/CheckoutPage/ShippingInformation/AddressForm/useGuestForm'; | |
import { mergeClasses } from '../../../../classify'; | |
import { isRequired } from '../../../../util/formValidators'; | |
import Button from '../../../Button'; | |
import Country from '../../../Country'; | |
import Field, { Message } from '../../../Field'; | |
import FormError from '../../../FormError'; | |
import Region from '../../../Region'; | |
import Postcode from '../../../Postcode'; | |
import TextInput from '../../../TextInput'; | |
import defaultClasses from './guestForm.css'; | |
import GuestFormOperations from './guestForm.gql'; | |
import Dialog from '../../../Dialog'; | |
const GuestForm = props => { | |
const { | |
afterSubmit, | |
classes: propClasses, | |
onCancel, | |
onSuccess, | |
shippingData | |
} = props; | |
const talonProps = useGuestForm({ | |
afterSubmit, | |
...GuestFormOperations, | |
onCancel, | |
onSuccess, | |
shippingData | |
}); | |
const { | |
errors, | |
handleCancel, | |
handleSubmit, | |
initialValues, | |
isSaving, | |
isUpdate | |
} = talonProps; | |
const { formatMessage } = useIntl(); | |
const classes = mergeClasses(defaultClasses, propClasses); | |
const guestEmailMessage = !isUpdate ? ( | |
<Message> | |
<FormattedMessage | |
id={'guestForm.emailMessage'} | |
defaultMessage={ | |
'Set a password at the end of guest checkout to create an account in one easy step.' | |
} | |
/> | |
</Message> | |
) : null; | |
const cancelButton = isUpdate ? ( | |
<Button disabled={isSaving} onClick={handleCancel} priority="low"> | |
<FormattedMessage | |
id={'global.cancelButton'} | |
defaultMessage={'Cancel'} | |
/> | |
</Button> | |
) : null; | |
const submitButtonText = isUpdate | |
? formatMessage({ | |
id: 'global.updateButton', | |
defaultMessage: 'Update' | |
}) | |
: formatMessage({ | |
id: 'guestForm.continueToNextStep', | |
defaultMessage: 'Continue to Shipping Method' | |
}); | |
const submitButtonProps = { | |
disabled: isSaving, | |
priority: isUpdate ? 'high' : 'normal', | |
type: 'submit' | |
}; | |
return ( | |
<Fragment> | |
<FormError errors={Array.from(errors.values())} /> | |
<Dialog | |
confirmText={'Update'} | |
confirmTranslationId={'global.updateButton'} | |
formProps={initialValues} | |
isOpen={true} | |
onCancel={handleCancel} | |
onConfirm={handleSubmit} | |
shouldDisableAllButtons={isSaving} | |
shouldDisableConfirmButton={isSaving} | |
title={formatMessage({ | |
id: 'checkoutPage.editShippingInfo', | |
defaultMessage: 'Edit Shipping Information' | |
})} | |
> | |
<div className={classes.email}> | |
<Field | |
id="email" | |
label={formatMessage({ | |
id: 'global.email', | |
defaultMessage: 'Email' | |
})} | |
> | |
<TextInput | |
field="email" | |
id="email" | |
validate={isRequired} | |
/> | |
{guestEmailMessage} | |
</Field> | |
</div> | |
<div className={classes.firstname}> | |
<Field | |
id="firstname" | |
label={formatMessage({ | |
id: 'global.firstName', | |
defaultMessage: 'First Name' | |
})} | |
> | |
<TextInput | |
field="firstname" | |
id="firstname" | |
validate={isRequired} | |
/> | |
</Field> | |
</div> | |
<div className={classes.lastname}> | |
<Field | |
id="lastname" | |
label={formatMessage({ | |
id: 'global.lastName', | |
defaultMessage: 'Last Name' | |
})} | |
> | |
<TextInput | |
field="lastname" | |
id="lastname" | |
validate={isRequired} | |
/> | |
</Field> | |
</div> | |
<div className={classes.country}> | |
<Country validate={isRequired} /> | |
</div> | |
<div className={classes.street0}> | |
<Field | |
id="street0" | |
label={formatMessage({ | |
id: 'global.streetAddress', | |
defaultMessage: 'Street Address' | |
})} | |
> | |
<TextInput | |
field="street[0]" | |
id="street0" | |
validate={isRequired} | |
/> | |
</Field> | |
</div> | |
<div className={classes.street1}> | |
<Field | |
id="street1" | |
label={formatMessage({ | |
id: 'global.streetAddress2', | |
defaultMessage: 'Street Address 2' | |
})} | |
optional={true} | |
> | |
<TextInput field="street[1]" id="street1" /> | |
</Field> | |
</div> | |
<div className={classes.city}> | |
<Field | |
id="city" | |
label={formatMessage({ | |
id: 'global.city', | |
defaultMessage: 'City' | |
})} | |
> | |
<TextInput | |
field="city" | |
id="city" | |
validate={isRequired} | |
/> | |
</Field> | |
</div> | |
<div className={classes.region}> | |
<Region | |
validate={isRequired} | |
fieldInput={'region[region]'} | |
fieldSelect={'region[region_id]'} | |
optionValueKey={'id'} | |
/> | |
</div> | |
<div className={classes.postcode}> | |
<Postcode validate={isRequired} /> | |
</div> | |
<div className={classes.telephone}> | |
<Field | |
id="telephone" | |
label={formatMessage({ | |
id: 'global.phoneNumber', | |
defaultMessage: 'Phone Number' | |
})} | |
> | |
<TextInput | |
field="telephone" | |
id="telephone" | |
validate={isRequired} | |
/> | |
</Field> | |
</div> | |
<div className={classes.buttons}> | |
{cancelButton} | |
<Button {...submitButtonProps}>{submitButtonText}</Button> | |
</div> | |
</Dialog> | |
</Fragment> | |
); | |
}; | |
export default GuestForm; | |
GuestForm.defaultProps = { | |
shippingData: { | |
country: { | |
code: 'US' | |
}, | |
region: { | |
code: '' | |
} | |
} | |
}; | |
GuestForm.propTypes = { | |
afterSubmit: func, | |
classes: shape({ | |
root: string, | |
field: string, | |
email: string, | |
firstname: string, | |
lastname: string, | |
country: string, | |
street0: string, | |
street1: string, | |
city: string, | |
region: string, | |
postcode: string, | |
telephone: string, | |
buttons: string, | |
submit: string, | |
submit_update: string | |
}), | |
onCancel: func, | |
onSuccess: func.isRequired, | |
shippingData: shape({ | |
city: string, | |
country: shape({ | |
code: string.isRequired | |
}).isRequired, | |
email: string, | |
firstname: string, | |
lastname: string, | |
postcode: string, | |
region: shape({ | |
region_id: number, | |
region: string | |
}).isRequired, | |
street: arrayOf(string), | |
telephone: string | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment