1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-05-01 23:56:41 +07:00

fixed uploads with real data

This commit is contained in:
muerwre 2019-08-20 12:05:50 +07:00
parent db0a94b581
commit 38d56838f2
5 changed files with 127 additions and 58 deletions

View file

@ -1,28 +1,35 @@
import { takeEvery, all, spawn, call, put, take, fork, race } from 'redux-saga/effects';
import {
takeEvery, all, spawn, call, put, take, fork, race,
} from 'redux-saga/effects';
import { postUploadFile } from './api';
import { UPLOAD_ACTIONS } from '~/redux/uploads/constants';
import {
uploadUploadFiles, uploadSetStatus, uploadAddStatus, uploadDropStatus, uploadAddFile
uploadUploadFiles,
uploadSetStatus,
uploadAddStatus,
uploadDropStatus,
uploadAddFile,
} from './actions';
import { reqWrapper } from '../auth/sagas';
import { createUploader, uploadGetThumb, fakeUploader } from '~/utils/uploader';
import { createUploader, uploadGetThumb } from '~/utils/uploader';
import { HTTP_RESPONSES } from '~/utils/api';
import { VALIDATORS } from '~/utils/validators';
import { UUID, IFileWithUUID, IFile, IUploadProgressHandler } from '../types';
import { IFileWithUUID, IFile, IUploadProgressHandler } from '../types';
function* uploadCall({ file, temp_id, target, type, onProgress }: IFileWithUUID & { onProgress: IUploadProgressHandler }) {
// return yield call(reqWrapper, fakeUploader, { file: { url: 'some', error: 'cant do this boss' }, onProgress, mustSucceed: true });
return yield call(
reqWrapper,
postUploadFile,
{
file,
temp_id,
type,
target,
onProgress,
}
);
function* uploadCall({
file,
temp_id,
target,
type,
onProgress,
}: IFileWithUUID & { onProgress: IUploadProgressHandler }) {
return yield call(reqWrapper, postUploadFile, {
file,
temp_id,
type,
target,
onProgress,
});
}
function* onUploadProgress(chan) {
@ -42,19 +49,27 @@ function* uploadCancelWorker(id) {
return true;
}
function* uploadWorker({
file, temp_id, target, type,
function* uploadWorker({
file, temp_id, target, type,
}: IFileWithUUID) {
const [promise, chan] = createUploader<Partial<IFileWithUUID>, Partial<IFileWithUUID>>(uploadCall, { temp_id, target, type });
yield fork(onUploadProgress, chan);
const [promise, chan] = createUploader<Partial<IFileWithUUID>, Partial<IFileWithUUID>>(
uploadCall,
{ temp_id, target, type }
);
fork(onUploadProgress, chan);
return yield call(promise, {
temp_id, file, target, type,
temp_id,
file,
target,
type,
});
}
function* uploadFile({ file, temp_id, type, target }: IFileWithUUID) {
function* uploadFile({
file, temp_id, type, target, subject,
}: IFileWithUUID) {
if (!file.type || !VALIDATORS.IS_IMAGE_MIME(file.type)) {
return { error: 'File_Not_Image', status: HTTP_RESPONSES.BAD_REQUEST, data: {} };
}
@ -74,10 +89,13 @@ function* uploadFile({ file, temp_id, type, target }: IFileWithUUID) {
)
);
const {
result, cancel, cancel_editing, save_inventory,
} = yield race({
result: call(uploadWorker, { file, temp_id, target, type }),
const { result, cancel, cancel_editing } = yield race({
result: call(uploadWorker, {
file,
temp_id,
target,
type,
}),
cancel: call(uploadCancelWorker, temp_id),
// subject_cancel: call(uploadSubjectCancelWorker, subject)
// add here CANCEL_UPLOADS worker, that will watch for subject
@ -85,7 +103,7 @@ function* uploadFile({ file, temp_id, type, target }: IFileWithUUID) {
// save_inventory: take(INVENTORY_ACTIONS.SAVE_INVENTORY),
}) as any;
if (cancel || cancel_editing || save_inventory) {
if (cancel || cancel_editing) {
return yield put(uploadDropStatus(temp_id));
}
@ -97,8 +115,6 @@ function* uploadFile({ file, temp_id, type, target }: IFileWithUUID) {
);
}
console.log('upload', data);
yield put(
uploadSetStatus(temp_id, {
is_uploading: false,