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

Fix multiple file uploads breaking which files are saved to request #70

Merged
merged 1 commit into from
Feb 21, 2019
Merged
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 @@ -30,6 +30,14 @@ class NewRequestDialog extends React.PureComponent {
this.state = defaultFormValues;
}

componentDidUpdate(prevProps) {
const { data, isUploading } = this.props;

if (prevProps.isUploading && !isUploading) {
this.save(data);
}
}

validateForm = () => {
if (!this.formRef.current) {
return false;
Expand Down
15 changes: 11 additions & 4 deletions frontend/src/modules/requests/containers/request-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { requestSchema } from '../schemas';

const mapStateToProps = state => {
const { currentRequestId, filesToDelete } = state.requests.viewState;
const filesForUpload = state.requests.files;
const filesUploadState = state.requests.uploads;
const keyPath = `data.entities.requests.${currentRequestId}`;
const isNewRequest = !has(state, keyPath);
const request = get(state, keyPath, {
Expand All @@ -30,13 +32,13 @@ const mapStateToProps = state => {
// Files (saved and current editing session files, uploaded or removed)
const queuedFiles = [];
const queuedSupportingFiles = [];
forIn(state.requests.files, (value, key) => {
if (state.requests.uploads[key] === 'loaded') {
forIn(filesForUpload, (value, key) => {
if (filesUploadState[key] === 'loaded') {
queuedFiles.push(key);
}
});
forIn(state.requests.supportingFiles, (value, key) => {
if (state.requests.uploads[key] === 'loaded') {
if (filesUploadState[key] === 'loaded') {
queuedSupportingFiles.push(key);
}
});
Expand All @@ -47,7 +49,12 @@ const mapStateToProps = state => {
request.supportingFiles,
queuedSupportingFiles
).filter(id => !filesToDelete.includes(id));
const isUploading = values(state.requests.uploads).some(isNumber);
// Determine uploading state
const totalFilesInQueue = values(filesForUpload).length;
const uploadStates = values(filesUploadState);
const isUploading =
uploadStates.length < totalFilesInQueue ||
uploadStates.some(d => d !== 'loaded');
const data = {
...request,
files,
Expand Down
30 changes: 2 additions & 28 deletions frontend/src/modules/requests/sagas.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { all, call, fork, put, select, take } from 'redux-saga/effects';
import { all, call, fork, put, take } from 'redux-saga/effects';
import { channel, delay, eventChannel, END } from 'redux-saga';
import { getToken } from '@src/services/auth';
import difference from 'lodash/difference';
import get from 'lodash/get';
import head from 'lodash/head';
import { normalize } from 'normalizr';
import tus from 'tus-js-client';

import { fileSchema, requestSchema } from './schemas';
import { fileSchema } from './schemas';
import {
uploadFileFailure,
uploadFileProgress,
Expand Down Expand Up @@ -110,31 +109,6 @@ function* uploadFileChannel(item, meta) {
payload: normalize(payload, fileSchema),
});
// Autosave the request
const { isSupportingFile, requestId } = meta;
const { filesToDelete } = yield select(state =>
get(state, 'requests.viewState', [])
);
const request = yield select(state =>
get(state, `data.entities.requests.${requestId}`)
);
yield put({
type: 'request/put',
meta: {
schema: { result: requestSchema },
id: requestId,
hideNotification: true,
url: `/api/v1/requests/save/${requestId}`,
},
payload: {
...request,
files: !isSupportingFile
? syncFilesPayload([...request.files, id], filesToDelete)
: request.files,
supportingFiles: isSupportingFile
? syncFilesPayload([...request.supportingFiles, id], filesToDelete)
: request.supportingFiles,
},
});
yield call(delay, 1500);
yield put(uploadFileReset(payload.id));
return;
Expand Down