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/82336/ts migration for uploader #66

Closed
Show file tree
Hide file tree
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
Expand Up @@ -4,7 +4,7 @@ import classNames from 'classnames';
import { Icon, Text } from '@deriv/components';
import { PlatformContext, isDesktop, WS } from '@deriv/shared';
import { Localize, localize } from '@deriv/translations';
import FileUploader from './file-uploader.jsx';
import FileUploader from './file-uploader';

const FileProperties = () => {
const properties = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { localize } from '@deriv/translations';
import { isMobile } from '@deriv/shared';
import { Button, Icon, Text } from '@deriv/components';
import InputField from './input-field';
import Uploader from './uploader.jsx';
import Uploader from './uploader';
import { setInitialValues, validateFields } from './utils';
import { ROOT_CLASS } from '../constants';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Formik, Form, FormikProps, FormikValues } from 'formik';
import { localize } from '@deriv/translations';
import { isMobile } from '@deriv/shared';
import { Button, Icon, Text } from '@deriv/components';
import Uploader from './uploader.jsx';
import Uploader from './uploader';
import { setInitialValues, validateFields } from './utils';
import { ROOT_CLASS, SELFIE_DOCUMENT } from '../constants';

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, { string } from 'prop-types';
import { Field } from 'formik';
import { Field, FormikProps, FormikValues } from 'formik';
import { localize } from '@deriv/translations';
import { isMobile, supported_filetypes, max_document_size } from '@deriv/shared';
import { Button, Icon, Text, FileDropzone } from '@deriv/components';
Expand All @@ -13,8 +12,24 @@ const DROPZONE_ERRORS = {
'too-many-files': localize('Please select one file only'),
GENERAL: localize('Sorry, an error occured. Please select another file.'),
};
type TDROPZONE_ERRORS = Readonly<{
'file-too-large': string;
'file-invalid-type': string;
'too-many-files': string;
GENERAL: string;
}>;

const Message = ({ data, open }) => (
type TUploader = {
data: FormikValues;
value: FormikValues;
is_full: boolean;
has_frame: boolean;
onChange: (e: unknown) => void;
setFieldValue: FormikProps<FormikValues>['setFieldValue'];
handleChange: (file: object | null, setFieldValue?: FormikProps<FormikValues>['setFieldValue']) => void;
};

const Message = ({ data, open }: FormikValues) => (
<div className={`${ROOT_CLASS}__uploader-details`}>
<Icon className={`${ROOT_CLASS}__uploader-icon`} icon={data.icon} size={236} />
<Text as='p' size='xs' color='general' align='center'>
Expand All @@ -29,11 +44,11 @@ const Message = ({ data, open }) => (
</div>
);

const Preview = ({ data, setFieldValue, value, has_frame, handleChange }) => {
const [background_url, setBackgroundUrl] = React.useState();
const Preview = ({ data, setFieldValue, value, has_frame, handleChange }: Partial<TUploader>) => {
const [background_url, setBackgroundUrl] = React.useState<string>();

React.useEffect(() => {
setBackgroundUrl(value.file ? URL.createObjectURL(value.file) : '');
setBackgroundUrl(value?.file ? URL.createObjectURL(value?.file) : '');
}, [value]);

return (
Expand All @@ -45,58 +60,62 @@ const Preview = ({ data, setFieldValue, value, has_frame, handleChange }) => {
style={{ backgroundImage: `url(${background_url})` }}
>
{has_frame && <Icon icon='IcPoiFrame' className={`${ROOT_CLASS}__uploader-frame`} />}
{(!background_url || value.file.type.indexOf('pdf') !== -1) && (
{(!background_url || value?.file.type.indexOf('pdf') !== -1) && (
<React.Fragment>
<Icon icon='IcCloudUpload' size={50} />
<Text as='p' size='xs' color='general' align='center'>
{value.file.name}
{value?.file.name}
</Text>
</React.Fragment>
)}
<Icon
icon='IcCloseCircle'
className={`${ROOT_CLASS}__uploader-remove`}
onClick={() => handleChange(null, setFieldValue)}
onClick={() => {
handleChange?.(null, setFieldValue);
}}
size={16}
/>
</div>
<Text as='p' size='xs' color='general' align='center'>
{data.info}
{data?.info}
</Text>
</div>
);
};

const Uploader = ({ data, value, is_full, onChange, has_frame }) => {
const [image, setImage] = React.useState();
const Uploader = ({ data, value, is_full, onChange, has_frame }: Partial<TUploader>) => {
Copy link

@jim-deriv jim-deriv Dec 22, 2022

Choose a reason for hiding this comment

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

@suisin-deriv, this is just for future reference, you can use "Pick" to pick out only the props that you want, instead of using "Partial". That is unless you want the props themselves to be undefined. But this is OK as well

const [image, setImage] = React.useState<{ errors?: [] }>();

React.useEffect(() => {
setImage(value);
}, [value]);

const handleChange = (file, setFieldValue) => {
const handleChange = (file?: object, setFieldValue?: FormikProps<FormikValues>['setFieldValue']) => {
if (onChange && typeof onChange === 'function') {
onChange(file);
}
setFieldValue(data.name, file);
setFieldValue?.(data?.name, file);
};

const handleAccept = (files, setFieldValue) => {
const handleAccept = (files: object[], setFieldValue: () => void) => {
const file = { file: files[0], errors: [], ...data };
handleChange(file, setFieldValue);
};

const handleReject = (files, setFieldValue) => {
const errors = files[0].errors.map(error =>
DROPZONE_ERRORS[error.code] ? DROPZONE_ERRORS[error.code] : DROPZONE_ERRORS.GENERAL
const handleReject = (files: Array<{ errors?: [] }>, setFieldValue: () => void) => {
const errors = files[0].errors?.map((error: { code: string }) =>
DROPZONE_ERRORS[error.code as keyof TDROPZONE_ERRORS]
? DROPZONE_ERRORS[error.code as keyof TDROPZONE_ERRORS]
: DROPZONE_ERRORS.GENERAL
);
const file = { ...files[0], errors, ...data };
handleChange(file, setFieldValue);
};

const ValidationErrorMessage = open => (
const ValidationErrorMessage = (open: () => void) => (
<div className={`${ROOT_CLASS}__uploader-details`}>
{image.errors.map((error, index) => (
{image?.errors?.map((error: string, index: number) => (
<Text key={index} as='p' size='xs' color='secondary' align='center'>
{error}
</Text>
Expand All @@ -111,8 +130,8 @@ const Uploader = ({ data, value, is_full, onChange, has_frame }) => {
);

return (
<Field name={data.name}>
{({ form: { setFieldValue } }) => (
<Field name={data?.name}>
{({ form: { setFieldValue } }: FormikValues) => (
<div
className={classNames(`${ROOT_CLASS}__uploader`, {
[`${ROOT_CLASS}__uploader--full`]: is_full,
Expand All @@ -124,21 +143,21 @@ const Uploader = ({ data, value, is_full, onChange, has_frame }) => {
filename_limit={32}
hover_message={localize('Drop files here..')}
max_size={max_document_size}
message={open => <Message open={open} data={data} />}
message={(open: FormikValues) => <Message open={open} data={data} />}
preview_single={
image && (
<Preview
data={data}
value={image}
has_frame={has_frame}
setFieldValue={setFieldValue}
handleChange={handleChange}
handleChange={() => handleChange()}
/>
)
}
multiple={false}
onDropAccepted={files => handleAccept(files, setFieldValue)}
onDropRejected={files => handleReject(files, setFieldValue)}
onDropAccepted={(files: object[]) => handleAccept(files, setFieldValue)}
onDropRejected={(files: Array<object>) => handleReject(files, setFieldValue)}
validation_error_message={value?.errors?.length ? ValidationErrorMessage : null}
noClick
value={image ? [image] : []}
Expand All @@ -149,11 +168,4 @@ const Uploader = ({ data, value, is_full, onChange, has_frame }) => {
);
};

Uploader.propTypes = {
data: PropTypes.object,
value: PropTypes.oneOfType([PropTypes.object, PropTypes, string]),
is_full: PropTypes.bool,
has_frame: PropTypes.bool,
onChange: PropTypes.func,
};
export default Uploader;