Skip to content

Commit

Permalink
feat(core): add custom password field and text field for update setti…
Browse files Browse the repository at this point in the history
…ngs form
  • Loading branch information
bc-alexsaiannyi committed Aug 7, 2024
1 parent 818559d commit 1ef848c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,16 @@ const createRadioButtonsValidationHandler =

const createDatesValidationHandler =
(dateStateSetter: FieldStateSetFn<FieldState>, dateFieldState: FieldState) =>
(e: React.ChangeEvent<HTMLInputElement>) => {
(e: React.ChangeEvent<HTMLInputElement> & { nativeEvent: { inputType: string } }) => {
const fieldId = Number(e.target.id.split('-')[2]);
const validationStatus = e.target.validity.valueMissing;

// cancel validation on attempt to delete input content via keyboard since
// DayPicker won't allow the user to unselect the selected date when it's required
if (e.nativeEvent.inputType === 'deleteContentBackward') {
return;
}

dateStateSetter({ ...dateFieldState, [fieldId]: !validationStatus });
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getPreviouslySubmittedValue,
MultilineText,
NumbersOnly,
Password,
Picklist,
RadioButtons,
} from '~/app/[locale]/(default)/login/register-customer/_components/register-customer-form/fields';
Expand All @@ -26,9 +27,11 @@ import {
createDatesValidationHandler,
createMultilineTextValidationHandler,
createNumbersInputValidationHandler,
createPasswordValidationHandler,
createPreSubmitCheckboxesValidationHandler,
createPreSubmitPicklistValidationHandler,
createRadioButtonsValidationHandler,
createTextInputValidationHandler,
} from '../addresses-content/address-field-handlers';

import { updateCustomer } from './_actions/update-customer';
Expand Down Expand Up @@ -115,6 +118,7 @@ export const UpdateSettingsForm = ({
const [picklistValid, setPicklistValid] = useState<Record<string, boolean>>({});
const [checkboxesValid, setCheckboxesValid] = useState<Record<string, boolean>>({});
const [datesValid, setDatesValid] = useState<Record<string, boolean>>({});
const [passwordValid, setPasswordValid] = useState<Record<string, boolean>>({});

const reCaptchaRef = useRef<ReCaptcha>(null);
const [reCaptchaToken, setReCaptchaToken] = useState('');
Expand Down Expand Up @@ -151,6 +155,14 @@ export const UpdateSettingsForm = ({
customerFields,
setCheckboxesValid,
);
const handlePasswordValidation = createPasswordValidationHandler(
setPasswordValid,
customerFields,
);
const handleCustomTextValidation = createTextInputValidationHandler(
setTextInputValid,
textInputValid,
);
const preSubmitFieldsValidation = (
e: MouseEvent<HTMLFormElement> & { target: HTMLButtonElement },
) => {
Expand Down Expand Up @@ -369,20 +381,37 @@ export const UpdateSettingsForm = ({
<FieldWrapper fieldId={fieldId} key={fieldId}>
<Text
defaultValue={submittedValue}
entityId={FieldNameToFieldId.email}
entityId={fieldId}
isRequired={field.isRequired}
isValid={textInputValid[fieldId]}
label={
customerFields.find(({ entityId: id }) => id === fieldId)?.label ?? ''
}
name={fieldName}
onChange={handleTextInputValidation}
onChange={handleCustomTextValidation}
type="text"
/>
</FieldWrapper>
);
}

case 'PasswordFormField': {
const submittedValue =
getPreviouslySubmittedValue(previouslySubmittedField).PasswordFormField;

return (
<FieldWrapper fieldId={fieldId} key={fieldId}>
<Password
defaultValue={submittedValue}
field={field}
isValid={passwordValid[fieldId]}
name={fieldName}
onChange={handlePasswordValidation}
/>
</FieldWrapper>
);
}

default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface DateProps {
defaultValue?: Date | string;
field: DateType;
isValid?: boolean;
onChange?: (e: ChangeEvent<HTMLInputElement>) => void;
onChange?: (e: ChangeEvent<HTMLInputElement> & { nativeEvent: { inputType: string } }) => void;
onValidate?: (
state:
| Record<string, boolean>
Expand Down

0 comments on commit 1ef848c

Please sign in to comment.