Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TD-768: Fix terminal form instant payment state #246

Merged
merged 1 commit into from
Oct 11, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useEffect, useState } from 'react';
import { useContext, useEffect, useMemo, useState } from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';

import { getMetadata, MetadataField, MetadataLogo, MetadataSelect } from 'checkout/components/ui';
Expand Down Expand Up @@ -43,15 +43,17 @@ const PaymentTerminalForm = ({ onMount }: { onMount: () => void }) => {
});
const [formTouched, setFormTouched] = useState(false);

const isInstantPayment = useMemo(() => !formTouched && isValid, [isValid, formTouched]);

useEffect(() => {
onMount();
}, []);

useEffect(() => {
if (!formTouched && isValid) {
if (isInstantPayment && createPaymentState.status === 'PRISTINE') {
onSubmit(getValues());
}
}, [isValid, formTouched]);
}, [isInstantPayment]);

useEffect(() => {
if (isSubmitted && !isEmptyObject(errors)) {
Expand Down Expand Up @@ -87,65 +89,69 @@ const PaymentTerminalForm = ({ onMount }: { onMount: () => void }) => {
};

return (
<form onClick={() => setFormTouched(true)} onSubmit={handleSubmit(onSubmit)}>
<Container>
<div>
<Header title={serviceProvider?.brandName} />
{logo && (
<LogoContainer>
<MetadataLogo metadata={logo} />
</LogoContainer>
)}
{form &&
form?.sort(sortByIndex).map((m) => (
<FormGroup key={m.name} direction={'column'}>
{m.type === 'select' && (
<MetadataSelect
fieldError={errors?.metadata?.[m.name]}
isDirty={dirtyFields?.metadata?.[m.name]}
localeCode={initConfig.locale}
metadata={m}
<>
{!isInstantPayment && (
<form onClick={() => setFormTouched(true)} onSubmit={handleSubmit(onSubmit)}>
<Container>
<div>
<Header title={serviceProvider?.brandName} />
{logo && (
<LogoContainer>
<MetadataLogo metadata={logo} />
</LogoContainer>
)}
{form &&
form?.sort(sortByIndex).map((m) => (
<FormGroup key={m.name} direction={'column'}>
{m.type === 'select' && (
<MetadataSelect
fieldError={errors?.metadata?.[m.name]}
isDirty={dirtyFields?.metadata?.[m.name]}
localeCode={initConfig.locale}
metadata={m}
register={register}
wrappedName="metadata"
/>
)}
{m.type !== 'select' && (
<MetadataField
fieldError={errors?.metadata?.[m.name]}
isDirty={dirtyFields?.metadata?.[m.name]}
localeCode={initConfig.locale}
metadata={m}
register={register}
wrappedName="metadata"
/>
)}
{m?.addon === 'vpa' && <VpaInstruction locale={locale} />}
</FormGroup>
))}
{contactInfo?.email && (
<FormGroup>
<Email
fieldError={errors.email}
isDirty={dirtyFields.email}
locale={locale}
register={register}
wrappedName="metadata"
/>
)}
{m.type !== 'select' && (
<MetadataField
fieldError={errors?.metadata?.[m.name]}
isDirty={dirtyFields?.metadata?.[m.name]}
localeCode={initConfig.locale}
metadata={m}
</FormGroup>
)}
{contactInfo?.phoneNumber && (
<FormGroup>
<Phone
fieldError={errors.phoneNumber}
isDirty={dirtyFields.email}
locale={locale}
register={register}
wrappedName="metadata"
/>
)}
{m?.addon === 'vpa' && <VpaInstruction locale={locale} />}
</FormGroup>
))}
{contactInfo?.email && (
<FormGroup>
<Email
fieldError={errors.email}
isDirty={dirtyFields.email}
locale={locale}
register={register}
/>
</FormGroup>
)}
{contactInfo?.phoneNumber && (
<FormGroup>
<Phone
fieldError={errors.phoneNumber}
isDirty={dirtyFields.email}
locale={locale}
register={register}
/>
</FormGroup>
)}
</div>
<PayButton />
</Container>
</form>
</FormGroup>
)}
</div>
<PayButton />
</Container>
</form>
)}
</>
);
};

Expand Down