Skip to content

Commit

Permalink
Clean up FileUploader
Browse files Browse the repository at this point in the history
We remove the unused ImporterValidator. Comment out unused Error section and remove non-necessaru onChange handler
  • Loading branch information
greenscientist authored and tahini committed Dec 18, 2024
1 parent faa8147 commit 39c63e8
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 53 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import React from 'react';
import SocketIOFileClient from 'socket.io-file-client';

import serviceLocator from 'chaire-lib-common/lib/utils/ServiceLocator';
import ImportValidator from 'chaire-lib-common/lib/services/importers/ImporterValidator';

export type FileUploadStatus =
| {
Expand All @@ -32,21 +31,14 @@ export type FileUploadStatus =
export type FileUploaderHOCProps = {
fileImportRef: React.RefObject<HTMLInputElement>;
fileUploader: SocketIOFileClient;
onChange: React.ChangeEventHandler;
validator?: ImportValidator;
uploadStatus: FileUploadStatus;
};

type FileUploaderHOCState = {
validator?: ImportValidator;
interface FileUploaderHOCState {
uploadStatus: FileUploadStatus;
};
}

const fileUploaderHOC = <P,>(
WrappedComponent: React.ComponentType<P>,
importerValidator?: typeof ImportValidator,
autoImport = true
) => {
const fileUploaderHOC = <P,>(WrappedComponent: React.ComponentType<P>, autoImport = true) => {
class FileUploaderHOC extends React.Component<P & FileUploaderHOCProps, FileUploaderHOCState> {
private fileImportRef: React.RefObject<HTMLElement>;
private fileUploader: SocketIOFileClient;
Expand All @@ -55,7 +47,6 @@ const fileUploaderHOC = <P,>(
super(props);

this.state = {
validator: importerValidator ? new importerValidator({}) : undefined,
uploadStatus: { status: 'notUploaded' }
};

Expand All @@ -67,7 +58,6 @@ const fileUploaderHOC = <P,>(
this.onFileUploadComplete = this.onFileUploadComplete.bind(this);
this.onFileUploadError = this.onFileUploadError.bind(this);
this.onFileUploadAbort = this.onFileUploadAbort.bind(this);
this.onChange = this.onChange.bind(this);
}

componentDidMount() {
Expand All @@ -86,14 +76,6 @@ const fileUploaderHOC = <P,>(
this.fileUploader.off('abort', this.onFileUploadAbort);
}

onChange() {
this.setState((state) => {
return {
validator: state.validator
};
});
}

onFileUploadStart(_fileInfo) {
this.setState({ uploadStatus: { status: 'uploading', progress: 0 } });
serviceLocator.eventManager.emit('progress', { name: 'UploadingFile', progress: 0.0 });
Expand Down Expand Up @@ -129,8 +111,6 @@ const fileUploaderHOC = <P,>(
{...this.props}
fileImportRef={this.fileImportRef}
fileUploader={this.fileUploader}
onChange={this.onChange}
validator={this.state.validator}
uploadStatus={this.state.uploadStatus}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,4 @@ class AccessibilityMapBatchForm extends ChangeEventsForm<

// ** File upload
//export default FileUploaderHOC(TransitRoutingForm, null, false);
export default FileUploaderHOC(withTranslation(['transit', 'main'])(AccessibilityMapBatchForm), undefined, false);
export default FileUploaderHOC(withTranslation(['transit', 'main'])(AccessibilityMapBatchForm), false);
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ const ConfigureDemandFromCsvForm: React.FunctionComponent<
);
};

export default FileUploaderHOC(withTranslation(['transit', 'main'])(ConfigureDemandFromCsvForm), undefined, false);
export default FileUploaderHOC(withTranslation(['transit', 'main'])(ConfigureDemandFromCsvForm), false);
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { faUpload } from '@fortawesome/free-solid-svg-icons/faUpload';
import InputFile from 'chaire-lib-frontend/lib/components/input/InputFile';
import Button from 'chaire-lib-frontend/lib/components/input/Button';
import FormErrors from 'chaire-lib-frontend/lib/components/pageParts/FormErrors';
import ImporterValidator from 'chaire-lib-common/lib/services/importers/ImporterValidator';
import FileUploaderHOC, { FileUploaderHOCProps } from 'chaire-lib-frontend/lib/components/input/FileUploaderHOC';

interface FileImportFormProps extends FileUploaderHOCProps {
Expand All @@ -27,26 +26,25 @@ interface FileImportFormProps extends FileUploaderHOCProps {
const FileImportForm: React.FunctionComponent<FileImportFormProps & WithTranslation> = (
props: FileImportFormProps & WithTranslation
) => {
const validator = props.validator as ImporterValidator;
const [fileSelected, setFileSelected] = React.useState(false);

return (
<form
id={`tr__form-transit-${props.pluralizedObjectsName}-import`}
className={`tr__form-transit-${props.pluralizedObjectsName}-import apptr__form`}
>
<h3>{props.t('main:Import')}</h3>

<div className="tr__form-section">
<div className="apptr__form-input-container _two-columns">
<label>{props.label}</label>
<InputFile
id={`formField${_upperFirst(props.pluralizedObjectsName)}ImporterFile${validator.get('id')}`}
id={`formField${_upperFirst(props.pluralizedObjectsName)}ImporterFile`}
accept={props.acceptsExtension}
inputRef={props.fileImportRef}
onChange={props.onChange}
onChange={() => setFileSelected(true)}
/>
</div>
</div>

<div className="tr__form-buttons-container">
<Button
icon={faUndoAlt}
Expand Down Expand Up @@ -74,10 +72,10 @@ const FileImportForm: React.FunctionComponent<FileImportFormProps & WithTranslat
/>
)}
</div>

<FormErrors errors={validator.getErrors()} />
{/* TODO Add actual upload error information when we have it
<FormErrors errors={validator.getErrors()} /> */}
</form>
);
};

export default FileUploaderHOC(withTranslation('main')(FileImportForm), ImporterValidator);
export default FileUploaderHOC(withTranslation('main')(FileImportForm));

0 comments on commit 39c63e8

Please sign in to comment.