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

Suisin/82332/ts migration for document upload #52

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import DocumentsUpload from './documents-upload.jsx';
import DocumentsUpload from './documents-upload';
import SelfieUpload from './selfie-upload';
import { SELFIE_DOCUMENT } from '../constants';
import './card-details.scss';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import { Formik, Form } from 'formik';
import { Formik, Form, FormikValues } from 'formik';
import { localize } from '@deriv/translations';
import { isMobile } from '@deriv/shared';
import { Button, Icon, Text } from '@deriv/components';
Expand Down Expand Up @@ -29,7 +28,24 @@ const icons = [
},
];

const IconsItem = ({ data }) => (
type TDocumentsUpload = {
initial_values: FormikValues;
is_from_external: boolean;
goToCards: () => void;
onSubmit: () => void;
};

type TIconsItem = {
data: {
icon: string;
text: string;
fields?: FormikValues[];
documents_title?: string;
documents?: FormikValues[];
};
};

const IconsItem = ({ data }: TIconsItem) => (
<div className={`${ROOT_CLASS}__icons-item`}>
<Icon icon={data.icon} size={24} />
<Text as='p' size='xxxs' align='center'>
Expand All @@ -38,11 +54,17 @@ const IconsItem = ({ data }) => (
</div>
);

const DocumentsUpload = ({ initial_values, is_from_external, data, goToCards, onSubmit }) => {
const DocumentsUpload = ({
initial_values,
is_from_external,
data,
goToCards,
onSubmit,
}: TDocumentsUpload & TIconsItem) => {
const { fields, documents_title, documents } = data;

const fields_title = localize('First, enter your {{label}} and the expiry date.', {
label: fields[0].label,
label: fields?.[0].label,
});

return (
Expand All @@ -52,14 +74,14 @@ const DocumentsUpload = ({ initial_values, is_from_external, data, goToCards, on
})}
>
<Formik
initialValues={initial_values || setInitialValues([...fields, ...documents])}
initialValues={initial_values || setInitialValues([...(fields || []), ...(documents || [])])}
validate={values => validateFields(values, fields, documents)}
onSubmit={onSubmit}
>
{({ values, isValid, touched }) => {
{({ values, isValid, touched }: FormikValues) => {
const is_form_touched = Object.keys(touched).length > 0;
const is_form_empty = Object.values(values).some(
(field, key) => (field === null || field === '') && fields[key]?.required
(field, key) => (field === null || field === '') && fields?.[key]?.required
);

return (
Expand All @@ -69,7 +91,7 @@ const DocumentsUpload = ({ initial_values, is_from_external, data, goToCards, on
{fields_title}
</Text>
<div className={`${ROOT_CLASS}__fields-wrap`}>
{fields.map(field => (
{fields?.map((field: FormikValues) => (
<InputField key={field.name} data={field} />
))}
</div>
Expand All @@ -78,7 +100,7 @@ const DocumentsUpload = ({ initial_values, is_from_external, data, goToCards, on
{documents_title}
</Text>
<div className={`${ROOT_CLASS}__uploaders-wrap`}>
{documents.map(item => (
{documents?.map((item: FormikValues) => (
<Uploader
key={item.name}
data={item}
Expand Down Expand Up @@ -117,10 +139,4 @@ const DocumentsUpload = ({ initial_values, is_from_external, data, goToCards, on
);
};

DocumentsUpload.propTypes = {
initial_values: PropTypes.object,
data: PropTypes.object,
goToCards: PropTypes.func,
onSubmit: PropTypes.func,
};
export default DocumentsUpload;