Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 84 additions & 54 deletions frontend/public/src/components/forms/ProfileFormFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,70 @@ const findStates = (country: string, countries: Array<Country>) => {
null
}

const renderFirstNameField = errors => {
const hasError =
errors && errors.legal_address && errors.legal_address.first_name
return (
<div>
<CardLabel
htmlFor="legal_address.first_name"
isRequired={true}
label="First Name"
subLabel="Name that will appear on emails"
/>
<Field
type="text"
name="legal_address.first_name"
id="legal_address.first_name"
className="form-control"
autoComplete="given-name"
aria-invalid={hasError ? "true" : null}
aria-describedby={hasError ? "first-name-error" : null}
aria-description="Name cannot start with, or contain, a special character"
title="Name cannot start with, or contain, a special character."
required
/>
<ErrorMessage
id="first-name-error"
name="legal_address.first_name"
component={FormError}
/>
</div>
)
}

const renderLastNameField = errors => {
const hasError =
errors && errors.legal_address && errors.legal_address.last_name
return (
<div>
<CardLabel
htmlFor="legal_address.last_name"
isRequired={true}
label="Last Name"
subLabel="Name that will appear on emails"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this subLabel is correct. Can we just omit it, like we do on https://mitxonline.mit.edu/create-profile/

/>
<Field
type="text"
name="legal_address.last_name"
id="legal_address.last_name"
className="form-control"
autoComplete="family-name"
aria-invalid={hasError ? "true" : null}
aria-describedby={hasError ? "last-name-error" : null}
aria-description="Name cannot start with, or contain, a special character"
title="Name cannot start with, or contain, a special character."
required
/>
<ErrorMessage
id="last-name-error"
name="legal_address.last_name"
component={FormError}
/>
</div>
)
}

const renderYearOfBirthField = errors => {
const hasError =
errors && errors.user_profile && errors.user_profile.year_of_birth
Expand Down Expand Up @@ -202,8 +266,20 @@ export const LegalAddressCountryFields = ({
values.legal_address.country === "CA") &&
!values.legal_address.state)
)
const [showFirstNameField, setShowFirstNameField] = React.useState(
values.legal_address.first_name === ""
)
const [showLastNameField, setShowLastNameField] = React.useState(
values.legal_address.last_name === ""
)

React.useEffect(() => {
if (values.legal_address.first_name === "") {
setShowFirstNameField(true)
}
if (values.legal_address.last_name === "") {
setShowLastNameField(true)
}
if (values.user_profile.year_of_birth === "") {
setShowYearOfBirthField(true)
}
Expand All @@ -219,6 +295,12 @@ export const LegalAddressCountryFields = ({

return (
<React.Fragment>
{showFirstNameField ? (
<div className="form-group">{renderFirstNameField(errors)}</div>
) : null}
{showLastNameField ? (
<div className="form-group">{renderLastNameField(errors)}</div>
) : null}
{showYearOfBirthField ? (
<div className="form-group">{renderYearOfBirthField(errors)}</div>
) : null}
Expand Down Expand Up @@ -316,60 +398,8 @@ export const LegalAddressFields = ({
const addressErrors = errors && errors.legal_address
return (
<React.Fragment>
<div className="form-group">
<CardLabel
htmlFor="legal_address.first_name"
isRequired={true}
label="First Name"
subLabel="Name that will appear on emails"
/>
<Field
type="text"
name="legal_address.first_name"
id="legal_address.first_name"
className="form-control"
autoComplete="given-name"
aria-invalid={
addressErrors && addressErrors.first_name ? "true" : null
}
aria-describedby={
addressErrors && addressErrors.first_name ?
"first-name-error" :
null
}
aria-description="Name cannot start with, or contain, a special character"
title="Name cannot start with, or contain, a special character."
required
/>
<ErrorMessage
id="first-name-error"
name="legal_address.first_name"
component={FormError}
/>
</div>
<div className="form-group">
<CardLabel
htmlFor="legal_address.last_name"
isRequired={true}
label="Last Name"
/>
<Field
type="text"
name="legal_address.last_name"
id="legal_address.last_name"
className="form-control"
autoComplete="family-name"
aria-invalid={
addressErrors && addressErrors.last_name ? "true" : null
}
aria-describedby={
addressErrors && addressErrors.last_name ? "last-name-error" : null
}
aria-description="Name cannot start with, or contain, a special character"
required
/>
<ErrorMessage name="legal_address.last_name" component={FormError} />
</div>
<div className="form-group">{renderFirstNameField(errors)}</div>
<div className="form-group">{renderLastNameField(errors)}</div>
<div className="form-group">
<CardLabel
htmlFor="name"
Expand Down
Loading