Skip to content

Commit

Permalink
[#163818577] Add error handling for non csv files
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur236 authored and fojuri committed Mar 11, 2019
1 parent e030b9b commit 80605ea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/_actions/assets.action.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const getAssetsAction = (pageNumber, limit, filters, status = '') => {

export const uploadAssets = file => (dispatch) => {
const formData = new FormData();

formData.append('file', file[0]);
dispatch(uploading(true));
return axios
Expand Down
26 changes: 20 additions & 6 deletions src/components/Assets/UploadAssetsComponent.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import React from 'react';
import { isEmpty } from 'lodash';
import PropTypes from 'prop-types';
import { Progress } from 'semantic-ui-react';
import Dropzone from 'react-dropzone';

import { uploadStatus, StatusMessage } from './UploadStatus';
import errorMessage from './UploadErrorMessages';

import '../../_css/UploadAssets.css';

class UploadAssets extends React.Component {
state = {
accepted: [],
rejected: []
};

componentDidUpdate(prevProps) {
if (this.props.downloadedFile !== prevProps.downloadedFile) {
const link = document.createElement('a');
Expand All @@ -17,9 +25,14 @@ class UploadAssets extends React.Component {
}
}

handleDrop = (files) => {
handleDrop = (accepted, rejected) => {
this.resetUpload();
this.props.uploadAssets(files);

this.setState({ accepted, rejected });

if (!isEmpty(accepted)) {
this.props.uploadAssets(accepted);
}
};

resetUpload = () => {
Expand All @@ -32,13 +45,14 @@ class UploadAssets extends React.Component {
};

render() {
const { rejected } = this.state;
const { loading, success, error } = this.props;
const showStatus = success || error;
const showStatus = success || error || !isEmpty(rejected);

return (
<div className="center-upload">
<span className="failed-file">
<a href="# " onClick={() => this.handleFileDownload('files/sample_import_file/')}>
<a href=" " onClick={() => this.handleFileDownload('files/sample_import_file/')}>
Download the sample file
</a>{' '}
, fill the columns and upload it.
Expand All @@ -65,8 +79,8 @@ class UploadAssets extends React.Component {

{showStatus && (
<StatusMessage
message={uploadStatus(success, error)}
className={success.fail || error ? 'error-status' : 'success-status'}
message={uploadStatus(success, error, rejected)}
className={success.fail || (error || !isEmpty(rejected)) ? 'error-status' : 'success-status'}
reset={this.resetUpload}
/>
)}
Expand Down
9 changes: 6 additions & 3 deletions src/components/Assets/UploadStatus.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { isEmpty } from 'lodash';
import PropTypes from 'prop-types';
import { Icon } from 'semantic-ui-react';

Expand All @@ -8,12 +9,14 @@ export const StatusMessage = props => (
</div>
);

export const uploadStatus = (success, error) => {
if (success.fail || error) {
export const uploadStatus = (success, error, rejected) => {
const errorMessage = !isEmpty(rejected) ? 'Only csv files can be imported' : success.fail;

if (success.fail || error || !isEmpty(rejected)) {
return (
<div>
<Icon name="x icon" size="big" />
{success.fail || error}
{errorMessage || error}
</div>
);
}
Expand Down

0 comments on commit 80605ea

Please sign in to comment.