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

[WIP] data upload - send files to backend #98

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
85 changes: 49 additions & 36 deletions src/app-bundles/upload-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import { toast } from 'react-toastify';
import { tSuccess, tError } from 'common/toast/toastHelper';
import { queryFromObject } from 'utils';

const processResponse = (response, type) => (
new Promise((resolve, reject) => {
const func = response.status < 400 ? resolve : reject;
// @TODO: handle different response types
type === 'json' ?
response.json()
.then(json => func({
'status': response.status,
'json': json,
}))
.catch(e => console.error(e)) :
response.blob()
.then(blob => func({
'status': response.status,
'blob': blob,
}))
.catch(e => console.error(e));
})
);

export default {
name: 'upload',
getReducer: () => {
Expand Down Expand Up @@ -31,47 +51,40 @@ export default {
selectUploadLogs: state => state.upload.logs,
selectUploadData: state => state.upload.data,

doUploadAllFiles: (params) => ({ dispatch, apiPost, store }) => {
doUploadAllFiles: (files, version, recorder) => ({ dispatch, apiFetch, store }) => {
dispatch({ type: 'UPLOAD_FILES_START' });
const toastId = toast.loading('Uploading files, please wait...');

const { files, data, recorder } = params;
const {
siteFile = null,
searchEffortFile = null,
telemetryFishFile = null,
missouriRiverFile = null,
fishFile = null,
supplementalFile = null,
proceduresFile = null,
} = data;

const url = '/psapi/upload';
const payload = {
editInitials: recorder,
...siteFile && { siteUpload: { uploadFilename: files.siteFile.name, items: siteFile }},
...searchEffortFile && { searchUpload: { uploadFilename: files.searchEffortFile.name, items: searchEffortFile }},
...telemetryFishFile && { telemetryUpload: { uploadFilename: files.telemetryFishFile.name, items: telemetryFishFile }},
...missouriRiverFile && { moriverUpload: { uploadFilename: files.missouriRiverFile.name, items: missouriRiverFile }},
...fishFile && { fishUpload: { uploadFilename: files.fishFile.name, items: fishFile }},
...supplementalFile && { supplementalUpload: { uploadFilename: files.supplementalFile.name, items: supplementalFile }},
...proceduresFile && { procedureUpload: { uploadFilename: files.proceduresFile.name, items: proceduresFile }},
};

apiPost(url, payload, (err, _body) => {
if (!err) {
dispatch({
type: 'UPDATE_UPLOAD',
payload: _body,
});
dispatch({ type: 'UPLOAD_FILES_FINISHED' });
tSuccess(toastId, 'Successfully uploaded all files!');
store.doFetchUploadSessionLogs({ uploadSessionId: _body.uploadSessionId});
} else {
dispatch({ type: 'UPLOAD_FILES_ERROR', payload: err });
tError(toastId, 'Failed to upload files. Please verify file formats and try again.');
}
});
var data = new FormData();
data.append('editInitials', recorder);
data.append('version', version);
for (let key in files) {
files[key] && data.append('files', files[key]);
}
// PRINTING FORMDATA
// for (var pair of data.entries()) {
// console.log(pair);
// }

apiFetch(url, { method: 'POST', body: data })
.then(response => processResponse(response, 'json'))
.then(response => {
if (response.json.status === 'Success') {
dispatch({ type: 'UPDATE_UPLOAD', payload: body });
dispatch({ type: 'UPLOAD_FILES_FINISHED' });
tSuccess(toastId, 'Successfully uploaded all files!');
store.doFetchUploadSessionLogs({ uploadSessionId: body.uploadSessionId });
} else {
dispatch({ type: 'UPLOAD_FILES_ERROR', payload: err });
tError(toastId, 'Failed to upload files. Please verify file formats and try again.');
}
})
.catch(e => {
dispatch({ type: 'UPLOAD_FILE_ERROR' });
console.error(`Request returned a ${e.status}`);
});
},

doFetchUploadSessionLogs: (params) => ({ dispatch, apiGet }) => {
Expand Down
24 changes: 7 additions & 17 deletions src/app-pages/data-upload/dataUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import DragInput from 'app-components/drag-input';
import Select from 'app-components/select';

import { keyAsText } from 'utils';
import { getIsRequired, reduceCsvState, formatAsNumber, formatJsonKey } from './helper';
import { getIsRequired, reduceCsvState, formatAsNumber, formatJsonKey } from './helper';

import './dataupload.scss';

Expand Down Expand Up @@ -52,18 +52,6 @@ export default connect(
...files,
[key]: file,
});

if (file) {
Papa.parse(file, {
complete: result => dispatch({ type: 'update', key, data: result.data }),
transformHeader: formatJsonKey,
transform: formatAsNumber,
skipEmptyLines: true,
header: true,
});
} else {
dispatch({ type: 'update', key, data: null });
}
};

const submitIsDisabled = () => {
Expand All @@ -79,13 +67,15 @@ export default connect(
};

const uploadAllFiles = () => {
doUploadAllFiles({ files, data: csvData, version, recorder });
doUploadAllFiles(files, version, recorder);
};

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

console.log('files: ', files);

return (
<div className='container-fluid w-75'>
<Card>
Expand All @@ -96,7 +86,7 @@ export default connect(
<Select
className='w-25 d-inline-block mb-1 mr-4'
onChange={value => setVersion(value)}
defaultOption={{ value: '4.2.1' }}
defaultOption={'4.2.1'}
options={[
{ value: '4.2.1' },
{ value: '4.1.3' },
Expand Down Expand Up @@ -152,9 +142,9 @@ export default connect(
<div>{`Date Created: ${uploadLogs[0].dateCreated.split('T')[0]}`}</div>
{uploadLogs.map((log, index) => (
<div key={index} className='text log'>{log.debugText}</div>
))}
))}
</>)
: <div className='text'>No logs to report</div> }
: <div className='text'>No logs to report</div>}
</Card.Body>
</Card>
</div>
Expand Down